2016-01-07 105 views
0

我試圖創建一個使用從以前的頁面發佈數據的INSERT語句,並將其綁定到聲明錯誤綁定參數在PHP /庫MySQLi

我的SQL INSERT語句是這樣的:

// insert new user 
$db = createConnection(); 
$insertquery="insert into comments (com, userid, comdate, blogpost) values (?,?,?,?)"; 
$inst=$db->prepare($insertquery); 
$inst->bind_param("sisi", $comment, $user, $today, $id); 
$inst->execute(); 
$inst->close(); 

當我打開調試PHP這是錯誤我收到

Fatal error: Call to a member function bind_param() on boolean in /blog/xcomment.php on line 29 

正在傳遞中的bind_param的數據

Array ([comment] => My New Comment [blogid] => 1) 2016-01-07 12:31:50 

$user = $currentuser['userlevel']; 
$comment = $_POST['comment']; 
$id = $_POST['blogid']; 
$today = date("Y-m-d H:i:s"); 

回答

1

嘗試使用單引號代替:

$inst->bind_param('sisi', $comment, $user, $today, $id); 
-1

http://php.net/manual/en/mysqli-stmt.prepare.php

$ DB->準備 - 返回boolean變量。

它的正確用法:

<?php 
$mysqli = new mysqli("localhost", "my_user", "my_password", "world"); 

/* check connection */ 
if (mysqli_connect_errno()) { 
    printf("Connect failed: %s\n", mysqli_connect_error()); 
    exit(); 
} 

$city = "Amersfoort"; 

/* create a prepared statement */ 
$stmt = $mysqli->stmt_init(); 
if ($stmt->prepare("SELECT District FROM City WHERE Name=?")) { 

    /* bind parameters for markers */ 
    $stmt->bind_param("s", $city); 

    /* execute query */ 
    $stmt->execute(); 

    /* bind result variables */ 
    $stmt->bind_result($district); 

    /* fetch value */ 
    $stmt->fetch(); 

    printf("%s is in district %s\n", $city, $district); 

    /* close statement */ 
    $stmt->close(); 
} 

/* close connection */ 
$mysqli->close(); 
?>