I'm learning and new in PDO. The usage of array in PDO causes difficulty for me. I'm developing simple web application using PDO syntax. Everything is going on smoothly, but I can't update multiple values from single submit button.
HTML FORM
<td>
<input type="number" name="roll[]" value="<?php echo $roll?>">
</td>
<td>
<input type="text" name="name[]" value="<?php echo $name?>">
<td>
I can print data.
if(isset($_POST['submit'])){
$name = $_POST['name'];
$roll = $_POST['roll'];
foreach( $roll as $key => $n ){
print "The name is ".$name[$key]." and email is ".$roll[$key].", thank you\n";
}
}
But I can't update multiple value at once. Perhaps it is because of lack of knowledge in terms of combination of array in PDO. I've searched in internet. But I found only advance question and answers. I can't found any example or discussion topics in this matter. I am sure I am 100% wrong, so following code doesn't work. Please help how to update using array
PHP CODE
if(isset($_POST['submit'])){
$name = $_POST['name'];
$roll = $_POST['roll'];
foreach( $roll as $key => $n ){
$sql = "UPDATE student SET name=:name WHERE roll=:roll";
$query = $con->prepare($sql);
$query->bindparam(':roll', $roll[$key]);
$query->bindparam(':name', $name[$key]);
$query->execute();
}
}
bindValues()? And more importantly, do you have any error handling, is PDO set up to throw exceptions?