John Davidson

mysql - PHP Mysqli update Where IN from serialize ajax post get variables in array

0 comments
Message:


I am trying to update my SQL database but I get error Uncaught mysqli_sql_exception: No data supplied for parameters in prepared statement


I dont understand why I get this error. I use the folowing to sent data to my PHP


$.ajax({  
url:"UpdateMultipleAccounts.php",
method:"POST",
data: $(".bootbox-body #myForm").serialize() +"&UpdatedID="+checkedid,

This gives the folowing payload response in network tab


TypeChecked: 0
AccType:
CategoryChecked: 0
Category:
RankerChecked: 0
Ranker:
NotesChecked: 0
Notes:
UpdatedID: 8548,8551,8544

And I use the folowing PHP code to make the update


print_r($_POST["UpdatedID"]);


$NotesResult = "NotesTest";
$in8 = str_repeat('?,', count($_POST["UpdatedID"]) - 1) . '?';
$types29 = str_repeat('i', count($_POST["UpdatedID"]));
$sql8 = "UPDATE account SET notes = ? WHERE id IN (0,$in8) AND door = 'Martin' ";
$stmt8 = $mysqli->prepare($sql8);
$stmt8->bind_param('s' . $types29,$NotesResult, ... $_POST["UpdatedID"]);
$stmt8->execute();
$stmt8->close();

Thje response from the print_r($_POST["UpdatedID"]); in the response tab is


8548,8551,8544

So the variables are posted correctly, they are received correctly, why is my code telling me that there is nu data suplied? What am I doing wrong? Do I need to do something with the variable $_POST["UpdatedID"] before using it in the sql? I first need this to work before I can get the other post variables implented..


PS This worked when I did the ajax like this, so I am thinking it has something to do with the way the data is posted?


$.ajax({ type: "POST", url: "UpdateMultipleAccounts.php", data: {UpdatedID: checkedid},


My Answer:
"update.php",
method: "POST",
data: { ids: ids },
success: function(response) {
console.log(response);
}
});

In your PHP code, you need to retrieve the data sent from the AJAX request and use it in your SQL query. Here is an example of how you can do this:

php
$ids = $_POST['ids'];
$ids = serialize($ids);

$mysqli = new mysqli("localhost", "username", "password", "database");

$stmt = $mysqli->prepare("UPDATE table SET column = ? WHERE id IN (?)");
$stmt->bind_param("ss", $value, $ids);

$value = "new value";

$stmt->execute();

if ($stmt->affected_rows > 0) {
echo "Update successful";
} else {
echo "Update failed";
}

$stmt->close();
$mysqli->close();
?>


Make sure to adjust the database connection details and the SQL query to match your specific setup. Also, ensure that the `ids` variable is properly serialized before sending it in the AJAX request.

Rate this post

3 of 5 based on 5154 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us