g1c9 Posted January 16, 2006 Share Posted January 16, 2006 Hey, I ran this code after submitting a form but for some reason I used phpmyadmin and there were no rows? $email = $_POST['email']; echo $email; $link = md5($email); include("dbinfo.inc.php"); mysql_connect(localhost,$username,$password); @mysql_select_db($database) or die( "Unable to select database"); $query = "INSERT INTO tests VALUES ($email,$link)"; mysql_query($query); mysql_close(); Is the problem the PHP code or the form? Here is the form. <form action="signup.php" method="POST"> <input type="text" name="email" size="40"><br><br> <input type="submit" value="Start tricking your friends!"> </form> I don't know what is wrong... EDIT: I have deduced that it is the form. But whats wrong with the form? Quote Link to comment https://forums.phpfreaks.com/topic/3205-inserting-rows-into-a-db-from-form-data/ Share on other sites More sharing options...
fenway Posted January 16, 2006 Share Posted January 16, 2006 You should verify that your INSERT statement is being run correctly. Quote Link to comment https://forums.phpfreaks.com/topic/3205-inserting-rows-into-a-db-from-form-data/#findComment-10932 Share on other sites More sharing options...
g1c9 Posted January 16, 2006 Author Share Posted January 16, 2006 $query = "INSERT INTO tests VALUES ('cameron.garvie@gmail.com','camerons')"; if (mysql_query($query)) { echo 'inserted'; } else { echo 'nope'; } I ran that code, (values hardcoded), and it returned 'nope'. Why is taht? tests is a table = and it has 2 string columns, email and link. Quote Link to comment https://forums.phpfreaks.com/topic/3205-inserting-rows-into-a-db-from-form-data/#findComment-10933 Share on other sites More sharing options...
fenway Posted January 16, 2006 Share Posted January 16, 2006 Run your query as LazyJones always suggests: [!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Always make your SQL queries like this: $sql = "SELECT ...."; $result = mysql_query($sql) or die ("ERROR: ".mysql_error()." with query: $sql"); And you'll find out what MySQL is complaining about -- personally, I can't stand the fact that your column names aren't explicitly stated. Good luck. Quote Link to comment https://forums.phpfreaks.com/topic/3205-inserting-rows-into-a-db-from-form-data/#findComment-10938 Share on other sites More sharing options...
g1c9 Posted January 17, 2006 Author Share Posted January 17, 2006 Ok, so now my code is looking like this: $email = $_POST["email"]; echo 'email:'.$email.'<br>'; $link = md5($email); include("dbinfo.inc.php"); mysql_connect(localhost,$username,$password); @mysql_select_db($database) or die( "Unable to select database"); $query = "INSERT INTO tests VALUES ('cameron.garvie@gmail.com','camerons')"; $result = mysql_query($query) or die ("ERROR: ".mysql_error()." with query: $sql"); echo $result; mysql_close(); And when run, it spits out this: email:dasd1234asdasd@gmail.com ERROR: Duplicate entry 'cameron.garvie@gmail.com' for key 1 with query: So i check my DB, and it spits out this as the total: email | link ------------- email | link cameron.garvie@gmail.com | camerons Sorry, I'm new to all of this, whats wrong? Quote Link to comment https://forums.phpfreaks.com/topic/3205-inserting-rows-into-a-db-from-form-data/#findComment-10959 Share on other sites More sharing options...
fenway Posted January 17, 2006 Share Posted January 17, 2006 Apparently, you have a UNIQUE key on the email address column -- if so, MySQL will complain if you try and add another record with the same value for this column. Quote Link to comment https://forums.phpfreaks.com/topic/3205-inserting-rows-into-a-db-from-form-data/#findComment-10960 Share on other sites More sharing options...
g1c9 Posted January 17, 2006 Author Share Posted January 17, 2006 I removed the unique key, and then using this code, tried again. $email = $_POST["email"]; echo 'email:'.$email.'<br>'; $link = md5($email); include("dbinfo.inc.php"); mysql_connect(localhost,$username,$password); @mysql_select_db($database) or die( "Unable to select database"); $query = "INSERT INTO tests VALUES ($email,$link)"; $result = mysql_query($query) or die ("ERROR: ".mysql_error()." with query: $sql"); echo $result; mysql_close(); I submitted the form email as cameron.garvie@gmail.com email:Cameron.garvie@gmail.com ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '@gmail.com,0136542e3041addfa391312618e87388)' at line 1 with query: Relevant server info. PHP version 4.3.11 MySQL version 4.1.13-standard Quote Link to comment https://forums.phpfreaks.com/topic/3205-inserting-rows-into-a-db-from-form-data/#findComment-10961 Share on other sites More sharing options...
fenway Posted January 17, 2006 Share Posted January 17, 2006 You need to quote your column values; try the following: $query = "INSERT INTO tests VALUES ('$email','$link')"; BTW, I still think you should name your columns in the INSERT statement explicitly. Quote Link to comment https://forums.phpfreaks.com/topic/3205-inserting-rows-into-a-db-from-form-data/#findComment-10962 Share on other sites More sharing options...
g1c9 Posted January 17, 2006 Author Share Posted January 17, 2006 THANK YOU!!! It finally works, and I'll make sure to PM you when the whole project is up and running. Also, I didn't mean to ignore your suggestion, I'm just not sure what [!--fonto:Lucida Console--][span style=\"font-family:Lucida Console\"][!--/fonto--]explicitly[!--fontc--][/span][!--/fontc--] means. Quote Link to comment https://forums.phpfreaks.com/topic/3205-inserting-rows-into-a-db-from-form-data/#findComment-10963 Share on other sites More sharing options...
fenway Posted January 17, 2006 Share Posted January 17, 2006 Glad you got it working. By explicitly, I meant the following: $query = "INSERT INTO tests ( email, link ) VALUES ('$email','$link')"; Otherwise, the parser simply assumes you mean the first two columns, which is unstable if you ever add any other fields to this table -- which is very likely. Quote Link to comment https://forums.phpfreaks.com/topic/3205-inserting-rows-into-a-db-from-form-data/#findComment-10964 Share on other sites More sharing options...
g1c9 Posted January 17, 2006 Author Share Posted January 17, 2006 Thanks for all your help. Quote Link to comment https://forums.phpfreaks.com/topic/3205-inserting-rows-into-a-db-from-form-data/#findComment-10965 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.