silverglade Posted August 28, 2011 Share Posted August 28, 2011 I am trying to insert with a form the information below into the database, but I don't know how to link it to a specific user in the DB. Below is the SQL dump and my code for the insert page. Any help greatly appreciated. also, I get the error ,"duplicate entry for key 2" - phpMyAdmin SQL Dump -- version 2.8.0.1 -- http://www.phpmyadmin.net -- -- Host: custsql-sl07.eigbox.net -- Generation Time: Aug 28, 2011 at 07:42 AM -- Server version: 5.0.91 -- PHP Version: 4.4.9 -- -- Database: `photo_artists` -- -- -------------------------------------------------------- -- -- Table structure for table `images` -- CREATE TABLE `images` ( `image_id` bigint(20) unsigned NOT NULL auto_increment, `gallery_user` int(11) NOT NULL, `filename` varchar(255) NOT NULL, `mime_type` varchar(255) NOT NULL, `file_size` int(11) NOT NULL, `file_data` longblob NOT NULL, PRIMARY KEY (`image_id`), UNIQUE KEY `image_id` (`image_id`), KEY `filename` (`filename`), KEY `gallery_user` (`gallery_user`) ) ENGINE=MyISAM AUTO_INCREMENT=38 DEFAULT CHARSET=latin1 AUTO_INCREMENT=38 ; -- Table structure for table `users` -- CREATE TABLE `users` ( `id` int(11) NOT NULL auto_increment, `status` varchar(20) NOT NULL, `lastname` varchar(50) NOT NULL, `dob` date NOT NULL, `gender` varchar(10) NOT NULL, `username` varchar(20) NOT NULL, `password` varchar(60) NOT NULL, `email` varchar(20) NOT NULL, `activationkey` varchar(100) NOT NULL, `sex` varchar(10) NOT NULL, `race` varchar(40) NOT NULL, `job` varchar(40) NOT NULL, `country` varchar(30) NOT NULL, `state` varchar(30) NOT NULL, `town` varchar(30) NOT NULL, `hobbies` mediumtext NOT NULL, `business` varchar(50) NOT NULL, `religion` varchar(40) NOT NULL, `social_groups` mediumtext NOT NULL, `political_groups` varchar(200) NOT NULL, `other_affiliations` varchar(200) NOT NULL, `buying` longtext NOT NULL, `selling` longtext NOT NULL, `links` varchar(100) NOT NULL, `likes` mediumtext NOT NULL, `dislikes` longtext NOT NULL, `firstName` varchar(30) NOT NULL, `zip` varchar(15) NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `username` (`username`), UNIQUE KEY `email` (`email`), UNIQUE KEY `activationkey` (`activationkey`) ) ENGINE=MyISAM AUTO_INCREMENT=54 DEFAULT CHARSET=latin1 AUTO_INCREMENT=54 ; -- -- Dumping data for table `users` -- INSERT INTO `users` VALUES (49, 'verify', '', '0000-00-00', '', 'user2', '32b3491336522e073489725b5daf298cd749007a', '[email protected]', '34705256317118179901016541118525481318594628324', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''); INSERT INTO `users` VALUES (53, '', '', '0000-00-00', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''); INSERT INTO `users` VALUES (52, 'activated', '', '0000-00-00', '', 'user', 'e8ee80e34d93d48813b08949c07fc826a1c82b63', '1@hotmai', '153967352213317143421219669520489072241284552824', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''); and here is the code. note I don't know how to link the insert to the current user. <?php require_once('globals.php'); include('bouncer.php'); $first = $_POST['first']; $last = $_POST['last']; $first = $_POST['dob']; $last = $_POST['gender']; $first = $_POST['ethnic']; $last = $_POST['country']; $first = $_POST['state']; $last = $_POST['town']; $first = $_POST['zip']; $last = $_POST['mail']; $first = $_POST['job']; $last = $_POST['business']; $first = $_POST['religion']; $last = $_POST['social']; $first = $_POST['political']; $last = $_POST['affiliations']; $first = $_POST['political']; $last = $_POST['affiliations']; $first = $_POST['buying']; $last = $_POST['selling']; $first = $_POST['likes']; $last = $_POST['dislikes']; $first = $_POST['links']; $currentUser = $_SESSION['myusername']; mysql_query ( "INSERT INTO users (lastname, firstName, dob, gender, email, race, job, country, state, town, zip, hobbies, business, religion, social_groups, political_groups, other_affiliations, buying, selling, links, likes, dislikes) VALUES ('$last','$first','$dob','$gender','$email','$ethnic','$job','$country','$state','$town','$zip','$hobbies','$business', '$religion','$social','$political','$affiliations','$buying','$selling','$links','$likes','$dislikes')"); echo mysql_error(); mysql_close(); ?> <html> <body> <p>Login Successful</p> <p> </p> <form action="login_success.php" method="post"> <p> <input type="text" name="first" size="20" /> First name<br /> <input type="text" name="last" size="20" /> Last name<br /> <input name="dob" type="text" size="20" id="dob" /> Date of Birth<br /> <input type="text" name="gender" size="20" id="gender" /> Gender <br /> <input type="text" name="ethnic" size="20" id="ethnic" /> Ethnicity <br /> <input type="text" name="country" size="20" id="country" /> Country<br /> <input type="text" name="state" size="20" id="state" /> State<br /> <input type="text" name="town" size="20" id="town" /> Town<br /> <input type="text" name="zip" size="20" id="zip" /> Zip Code<br /> <br /> <input type="text" name="email" size="40" id="email" /> Email<br /> <textarea name="job" cols="40" id="job"></textarea> Job<br /> <textarea name="business" cols="40" id="business"></textarea> Business<br /> <input type="text" name="religion" size="60" id="religion" /> Religion</p> <p><br /> <textarea name="social" cols="100" id="social"></textarea> Social Groups<br /> <textarea name="political" cols="100" id="political"></textarea> Political groups<br /> <textarea name="affiliations" cols="100" id="affiliations"></textarea> Other Affiliations<br /> <textarea name="buying" cols="100" id="buying"></textarea> Items I am buying<br /> <textarea name="selling" cols="100" id="selling"></textarea> Items I am selling<br /> <textarea name="likes" cols="100" id="likes"></textarea> My likes <br /> <textarea name="dislikes" cols="100" id="dislikes"></textarea> My dislikes <br /> <textarea name="links" cols="100" id="links"></textarea> My links <br /> <input type="submit" value="Store in database and search" /> <input type="reset" value="Reset fields" /> </p> </p> </form> <p><a href="index.php">HOME</a></p> <p>Welcome to members page. to log out click <a href="Logout.php">HERE. </a></p> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/245878-form-and-database-problem/ Share on other sites More sharing options...
conan318 Posted August 28, 2011 Share Posted August 28, 2011 try using an auto increment on your primary key in the database. maybe add the id and and 2 blank quotes in the values like this. mysql_query ( "INSERT INTO users (id lastname, firstName, dob, gender, email, race, job, country, state, town, zip, hobbies, business, religion, social_groups, political_groups, other_affiliations, buying, selling, links, likes, dislikes) VALUES ('','$last','$first','$dob','$gender','$email','$ethnic','$job','$country','$state','$town','$zip','$hobbies','$business', '$religion','$social','$political','$affiliations','$buying','$selling','$links','$likes','$dislikes')"); Quote Link to comment https://forums.phpfreaks.com/topic/245878-form-and-database-problem/#findComment-1262834 Share on other sites More sharing options...
conan318 Posted August 28, 2011 Share Posted August 28, 2011 try changing your variable names too $first = $_POST['first']; $last = $_POST['last']; $dob = $_POST['dob']; $gender = $_POST['gender']; $ethnic = $_POST['ethnic']; $country = $_POST['country']; $state = $_POST['state']; $town = $_POST['town']; $zip = $_POST['zip']; $mail = $_POST['mail']; $job = $_POST['job']; $business = $_POST['business']; $religion = $_POST['religion']; $social = $_POST['social']; $political = $_POST['political']; $affiliations = $_POST['affiliations']; $political = $_POST['political']; $affiliations = $_POST['affiliations']; $buying = $_POST['buying']; $selling = $_POST['selling']; $likes = $_POST['likes']; $dislikes = $_POST['dislikes']; $links = $_POST['links']; Quote Link to comment https://forums.phpfreaks.com/topic/245878-form-and-database-problem/#findComment-1262837 Share on other sites More sharing options...
Pikachu2000 Posted August 28, 2011 Share Posted August 28, 2011 You should probably explain in more detail what you need to do, and what has already been done by the point this script runs. Is the user logged in, or is the user identified in some other way, etc.? Quote Link to comment https://forums.phpfreaks.com/topic/245878-form-and-database-problem/#findComment-1262852 Share on other sites More sharing options...
silverglade Posted August 28, 2011 Author Share Posted August 28, 2011 yes the user is logged in, I am trying to get all of the user information, and insert it into the database for now. and it gives me the "Duplicate entry ' for key 2 " error. The code looks ok to me so I don't know what is going wrong, I will have to look over the suggestions because I didn't understand them at first. thanks. Quote Link to comment https://forums.phpfreaks.com/topic/245878-form-and-database-problem/#findComment-1262874 Share on other sites More sharing options...
silverglade Posted August 28, 2011 Author Share Posted August 28, 2011 I checked google and altered my code to make it an update since I want the user to be able to change the data in the database for their info. but I still get the "duplicate entry for key 2" . I removed this email=$email as email is a unique key, but it still gets the error. <?php require_once('globals.php'); $first = $_POST['first']; $last = $_POST['last']; $dob = $_POST['dob']; $gender = $_POST['gender']; $ethnic = $_POST['ethnic']; $country = $_POST['country']; $state = $_POST['state']; $town = $_POST['town']; $zip = $_POST['zip']; $email = $_POST['email']; $job = $_POST['job']; $business = $_POST['business']; $religion = $_POST['religion']; $social = $_POST['social']; $political = $_POST['political']; $affiliations = $_POST['affiliations']; $buying = $_POST['buying']; $selling = $_POST['selling']; $likes = $_POST['likes']; $dislikes = $_POST['dislikes']; $links = $_POST['links']; $currentUser = $_SESSION['myusername']; /*UPDATE orders SET quantity = '11', Product = 'Hanging Files' WHERE id = '1' */ /*USE photo_artists;*/ mysql_query ( "UPDATE users SET dob = $dob, gender=$gender, race=$ethnic, job=$job, country=$country, state=$state, town=$town, zip=$zip, hobbies=$hobbies, business=$business, religion=$religion, social_groups=$social, political_groups=$political, other_affiliations=$affiliations, buying=$buying, selling=$selling, links=$links, likes=$likes, dislikes=$dislikes WHERE id=$currentUser"); ?> <html> <body> <p>Login Successful</p> <form action="login_success.php" method="post"> <p> <input type="text" name="first" size="20" /> First name<br /> <input type="text" name="last" size="20" /> Last name<br /> <input name="dob" type="text" size="20" id="dob" /> Date of Birth<br /> <input type="text" name="gender" size="20" id="gender" /> Gender <br /> <input type="text" name="ethnic" size="20" id="ethnic" /> Ethnicity <br /> <input type="text" name="country" size="20" id="country" /> Country<br /> <input type="text" name="state" size="20" id="state" /> State<br /> <input type="text" name="town" size="20" id="town" /> Town<br /> <input type="text" name="zip" size="20" id="zip" /> Zip Code<br /> <br /> <input type="text" name="email" size="40" id="email" /> Email<br /> <textarea name="job" cols="40" id="job"></textarea> Job<br /> <textarea name="business" cols="40" id="business"></textarea> Business<br /> <input type="text" name="religion" size="60" id="religion" /> Religion</p> <p><br /> <textarea name="social" cols="100" id="social"></textarea> Social Groups<br /> <textarea name="political" cols="100" id="political"></textarea> Political groups<br /> <textarea name="affiliations" cols="100" id="affiliations"></textarea> Other Affiliations<br /> <textarea name="buying" cols="100" id="buying"></textarea> Items I am buying<br /> <textarea name="selling" cols="100" id="selling"></textarea> Items I am selling<br /> <textarea name="likes" cols="100" id="likes"></textarea> My likes <br /> <textarea name="dislikes" cols="100" id="dislikes"></textarea> My dislikes <br /> <textarea name="links" cols="100" id="links"></textarea> My links <br /> <input type="submit" value="Store in database and search" /> <input type="reset" value="Reset fields" /> </p> </p> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/245878-form-and-database-problem/#findComment-1262881 Share on other sites More sharing options...
silverglade Posted August 28, 2011 Author Share Posted August 28, 2011 I also made the form point to the proper page. Quote Link to comment https://forums.phpfreaks.com/topic/245878-form-and-database-problem/#findComment-1262883 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.