Okay, I have searched for an erro开发者_运维百科r in this code for days and haven't succeeded. This is supposed to insert a new user (new row) into a table. For some reason, it does absolutely nothing to the mysql table "users." I can connect and retrieve data from the table perfectly fine. Please help!
include('config.php');
$db = mysql_connect('localhost', $user, $pass);
if(!$db)
{
echo 'Cannot find the database';
exit;
}
$dbname = $user;
mysql_select_db($dbname) or die('Cannot select the database');
(other code that I have not posted because I have tested it without this code and it produces the same result)
$query = "INSERT INTO users VALUES (
NULL,
'$Passw' ,
'$Fname' ,
'$Lname' ,
'$Email' ,
'$Add1' ,
'$Add2' ,
'$City' ,
'$State' ,
'$Zip ,
'$Country' ,
'$Phone' ,
'$Bio')";
$result = mysql_query($query);
1 : Retrieve the possible MySQL error using:
if ($result === false)
echo mysql_error();
2 : It's a good practice to specify the table fields when you use INSERT
INSERT INTO table (field1, field2, field3) VALUES ('value1', 'value2', 'value3');
Because if you don't, and your DB structure changes, you will have to update the VALUES(...)
part.
3 : use mysql_real_escape_string()
when inserting strings in queries for MySQL, to avoid SQL injection problems.
精彩评论