This is a follow-up question to another post (link)
I'm trying to wrap my head around how I perform a mysql update using a prepared statement. I need to add the WHERE id = ?.. I'm confused as to how I'm getting that back from my array. Thanks!
My form:
<form action="update.php" method="post">
<input type="hidden" name="id" value=""/>
<input type="text" name="name" value=""/>
<input type="text" name="age" value=""/>
<input type="hidden" name="id" value=""/>
<input type="text" name="name" value=""/>
<input type="text" name="age" value=""/>
<input type="submit" value="submit" name="submit" />
</form>
PHP:
// Create statement object
$stmt = $db->stmt_init();
if (isset($_POST['submit'])) {
// Create a prepared statement
if($stmt->prepare("UPDATE contact SET (name, age) VALUES (?, ?)")) {
// Bind your variables to replace the ?s
$stmt->bind_param('si', $name, $age);
$returnedData = $_POST['data'];
for($i=0;$i<count($returnedData);$i+=3){
$id = $returnedData[$i]['id']
$name = $returnedData[$i+1]['name'];
$age = $returnedData[$i+2]['age'];
$stmt->execute();
}
// Close statement object
$stmt->close();
}
}