Mysql SQL语法错误:未知列
我正在尝试运行此查询:Mysql SQL语法错误:未知列,mysql,mysqli,Mysql,Mysqli,我正在尝试运行此查询: $result = db_query("INSERT INTO `timesheets` (clientid, candid, weekending, department, orderno, basicpay, basiccharge, otpay, otcharge, ot2pay, ot2charge, status, hue, huc) VALUES (`$client`, `$cand`, `$week_ending`, `$department`, `$ord
$result = db_query("INSERT INTO `timesheets` (clientid, candid, weekending, department, orderno, basicpay, basiccharge, otpay, otcharge, ot2pay, ot2charge, status, hue, huc) VALUES (`$client`, `$cand`, `$week_ending`, `$department`, `$order_no`, `$basic_pay`, `$basic_charge`, `$ot_pay`, `$ot_charge`, `$ot2_pay`, `$ot2_charge`, `$status`, `$hue`, `$huc`)");
if($result){
print 'Success! ID of last inserted record is';
}
else {
die('Error : ' . db_error());
}
这些是表单中的值
$client = db_quote($_POST['client']);
$cand = db_quote($_POST['cand']);
$order_no = db_quote($_POST['order_no']);
$department = db_quote($_POST['department']);
$week_ending = db_quote($_POST['week_ending']);
$basic_pay = db_quote($_POST['basic_pay']);
$hue = db_quote($_POST['hue']);
$basic_charge = db_quote($_POST['basic_charge']);
$huc = db_quote($_POST['huc']);
$ot_pay = db_quote($_POST['ot1_pay']);
$ot_charge = db_quote($_POST['ot1_charge']);
$ot2_pay = db_quote($_POST['ot2_pay']);
$ot2_charge = db_quote($_POST['ot2_charge']);
$status = 'cand';
在本例中,$client
的值为“725”。我收到的错误是错误:“字段列表”中的未知列“725”
这是怎么回事-时间表表中的列名是
clientid
。我试图将值725放入clientid
列。有语法错误吗?错误消息似乎没有意义?正确的查询如下所示:
"INSERT INTO 'timesheets' (clientid, candid, weekending, department, orderno, basicpay, basiccharge, otpay, otcharge, ot2pay, ot2charge, status, hue, huc)
VALUES ('$client', '$cand', '$week_ending', '$department', '$order_no', '$basic_pay', '$basic_charge', '$ot_pay', '$ot_charge', '$ot2_pay', '$ot2_charge', '$status', '$hue', '$huc')
在MySQL中设置值时,不能使用反勾号字符表示字段,需要使用
'
或“
将变量括起来
使用反勾号是为了命名字段。这是MySQL的一个怪癖
您的查询行应为:
$result = db_query("INSERT INTO `timesheets` (clientid, candid, weekending, department, orderno, basicpay, basiccharge, otpay, otcharge, ot2pay, ot2charge, status, hue, huc) VALUES ('$client', '$cand', '$week_ending', '$department', '$order_no', '$basic_pay', '$basic_charge', '$ot_pay', '$ot_charge', '$ot2_pay', '$ot2_charge', '$status', '$hue', '$huc')");
这样试试
$result = db_query("INSERT INTO `timesheets` (clientid, candid, weekending, department, orderno, basicpay, basiccharge, otpay, otcharge, ot2pay, ot2charge, status, hue, huc) VALUES ('".$client."', '".$cand."', '".$week_ending."', '".$department."', '".$order_no."', '".$basic_pay."', '".$basic_charge."', '".$ot_pay."', '".$ot_charge."', '".$ot2_pay."', '".$ot2_charge."', '".$status."', '".$hue."', '".$huc."')");
在创建查询时,请注意引号 INSERT语句应该是这样的:
INSERT INTO table_name (col_name_1, col_name_2, col_name_3) VALUES('$param_1', '$param_2', '$param_3')
$sqlQuery = "INSERT INTO timesheets (clientid, candid, weekending, department, orderno,basicpay, basiccharge, otpay, otcharge, ot2pay, ot2charge, status, hue, huc) VALUES ('$client', '$cand', '$week_ending', '$department', '$order_no', '$basic_pay', '$basic_charge', '$ot_pay', '$ot_charge', '$ot2_pay', '$ot2_charge', '$status', '$hue', '$huc')";
请注意,表_name
不带引号,而每个值都用单引号分隔
您可以找到官方文档和完整的示例
顺便说一下,对代码的查询应该是这样的:
INSERT INTO table_name (col_name_1, col_name_2, col_name_3) VALUES('$param_1', '$param_2', '$param_3')
$sqlQuery = "INSERT INTO timesheets (clientid, candid, weekending, department, orderno,basicpay, basiccharge, otpay, otcharge, ot2pay, ot2charge, status, hue, huc) VALUES ('$client', '$cand', '$week_ending', '$department', '$order_no', '$basic_pay', '$basic_charge', '$ot_pay', '$ot_charge', '$ot2_pay', '$ot2_charge', '$status', '$hue', '$huc')";
这将导致语法错误,因为变量
$status
是字符串,需要在statement@gabe3886我现在得到了那个错误!错误:“字段列表”中的未知列“cand”。使用“$status”修复了您是否声明了“cand”?是的,如果列type在表中为varchar。@IanButler该部门很可能是一个字符串,但对db_quote
的调用可能会在使用前正确包装字符串。因为此SQL中没有引用值,所以任何字符串都会导致问题中的错误,表名部分不在引号中,它在后面的勾号中带有is comple非常有效,在某些地方,您可以在执行语句之前打印查询吗?我使用的是这个值($client、$cand、$week\u ending、$department、$order\u no、$basic\u pay、$basic\u charge、$ot\u pay、$ot\u charge、$ot2\u charge、$status’、$huc)列类型都是不同长度的varchar。那么为什么我可以将$department保留为不带引号,但使用$status这样做会产生错误?$department不是字符串吗?