2017-06-03 69 views
-3

我想提出一個形式,在更新數據庫中的數據,但它扔我這個錯誤Catchable fatal error: Object of class mysqli_result could not be converted to string on line 73開捕致命錯誤:類mysqli_result的對象不能轉換爲字符串上線73

我已經試過輸出所有數據和全部正在打印但不在數據庫中更新。

請給一些建議,如何解決呢?

$id = $_GET['id']; 
 
$t_id = $_GET['table_id'] - 2; 
 

 
if(isset($_POST['submit'])){ 
 

 
    $name = $_POST['name']; 
 
    $phone = $_POST['phone']; 
 
    $email = $_POST['email']; 
 
    $gender = intval($_POST['gender']); 
 
    $treatment = intval($_POST['treatment']); 
 
    $source = intval($_POST['source']); 
 
    $status_ = intval($_POST['status']); 
 
    $remark = $_POST['remark']; 
 

 
//below code is line 73 
 

 
$leadUpdateSql = "UPDATE lead SET 
 
        name = $name 
 
        ,phone = $phone 
 
        ,email = $email 
 
        ,gender_id = $gender 
 
        ,treatment_id = $treatment 
 
        ,source_id = $source 
 
        ,status_id = $status 
 
        ,remark = $remark 
 
        WHERE lead.id = $id"; 
 

 
if ($conn->query($leadUpdateSql) === TRUE) { 
 
    echo "Record updated successfully"; 
 
    header("location:edit.php?id=$id&table_id=$t_id&updated=successfully"); 
 
    } 
 

 
}

+0

瞭解準備語句以防止SQL injection.also周圍字符字段的qoutes將設置自動 – Jens

+0

我沒有看到你的代碼任何可能會拋出異常。沒有什麼可以創建一個「mysqli_result類的對象」。 –

回答

1

使SQL有:email = '.$email.'。您最好打印SQL字符串並在MySQL CMD或PHPMyAdmin中執行以查明。

+0

你應該建議在其他領域有同樣的問題,你應該解釋爲什麼單引號是必要的 – Jens

+0

是的,謝謝。由於SQL注入,單引號是必要的,並且可以防止上述意外錯誤。 –

+0

沒有單一的qoutes Gas與SQL注入無關。同時添加解釋的答案不作爲評論 – Jens

0

我設法解決了這個問題。我已經爲字符串值添加了引號。

$id = $_GET['id']; 
 
$t_id = $_GET['table_id'] - 2; 
 

 
if(isset($_POST['submit'])){ 
 

 
    $name = $_POST['name']; 
 
    $phone = $_POST['phone']; 
 
    $email = $_POST['email']; 
 
    $gender = intval($_POST['gender']); 
 
    $treatment = intval($_POST['treatment']); 
 
    $source = intval($_POST['source']); 
 
    $status_ = intval($_POST['status']); 
 
    $remark = $_POST['remark']; 
 

 

 

 
$leadUpdateSql = "UPDATE lead SET 
 
     name='$name' // here was the issue, now added quotes 
 
     ,phone='$phone' // here was the issue 
 
     ,email='$email' // here was the issue 
 
     ,gender_id=$gender 
 
     ,treatment_id=$treatment 
 
     ,source_id=$source 
 
     ,status_id=$status_ 
 
     ,remark='$remark' // here was the issue 
 
     WHERE lead.id=$id"; 
 

 
if ($conn->query($leadUpdateSql) === TRUE) { 
 

 
    header("location:index.php?updated=successfully&table_id=$table_id#$t_id"); 
 
    } 
 

 
else{ 
 
    header("location:edit.php?id=$id&table_id=$t_id&error=unsuccess"); 
 
} 
 
}

相關問題