我试图在数据库中插入值,但出现以下错误

Error: insert into lead (Name,phone,dob,height,weight,source,city,area,address,status,preferred_mode_of_contact,email,email_verified,style,number_of_classes_per_week,days_of_week,time_start,time_end,duration,start_date,description,preferred_trainer_type,price_per_class,price_per_month,call) value ('','','','','','Source 1','','','','','phone','','0','','1','0','','','','','','','','','2015-07-16')
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 'call) value ('','','','','','Source 1','','','','','phone','','0','','1','0',''' at line 1

我已经检查过我传递的值的数目是否正确没有多余的列没有多余的值。“call”列是日期数据类型。当我从insert查询中删除“call”时,它工作正常
以下是查询
$sql="insert into lead (Name,phone,dob,height,weight,source,city,area,address,status,preferred_mode_of_contact,email,email_verified,style,number_of_classes_per_week,days_of_week,time_start,time_end,duration,start_date,description,preferred_trainer_type,price_per_class,price_per_month,call)
value ('$name','$phone','".$dob."','$Height','$Weight','$Source','$City','$Area','$Address','$Status','$preferred_mode_of_con','$email','$email_verify','$style','$noc','$day','$time_from','$time_to','$duration','".$ts."','$des','$ptt','$price','$price_month','".$call."')";

if (mysqli_query($conn, $sql))
        echo "User registered Sucessfully";
    else
        echo "Error: " . $sql . "<br>" . mysqli_error($conn);
            echo '<br><a href="lead_reg.php">Click Here to go back</a></h3>';

最佳答案

因为CALL是一个保留字,所以如果要将它用作列名,需要用反引号(`)将其括起来。这是正确的查询格式:

$sql="insert into lead (Name,phone,dob,height,weight,source,city,area,address,status,preferred_mode_of_contact,email,email_verified,style,number_of_classes_per_week,days_of_week,time_start,time_end,duration,start_date,description,preferred_trainer_type,price_per_class,price_per_month,`call`) ".
     "value ('$name','$phone','".$dob."','$Height','$Weight','$Source','$City','$Area','$Address','$Status','$preferred_mode_of_con','$email','$email_verify','$style','$noc','$day','$time_from','$time_to','$duration','".$ts."','$des','$ptt','$price','$price_month','".$call."')";

另外,我不知道你为什么对某些变量,如$dob有不同的处理。您可以使用与$name相同的语法。

关于php - 在数据库中插入错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31695528/

10-11 02:44
查看更多