SQL 请求语法


SQL request syntax

我有一个这样的查询:

$modifiedDate1 = date("F d Y H:i:s.", filemtime($file));
$query = "INSERT IGNORE INTO file (filename, modifiedDate1) VALUES ".implode(',', $filenames)."".$modifiedDate1."";

我的第二个值的 SQL 语法有误,为什么?

错误:

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 'January 01 1970 01:00:00.' at line 1

如果它有帮助,整个代码是:

$pdo = new PDO('mysql:host=localhost;dbname=check', 'root', 'root');
if ($handle = opendir('check')) {
    /* This is the correct way to loop over the directory. */
    while (false !== ($file = readdir($handle))) {
        if($file!='.' && $file!='..') {
            $filenames[] = "('".$file."')";
        }
    }
    closedir($handle);
}
$modifiedDate1 = date("F d Y H:i:s.", filemtime($file));
/* insert new record into the table filenames with the filename */
$query = ("INSERT IGNORE INTO file (filename,modifiedDate1) VALUES ('".implode(',', $filenames)."".$modifiedDate1."')");
$stmt = $pdo->exec($query);   
    if (!$stmt) {
   echo "'nPDO::errorInfo():'n";
   print_r($pdo->errorInfo());
}
    print "finished installing your files!";

此代码用于比较数据库中是否已存在文件的数据。

首先,

我建议使用 PDO 或 MySQLi,这样您就可以安全地转义字符串以避免 SQL 注入。

此外,看起来您的$try变量已查询,但我怀疑这是错误,请 vardump 您的$try或回显它并显示您的内容,以便我可以更新我的答案。

编辑:

到目前为止,在您的查询中,您还没有使用单个 qoutes ' ' 来查询您的变量。这:'".implode(',', $filenames)."".$modifiedDate1."'";应该有效。