2015-01-26 30 views
-2

下面的代碼被拆分爲3,都在單獨的文件中,我不斷收到錯誤,並且不確定錯誤,因爲所有變量名都與數據庫匹配。格式不正確?bind_parm上的錯誤

請有人可以幫助我的錯誤是:

Fatal error: Call to a member function bind_param() on a non-object

我做編輯,刪除,添加等功能。

刪除:

<?php 
$db = new mysqli('localhost','root','','system'); 

$sql = "DELETE FROM (id, staff_id, start_time,  
end_time) WHERE id=1"; 

$stmt = $db->prepare($sql); 

$stmt->bind_param('iiss', $id, $staff_id, $start_time,  
$end_time); 

$stmt->execute(); 
$stmt->close(); 
?> 

更新:

<?php 
$db = new mysqli('localhost','root','','system'); 

$sql = "INSERT INTO roster(id, staffid, starttime, endtime) VALUES 
(?,?,?,?)"; 

$stmt = $db->prepare($sql); 

$stmt->bind_param("iiss", $id, $staffid, $starttime, $endtime); 
$stmt->execute(); 
$stmt->close(); 
?> 

編輯:

<?php 
$db = new mysqli('localhost','root','','system'); 


$sql = "UPDATE roster(id, staff_id,starttime, endtime) WHERE id=1)"; 

$stmt = $db->prepare($sql); 

$stmt->bind_param("iiss", $id, $staffid, $starttime, $endtime); 
$stmt->execute(); 
    $stmt->close(); 
?> 
+1

你不檢查,如果你的連接成功,那麼您的連接後補充一點:如果'($ DB-> connect_error){ 模具(「連接錯誤('。 $ db-> connect_errno。 ')' 。 $ DB-> connect_error); }' – Rizier123 2015-01-26 23:33:18

+0

我認爲你的問題是刪除文件。你在那裏做什麼?還有你的編輯文件?什麼? – Darren 2015-01-26 23:34:22

+0

連接是好的,我知道它連接 – user3552356 2015-01-26 23:36:27

回答

1

你的查詢都是錯誤的。您的UPDATEDELETE陳述完全不正確。一個UPDATE聲明看起來是這樣的:

UPDATE table SET col = val, col2 = val2 WHERE col3 = val3 

當你DELETE必須是結構是這樣的:

DELETE FROM table WHERE col1 = val1 

並與bind_param()你的問題,是因爲你不指定任何列綁定到(就像你在INSERT查詢做)


這些功能讀了起來:

+1

*啊,* si signore。 – 2015-01-26 23:38:03

+1

還要添加他想要插入5個值到4列:D – Rizier123 2015-01-26 23:41:32

+1

...'(?,?,?,?,?)'和4綁定'bind_param(「iiss」,$ id,$ staffid,$ starttime ,$ endtime)' – 2015-01-26 23:43:26