[英]Why does this UPDATE procedure update every row?
I have a MySQL stored procedure like this 我有一个这样的MySQL存储过程
UPDATE `Discounts` SET `Occupation`=occupation,
`Organization`=organization,
`LastName`=lastName,
`FirstName`=firstName,
`Email`=email,
`Phone`=phone,
`Description`=description,
`ExpirationDate`=expiration,
`Notes`=notes
WHERE `ID` = id
and I'm calling it with this PHP 我用这个PHP来称呼它
$occupation = $_POST["occupation"];
$organization = $_POST["organization"];
$last = $_POST["last"];
$first = $_POST["first"];
$email = $_POST["email"];
$phone = $_POST["phone"];
$description = $_POST["description"];
$notes = $_POST["notes"];
$expiration = date("Y-m-d H:i:s", strtotime($_POST["expiration"]));
$id = intval($_POST["id"], 10);
$password = $_POST["password"];
$mysqli = new mysqli("localhost", "xxx", $password, "xxxxxxxx");
if ($mysqli->connect_errno) {
die("Could not connect");
}
$stmt = mysqli_stmt_init($mysqli);
if (mysqli_stmt_prepare($stmt, 'CALL UpdateDiscount(?,?,?,?,?,?,?,?,?,?)')) {
mysqli_stmt_bind_param($stmt, "isssssssss",
$id,
$occupation,
$last,
$first,
$email,
$phone,
$description,
$organization,
$notes,
$expiration);
mysqli_stmt_execute($stmt);
mysqli_stmt_close($stmt);
echo "Success!";
}
The update works exactly as I expected except that it updates every single row instead of the one row corresponding to the ID. 该更新的工作方式与我预期的完全相同,只是它更新的是每一行而不是与ID对应的一行。 I can't understand why this is happening, I have a
WHERE 'ID'=id
check. 我不明白为什么会这样,我有
WHERE 'ID'=id
检查。 What is going on? 到底是怎么回事? How can I make it so that it only updates a single row?
我如何才能使它仅更新一行?
Because `ID` is the case sensitive name of your column and id is the case insensitive name of the same column. 因为ID是列的区分大小写的名称,而id是同一列的不区分大小写的名称。
edit this is wrong : You should be using a PHP variable where the lowercase id is. 编辑这是错误的 :您应该使用一个PHP变量,其中小写的id是。 Something like $id.
类似于$ id。
In your case you're calling a procedure with bound parameters. 在您的情况下,您正在调用带有绑定参数的过程。
Use a different name for the id parameter. 为id参数使用其他名称。
It's an issue of name-scoping local variable beloging to the procedure, versus argument variable belonging to the procedure versus the table column name. 这是一个将作用域命名为过程的名称范围局部变量,而属于该过程的参数变量与表列名称是一个问题。
In stored procedures, when a name conflict occurs between field and parameter names, the parameters are used. 在存储过程中,当字段名称和参数名称之间发生名称冲突时,将使用参数。
Your query is parsed as: 您的查询被解析为:
UPDATE ...
WHERE :id = :id
which is always true (unless you pass a NULL
) 这始终是正确的(除非您传递
NULL
)
Prepend the parameter names with an underscore: 在参数名称前加上下划线:
CREATE PROCEDURE myprc (_id, _occupation, ...)
AS
BEGIN
UPDATE mytable
SET occupation = _occupation
WHERE id = _id;
END;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.