mysql_select_db("musicDB", $con);
$sql = mysql_query("INSERT INTO Users (username, fname, lname, email, dob, password, occupation, genre )
VALUES ('$_POST[username]', '$_POST[fname]', '$_POST[lname]', '$_POST[email]', '$_POST[dob]', sha1('$_POST[password]'), '$_POST[occupation]', '$_POST[genre]')");
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
?>
我是PHP的新手,我收到了这个SQL错误:
查询为空。
我做错了什么?
答案 0 :(得分:1)
$sql = mysql_query("INSERT INTO Users (username, fname, lname, email, dob, password, occupation, genre )
VALUES ('$_POST[username]', '$_POST[fname]', '$_POST[lname]', '$_POST[email]', '$_POST[dob]', sha1('$_POST[password]'), '$_POST[occupation]', '$_POST[genre]')");
if (!sql)
{
是正确的。你试图做
if(!mysql_query(mysql_query("....")))
{
答案 1 :(得分:1)
mysql_select_db("musicDB", $con);
// that's dirty but at least something to protect that silly code
$_POST['password'] = sha1($_POST['password'].$_POST['username']);
foreach($_POST as $key => $value) $_POST[$key] = mysql_real_escape_string($value);
$sql = "INSERT INTO Users (username, fname, lname, email, dob, password, occupation, genre )
VALUES ('$_POST[username]', '$_POST[fname]', '$_POST[lname]', '$_POST[email]',
'$_POST[dob]', '$_POST[password]', '$_POST[occupation]', '$_POST[genre]')");
if (!mysql_query($sql,$con))
{
trigger_error(mysql_error()." ".$sql);
} else {
echo "1 record added";
}
?>
答案 2 :(得分:0)
在声明字符串查询变量时,不要只使用$sql = mysql_query(...);
$sql = "...";
,它应该可以正常工作