I have a login system and I am currently converting it to PDO, but I think there is something wrong with my code as whenever I run the original code it works and when I run the PDO it doesn't (there are no errors in the console).
This is my original code:
$update_otp = "UPDATE classadmin SET code = $code, status = $status WHERE code = $fetch_code";
$update_res = mysqli_query($con, $update_otp);
This is my PDO converted code:
$update_otp = $conn->prepare("UPDATE classadmin SET code = :code, status = :status WHERE code = :code");
$update_res = $update_otp->execute(['code' => $code, 'status' => $status, 'code' => $fetch_code]);
code
occurs twice as a named argument in the given statement, with two different values. This won't work, unless $code === $fetch_code
.
This might help you out, using distinct names for the named arguments (you are completely free to choose them, they don't need to match the column name):
$update_otp = $conn->prepare("UPDATE classadmin SET code = :newCode, status = :status WHERE code = :oldCode");
$update_res = $update_otp->execute(['newCode' => $code, 'status' => $status, 'oldCode' => $fetch_code]);
When using emulated prepared statements (this is the default with PDO), the same placeholder can appear multiple times in the SQL. You have :code
twice in the SQL. The value that you provide for :code
in the execute
will be put in place of the two placeholders named :code
.
In PHP, associative arrays cannot have the same value with the same key. The one that comes later will overwrite the value with the same key.
$update_otp = $conn->prepare("
UPDATE classadmin
SET
code = :code,
status = :status
WHERE code = :code
");
$update_res = $update_otp->execute([
'code' => $code,
'status' => $status,
'code' => $fetch_code // <-- This one will overwrite the first $code
]);
To fix the problem use distinctly named placeholders or use positional arguments instead.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.