yangkai9999 Posted November 2, 2009 Share Posted November 2, 2009 Hello,I just learn how to use PHP to insert a record to MySQL.This is my code:<?phpmysql_connect("localhost","root","Openit4me");mysql_select_db("mytest") or die( "Unable to select database/ this is my note");$query = "INSERT INTO p2 ( v1, v2, v3, v4 ) VALUES ('$v1','$v2','$v3','$v4');";mysql_query($query);if (!mysql_query($query)) { die('Error: ' . mysql_error()); }echo "1 record added";mysql_close();?> There are two problems:1. when I submit the data, it insert 2 duplicate records for me. 2. the values in $v1 ... $v4 were not entered into database.I would like to know where cause the error form the code.Thanks, Link to comment Share on other sites More sharing options...
jeffman Posted November 2, 2009 Share Posted November 2, 2009 mysql_query($query);if (!mysql_query($query))Here's part of your problem. You're calling mysql_query twice with the same data. What you really want is to capture the result:$result = mysql_query($query);Now you can test $result. For many operations, the result is a resource you'll need to get information you're requesting. So if you're in the habit of getting it all the time, you'll never go wrong.FWIW, my implementation of mysql requires `backticks` around the field names. Link to comment Share on other sites More sharing options...
justsomeguy Posted November 2, 2009 Share Posted November 2, 2009 You also haven't set your $v variables to anything, that's why they're blank. If you want that to be form data you need to get the data from the form. Link to comment Share on other sites More sharing options...
yangkai9999 Posted November 2, 2009 Author Share Posted November 2, 2009 You also haven't set your $v variables to anything, that's why they're blank. If you want that to be form data you need to get the data from the form.what do your mean about "haven't set your $v variables to anything". I'm not quite understand that. Would you please give me an example?Thank you. Link to comment Share on other sites More sharing options...
Err Posted November 2, 2009 Share Posted November 2, 2009 You need to set your $v variables to something. Right now all your $v variables have nothing in it because you're not assigning a value to it.You can assign values by using:$v1 = "first value";$v2 = "second value";$v3 = "third value";$v4 = "fourth value";So when you use the insert query you're assigning something instead of nothing.http://www.w3schools.com/php/php_variables.asphttp://www.php.net/manual/en/language.variables.basics.php Link to comment Share on other sites More sharing options...
yangkai9999 Posted November 2, 2009 Author Share Posted November 2, 2009 You need to set your $v variables to something. Right now all your $v variables have nothing in it because you're not assigning a value to it.You can assign values by using:$v1 = "first value";$v2 = "second value";$v3 = "third value";$v4 = "fourth value";So when you use the insert query you're assigning something instead of nothing.http://www.w3schools.com/php/php_variables.asphttp://www.php.net/manual/en/language.variables.basics.php thank you, i'll try that. Link to comment Share on other sites More sharing options...
jeffman Posted November 2, 2009 Share Posted November 2, 2009 Wow. I just assumed you left that part out on purpose, so we could see the problem more clearly. Link to comment Share on other sites More sharing options...
yangkai9999 Posted November 2, 2009 Author Share Posted November 2, 2009 Wow. I just assumed you left that part out on purpose, so we could see the problem more clearly.Thank you all of you. the code work now. Link to comment Share on other sites More sharing options...
thescientist Posted November 3, 2009 Share Posted November 3, 2009 Wow. I just assumed you left that part out on purpose, so we could see the problem more clearly. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.