如何使用 mySQLi 的预准备语句更新多个表?

发布于 2024-07-25 01:53:20 字数 1117 浏览 6 评论 0原文

我有一个表单,其中有两个字段,名称属性为“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沫离伤花 2024-08-01 01:53:20

这是一个答案,以便阅读此问题的人看到它,而不是在上面的评论中找到它。 我会标记此 CW,这样我就不会获得任何积分。

UPDATE photos LEFT JOIN photographers 
  ON photos.photographer_id = photographers.photographer_id 
SET photos.photo_title = ?, photographers.photographer_name = ? 
WHERE photos.photo_id = ?

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.

UPDATE photos LEFT JOIN photographers 
  ON photos.photographer_id = photographers.photographer_id 
SET photos.photo_title = ?, photographers.photographer_name = ? 
WHERE photos.photo_id = ?

FWIW, the documentation for MySQL's UPDATE syntax is illustrative.

简单 2024-08-01 01:53:20

我正在做类似的事情。 这是我所做的一些事情。 希望它有帮助

if (isset($_POST['update'])) {
    $id=intval($_GET['photo_id']);
    $photo_title=$_POST['photo_title'];
    $photographer_name=$_POST['photographer_name'];

    $sql = "update photos p, photographers pg set p.photo_title=:photo_title, pg.photographer_name=:photographer_name where p.photographer_id=$id and pg.photographer_id=$id";

    $query = $dbh->prepare($sql);
    $query->bindParam(':photo_title',$photo_title,PDO::PARAM_STR);
    $query->bindParam(':photographer_name',$photographer_name,PDO::PARAM_STR);
    $query->execute();
}

您甚至可以将您的 photo_id 添加到表单操作中,以防您想在不同页面上使用该 id。

<form id="form1" name="form1" method="post" action="index.php?id=<?php echo $photo_id;?>">
    <input name="photo_title" type="text" value=""/>
    <textarea name="photographer_name"></textarea>
    <input type="submit" name="update" value="Update entry" />
</form>

我创建了一个文件,该文件连接到我命名为 config 的数据库,其中包含以下代码。 将其包含在此代码中,其中您的表单位于顶部,这样执行上面的代码时就不会出现错误。

<?php
    // DB credentials.
    define('DB_HOST','localhost');
    define('DB_USER','root');
    define('DB_PASS','');
    define('DB_NAME','photographer');
    // Establish database connection.
    try{
        $dbh = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME,DB_USER, 
        DB_PASS,array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"));
        }
    catch (PDOException $e){
        exit("Error: " . $e->getMessage());}
?>

I was working on something similar. Here is a few things that I did. Hope it helps

if (isset($_POST['update'])) {
    $id=intval($_GET['photo_id']);
    $photo_title=$_POST['photo_title'];
    $photographer_name=$_POST['photographer_name'];

    $sql = "update photos p, photographers pg set p.photo_title=:photo_title, pg.photographer_name=:photographer_name where p.photographer_id=$id and pg.photographer_id=$id";

    $query = $dbh->prepare($sql);
    $query->bindParam(':photo_title',$photo_title,PDO::PARAM_STR);
    $query->bindParam(':photographer_name',$photographer_name,PDO::PARAM_STR);
    $query->execute();
}

You can even add your photo_id to the form action in case you want to use the id on a different page.

<form id="form1" name="form1" method="post" action="index.php?id=<?php echo $photo_id;?>">
    <input name="photo_title" type="text" value=""/>
    <textarea name="photographer_name"></textarea>
    <input type="submit" name="update" value="Update entry" />
</form>

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.

<?php
    // DB credentials.
    define('DB_HOST','localhost');
    define('DB_USER','root');
    define('DB_PASS','');
    define('DB_NAME','photographer');
    // Establish database connection.
    try{
        $dbh = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME,DB_USER, 
        DB_PASS,array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"));
        }
    catch (PDOException $e){
        exit("Error: " . $e->getMessage());}
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文