此部件工作良好
$stmt= $db->prepare("INSERT INTO books (title) VALUES (?)");
$booktitle=$_POST['booktitle'];
$stmt->bind_param('s', $booktitle); // bind $sample to the parameter
// escape the POST data for added protection
$booktitle = isset($_POST['booktitle'])
? $db->real_escape_string($_POST['booktitle'])
: '';
$stmt->execute();
这部分不起作用
$stmta= $db->prepare("INSERT INTO books (author) VALUES (?) WHERE title = '$booktitle' ");
$author=$_POST['author'];
$stmta->bind_param('s', $author); // bind $sample to the parameter
// escape the POST data for added protection
$author = isset($_POST['author'])
? $db->real_escape_string($_POST['author'])
: '';
$stmta->execute();
问题在于使用WHERE?我试着在没有WHERE的情况下使用相同的代码,结果成功了。怎么是对的?
您需要更新行,而不是插入新行。
UPDATE books SET author = ? WHERE title = '$booktitle'
此外,标题也应该绑定。