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

标签 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;
    }
}

上面的代码总是给我错误

Error: INSERT INTO call.logs (id, hashkey, ) VALUES (, 797d8782a433b30e196fafc0ce01d09b, )
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 ') VALUES (, 797d8782a433b30e196fafc0ce01d09b, )' at line 1

原始代码如下(我修改过的代码)

$file1 = file_get_contents('file:///C:/Users/thy/Desktop/2011_0419_1531_v3.12R/cdr/'.$newname, FILE_USE_INCLUDE_PATH); $arr1 = 爆炸("\n", $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 

最佳答案

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

INSERT INTO call.logs (" . $columns . ") VALUES (" . $values . ")";

在没有列名的情况下附加了一个额外的逗号?

关于php - 收到错误 'Error: INSERT INTO ` call`.`logs` (id, hashkey, ) VALUES (, XXX, )',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45859344/

相关文章:

php 包括打印 1

mysql - NOT EXISTS 子句

php - 通过多次提交在 PHP 中存储外键的最佳方法?

javascript - 使用 jQuery 获取提交的表单值,有超过 1 个具有相同类名的表单

php - Laravel 8+ 上的 unisharp/laravel-ckeditor 无法安装包

mysql - 如何将 mysql 查询执行到 cf7 按钮操作中

mysql - mysql中如何获取周末天数?

PHP 会跳过检查并插入数据,即使数据已经存在

php - 命名 php 文件

PHP 性能和冗余