这是另一个帖子的后续问题(链接)
我试图围绕我如何使用准备好的语句执行 mysql 更新。我需要添加 WHERE id = ?.. 我对如何从数组中取回它感到困惑。谢谢!
我的表格:
<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();
}
}