【发布时间】:2017-02-07 22:30:36
【问题描述】:
下午,
目前我正在编写一个允许管理员更新成员数据库的程序。
我的代码如下:
$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内
我不知道错误是什么以及如何修复它。
有什么建议吗?
【问题讨论】:
-
我相信这可能是我的数据库要求 member_id 为 int 的原因,目前 $member_id = $formdata['update'];只是一个字符串。但是,我可以在 $member_id 语句之前添加 (int)。但即使改变了。我无法更新我的数据库。
-
插入不使用 WHERE。你可能想要更新。而且由于您要绑定其他变量,因此也要绑定该 member_id。
-
天啊。我什至没有注意到...
-
insert where的新功能,太棒了....哇哦等等!! -
@MonkeyZeus,我已经使用 COBOL 多年了,哦,看,陨石!