Mysql插入在脚本中失败

时间:2015-02-02 14:29:11

标签: php mysql sql mysql-error-1064

我一直在反对这一点,但我仍然无法理解为什么不能这样做

include ("db_conn.php");

//function for sanitizing the user input
function clean_input($data)
{
$data = stripslashes($data);
$data = trim($data);
$data = htmlspecialchars($data);
return $data;
}
//registration form data validation
if(!empty($_POST['s_register_submit']))
{
$Salutation= $F_Name= $L_Name= $email= $pass= $P_Number = "";

$Salutation= clean_input($_POST['Salutation']);
$F_Name= clean_input($_POST['F_Name']);
$L_Name= clean_input($_POST['L_Name']);
$email= clean_input($_POST['email']);
$pass= clean_input($_POST['pass']);
$P_Number= clean_input($_POST['P_Number']);
$query= "INSERT INTO STUDENT (Salutation, F_Name, L_Name, email, password, phone) 
         VALUES
         (`$Salutation`,`$F_Name`,`$L_Name`,`$email`,`$pass`,`$P_Number`)";

$query1= mysqli_query($dbhandle,$query) || die("Unable to insert"); 
echo "Saved";

以下是db_conn.php的内容

$user="root";
$password="";
$host="localhost";
$dbname="Interns";

$dbhandle=mysqli_connect($host, $user, $password, $dbname);
if(!$dbhandle)
{
die("Unable to connect");
}
echo "Connected";

直接运行db_conn.php或通过包含它的脚本运行db_conn.php时,它仍会回显"已连接"仍然会返回"无法插入"。

1 个答案:

答案 0 :(得分:3)

用引号括起字符串,而不是刻度。刻度线保留用于标识符。

$query= "INSERT INTO STUDENT (Salutation, F_Name, L_Name, email, password, phone) 
     VALUES
     (`$Salutation`,`$F_Name`,`$L_Name`,`$email`,`$pass`,`$P_Number`)";

应该是

$query= "INSERT INTO STUDENT (Salutation, F_Name, L_Name, email, password, phone) 
     VALUES
     ('$Salutation','$F_Name','$L_Name','$email','$pass','$P_Number')";