收到错误'错误:INSERT INTO`call``logs`(id,hashkey,)VALUES(,XXX,)'

时间:2017-08-24 10:36:18

标签: php mysql

我一直在从cdr中检索调用日志并将它们转储到MySQL中的数据库中。最近数据库崩溃,并给了我重复和垃圾字符所以我修改为以下代码。   修改后的代码

 $file1 = file_get_contents('file:///C:/Users/thy/Desktop/2011_0419_1531_v3.12R/cdr/'.$newname, FILE_USE_INCLUDE_PATH);

$arr1 = explode("\n", $file1);
foreach ($arr1 as $key => $value) {
    $colArray = [];
    $colArray['id'] = null;
    $colArray['hashkey'] = md5(uniqid(rand(), true));

    $split = explode(";", $value);
    foreach ($split as $key => $val) {
        # code...
        $arr   = (explode('=', $val));
        $field = 'ch';
        $item  = '0';
        $field = $arr[0];
        $item  = $arr[1];
        $item  = str_replace(str_split(')(\/'), '', $item);

        $colArray[$field] = $item;
    }

    $columns = implode(', ', array_keys($colArray));
    $values = implode(', ', $colArray);
    $sql = "INSERT INTO `call`.`logs` (" . $columns . ") VALUES (" . $values . ")";

    if ($conn->query($sql) === TRUE) {
        echo "New record created successfully";
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }
}

上面的代码一直给我错误

  

错误:INSERT INTO calllogs(id,hashkey,)VALUES(,   797d8782a433b30e196fafc0ce01d09b,)
您的SQL中有错误   句法;查看与MySQL服务器版本对应的手册   使用接近&#39;)VALUES的正确语法(,   797d8782a433b30e196fafc0ce01d09b,)&#39;在第1行

原始代码位于下方(我修改后的代码)

$ file1 = file_get_contents(&#39; file:/// C:/Users/thy/Desktop/2011_0419_1531_v3.12R/cdr/'。$ newname,FILE_USE_INCLUDE_PATH);     $ arr1 = explode(&#34; \ n&#34;,$ file1);

$data1 = array();


foreach ($arr1 as $key => $value) {
$split = explode(";", $value);

$keys = md5(uniqid(rand(), true));
//insert key to identify call. 
$sql = "INSERT INTO `call`.`logs` (`id`, `hashkey`) VALUES (NULL, '{$keys}')";
    if ($conn->query($sql) === TRUE) {
        echo "New record created successfully";
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }

foreach ($split as $key => $val) {
    # code...
    $arr=(explode('=', $val));
    $field='ch';
    $item='0';
    $field=$arr[0];
    $item=$arr[1];

    echo $field . " --";
    echo "<br/>";
    echo $item;
//sql 

$sql = "UPDATE logs SET {$field}='{$item}' WHERE hashkey='{$keys}'";

if ($conn->query($sql) === TRUE) {
    echo "Record updated successfully";
} else {
    echo "Error updating record: " . $conn->error;
}

    echo "done";

//$conn->close();

}   echo "<br/>";
}
?>

<?php 

2 个答案:

答案 0 :(得分:0)

我认为问题在于生成/设计的INSERt语句

  

INSERT INTO calllogs(&#34;。$ columns。&#34;)VALUES(&#34;。$ values。&#34;)&#34 ;;

如果没有列名,会附加额外的逗号吗?

答案 1 :(得分:0)

如果 id 是自动增量字段。试试这个:

INSERT INTO call.logs (id, hashkey) VALUES (default, '797d8782a433b30e196fafc0ce01d09b')