使用SET和预准备语句插入INTO

时间:2014-01-31 00:20:43

标签: php mysql mysqli

$query = "INSERT INTO api_fails SET file=?, code=?, url=?, date=".time().")";

$stmt = $mysqli->stmt_init();
if(!$stmt->prepare($query)) {
    echo("Failed to prepare statement.<br />\n");
} else {
    $file = (isset($_GET['f'])) ? $_GET['f'] : '' ;
    $code = (isset($_GET['c'])) ? $_GET['c'] : '' ;
    $url  = (isset($_GET['u'])) ? $_GET['u'] : '' ;
    $stmt->bind_param("sis", $file, $code, $url);
    if($stmt->execute()) {
        echo("Inserted.<br />\n");
    }else {
        echo("Didn't insert.<br />\n");
    }
}

我得到以下输出:

Failed to prepare statement.

2 个答案:

答案 0 :(得分:0)

尝试

    $query = "INSERT INTO api_fails SET file=?, code=?, url=?, date=?)";

    $stmt = $mysqli->stmt_init();
    if(!$stmt->prepare($query)) {
     echo("Failed to prepare statement.
\n"); } else { $file = (isset($_GET['f'])) ? $_GET['f'] : '' ; $code = (isset($_GET['c'])) ? $_GET['c'] : '' ; $url = (isset($_GET['u'])) ? $_GET['u'] : '' ; $time = time(); $stmt->bind_param("sisi", $file, $code, $url,$time); if($stmt->execute()) { echo("Inserted.
\n"); }else { echo("Didn't insert.
\n"); } }

答案 1 :(得分:0)

根据您的工作查询,我会说您的错误在日期值附近缺少引号:

$query = "INSERT INTO api_fails SET file=?, code=?, url=?, date='".time()."')";