簡體   English   中英

更新成員表 PDO php 時出現 SQL 錯誤

[英]SQL error when updating member table PDO php

下午,

目前我正在編寫一個程序,允許管理員更新成員數據庫。

我的代碼如下:

    $member_id = $formdata['update']; 
$surname = $formdata['surname'];
$other_name = $formdata['othername'];
$contactmethod = $formdata['contactmethod'];
$email = $formdata['email'];
$mobilenum = $formdata['mobilenum'];
$phonenum = $formdata['phonenum'];
$occupation = $formdata['occupation'];    
$userpass = $formdata['userpass'];
if(!isset($formdata['magazine']))
   $magazine = 0;
else
   $magazine = 1;

//Get ready to talk to the DB
$db = getDBConnection();
//Make a prepared query so that we can use data binding and avoid SQL injections. 
$insertUser = $db->prepare('INSERT into member VALUES
                          (:surname, :other_name, :contact_method,
                           :email, :mobile, :landline, :magazine, :street,
                           :suburb, :postcode, :password,
                           :occupation) WHERE member_id=$member_id');
//Bind the data from the form to the query variables.
//Doing it this way means PDO sanitises the input which prevents SQL injection.
$insertUser->bindParam(':surname', $surname, PDO::PARAM_STR);
$insertUser->bindParam(':other_name', $other_name, PDO::PARAM_STR);
$insertUser->bindParam(':contact_method', $contactmethod, PDO::PARAM_STR);
$insertUser->bindParam(':email', $email, PDO::PARAM_STR);
$insertUser->bindParam(':mobile', $mobilenum, PDO::PARAM_STR);
$insertUser->bindParam(':landline', $phonenum, PDO::PARAM_STR);
$insertUser->bindParam(':magazine', $magazine, PDO::PARAM_INT);
$insertUser->bindParam(':street', $streetaddr, PDO::PARAM_STR);
$insertUser->bindParam(':suburb', $suburbstate, PDO::PARAM_STR);
$insertUser->bindParam(':postcode', $postcode, PDO::PARAM_INT);
$insertUser->bindParam(':password', $userpass, PDO::PARAM_STR);
$insertUser->bindParam(':occupation', $occupation, PDO::PARAM_STR);

當前錯誤在WHERE member_id=$member_id

我不知道錯誤是什么以及如何修復它。

有小費嗎?

嘗試使用更新。

'UPDATE member SET surname = :surname, other_name = :other_name, contact_method = :contact_method,
                           email = :email, mobile = :mobile, landline = :landline, magazine = :magazine, street = :street,
                           suburb = :suburb, postcode = :postcode, password = :password,
                           occupation = :occupation) WHERE member_id = :member_id'

此外,為 member_id 綁定另一個參數,否則做其他的沒有多大意義

$insertUser->bindParam(':member_id', $member_id, PDO::PARAM_INT);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM