SQL语法错误:未知列

时间:2015-07-22 10:04:49

标签: 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`, `$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"。我得到的错误是错误:未知栏'' ''''在'字段列表'。

这怎么可能 - 时间表表格中的列名是clientid。我试图将值725放入clientid列。某处有语法错误吗?错误信息似乎没有意义吗?

4 个答案:

答案 0 :(得分:1)

正确的查询将如下所示:

$rootScope

答案 1 :(得分:1)

在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')");

答案 2 :(得分:0)

试试这种方式

tempString = (string("cmd.exe /C \"") + tempString + "\"");

STARTUPINFO si = { sizeof(STARTUPINFO) };

PROCESS_INFORMATION pi;

CreateProcess(NULL, (LPSTR)tempString.c_str(), 0, 0, FALSE, CREATE_UNICODE_ENVIRONMENT, NULL, 0, &si, &pi);

答案 3 :(得分:0)

请在创建查询时注意引号。

INSERT 语句应该是这样的:

INSERT INTO table_name (col_name_1, col_name_2, col_name_3) VALUES('$param_1', '$param_2', '$param_3')

请注意,table_name不带引号,而每个值都用单引号分隔。

您可以找到官方文档here和完整示例here

顺便说一下,对代码的查询应该是这样的:

 $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')";