如何使用 mySQLi 的预准备语句更新多个表?
我有一个表单,其中有两个字段,名称属性为“photo_title”和“photographer_name”,以及一个名为“photo_id”的隐藏字段。 当用户按下提交按钮时,我希望它更新数据库中的两个单独的表。 我可以让它更新单个表,但是当我尝试左连接第二个表时,它不喜欢它。
我认为我的查询字符串或绑定可能有问题。 如何在仍然使用准备好的语句的同时更新 Mysql 数据库中两个单独表中的两个单独值?
这是 PHP:
if (array_key_exists('update', $_POST)) {
$sql = 'UPDATE photos SET photos.photo_title = ?, photographers.photographer_name = ?
LEFT JOIN photographers ON photos.photographer_id = photographers.photographer_id
WHERE photo_id = ?';
$stmt = $conn->stmt_init();
if ($stmt->prepare($sql)) {
$stmt->bind_param('ssi', $_POST['photo_title'], $_POST['photographer_name'], $_POST['photo_id']);
$done = $stmt->execute();
}
}
这是形式:
<form id="form1" name="form1" method="post" action="">
<input name="photo_title" type="text" value=""/>
<textarea name="photographer_name"></textarea>
<input type="submit" name="update" value="Update entry" />
<input name="photo_id" type="hidden" value="<?php echo $photo_id ?>"/>
</form>
I have a form with two fields with the name attribute of 'photo_title' and 'photographer_name', and a hidden field named 'photo_id'. When the user pushes the submit button, i want it to update two separate tables in the database. I can get it to update a single table, but as soon as I try to leftjoin the second table, it doesn't like it.
I think there may be something wrong with my query string or the binding. How can I update two separate values in two separate tables in my Mysql database while still using prepared statements?
Here's the PHP:
if (array_key_exists('update', $_POST)) {
$sql = 'UPDATE photos SET photos.photo_title = ?, photographers.photographer_name = ?
LEFT JOIN photographers ON photos.photographer_id = photographers.photographer_id
WHERE photo_id = ?';
$stmt = $conn->stmt_init();
if ($stmt->prepare($sql)) {
$stmt->bind_param('ssi', $_POST['photo_title'], $_POST['photographer_name'], $_POST['photo_id']);
$done = $stmt->execute();
}
}
Here's the form:
<form id="form1" name="form1" method="post" action="">
<input name="photo_title" type="text" value=""/>
<textarea name="photographer_name"></textarea>
<input type="submit" name="update" value="Update entry" />
<input name="photo_id" type="hidden" value="<?php echo $photo_id ?>"/>
</form>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个答案,以便阅读此问题的人看到它,而不是在上面的评论中找到它。 我会标记此 CW,这样我就不会获得任何积分。
FWIW,MySQL UPDATE 语法的文档是说明性的。
Here's an answer so folks who read this question see it, instead of finding it in your comment above. I'll mark this CW so I don't get any points for it.
FWIW, the documentation for MySQL's UPDATE syntax is illustrative.
我正在做类似的事情。 这是我所做的一些事情。 希望它有帮助
您甚至可以将您的 photo_id 添加到表单操作中,以防您想在不同页面上使用该 id。
我创建了一个文件,该文件连接到我命名为 config 的数据库,其中包含以下代码。 将其包含在此代码中,其中您的表单位于顶部,这样执行上面的代码时就不会出现错误。
I was working on something similar. Here is a few things that I did. Hope it helps
You can even add your photo_id to the form action in case you want to use the id on a different page.
I have a file I created that connects to the database that I named config that has the following codes. Include it with this code where your form is at the top so you don't get errors executing the code above.