【问题标题】:PHP Trying to insert data into two different tables using prepared statmentPHP尝试使用准备好的语句将数据插入两个不同的表
【发布时间】:2019-04-02 12:15:57
【问题描述】:

我已经为此工作了几天,似乎找不到哪里出错了,我想这很愚蠢,但由于我的大学导师在他几乎没有甚至没有之前从未使用过准备好的陈述采用。

第一个语句可以正常工作,第二个不会将我的任何数据输入到我的数据库中。我的目标是获取通过表单传递的信息(我可以包括不想用信息轰炸,因为我确信这不是问题)并获取作为我图片表中的主键的 PictureID 并插入这也是我的图片价格表中的其他信息。

欢迎任何帮助,我是该网站的新手,所以请温柔:)

<?php

include_once "dbh.php";

if (empty($imageTitle) || empty($imageDesc)) {
    header("Location:changes.php?upload=empty");
    exit();
} else {
    $sql = "SELECT * FROM pictures;";
    $sqltwo = "SELECT * FROM pictureprice;";
    $stmt = mysqli_stmt_init($conn);
    if (!mysqli_stmt_prepare($stmt, $sql)) {
        header("Location: changes.php?sqlerror=failed");
        exit();
    } else {     //Gallery order//
        mysqli_stmt_execute($stmt);
        $result = mysqli_stmt_get_result($stmt);
        $rowCount = mysqli_num_rows($result);
        $setImageOrder = $rowCount + 1;



        $sql = "INSERT INTO pictures (PhotographerID, PictureFolderPath, 
        imageDesc, imgFullNameGallery, orderGallery) VALUES (?, ?, ?, ?, 
         ?);";
        if (!mysqli_stmt_prepare($stmt, $sql)) {
            header("Location: changes.php?sqlerror=failedtoinputdata");
            exit();
        } else {
            mysqli_stmt_bind_param($stmt, "issss", $_SESSION['PhotographerID'], $fileDestination, $imageDesc, $imageFullName, $setImageOrder);
            mysqli_stmt_execute($stmt);
            move_uploaded_file($fileTempName, $fileDestination);

            $result = mysqli_stmt_get_result($stmt);
            $row = mysqli_fetch_assoc($result);
            $photoID = $row["PictureID"];     //new
            header("Location:changes.php?upload=success11");
        }

        $sqltwo = "INSERT INTO pictureprice 
      (PictureID, PictureSize, PictureSize2, PictureSize3, PictureSize4, 
      PicturePrice, PicturePrice2, PicturePrice3, PicturePrice4) VALUES (?, 
         ?, ?, ?, ?, ?, ?, ?, ?);";
        if (!mysqli_stmt_prepare($stmt, $sqltwo)) {
            header("Location: changes.php? 
       sqlerror=failedtoinputdatapictureprice");
            exit();
        } else {
            mysqli_stmt_bind_param($stmt, "issssiiii", $photoID, $picturesize1, $picturesize2, $picturesize3, $picturesize4, $price1, $price2, $price3, $price4);
            mysqli_stmt_execute($stmt);
            header("Location:changes.php?upload=success");
        }

【问题讨论】:

  • 为什么你的第二个 SQL 以 SELECT * FROM pictureprice INSERT INTO pi... 开头?
  • 抱歉,它确实从 INSERT INTO 开始,但我尝试了各种不同的方法,我将修改回原来的样子。对不起
  • 您没有将参数绑定到第二个语句 mysqli_stmt_bind_param ?, ?, ?, ?, ?, ?, ?, ?没有被 vars 替换 mysqli_stmt_bind_param 需要再次调用
  • 好吧,我假设您必须绑定参数才能将信息锁定到占位符中.. 抱歉,直到现在我才能看到您的全部答案,怎么办关于为 vars 创建占位符
  • @talsibony 我以为是mysqli_stmt_bind_param($stmt, "issssiiii", $photoID, $picturesize1...

标签: php sql database insert prepared-statement


【解决方案1】:

我认为问题在于您正试图从 INSERT 语句中获取照片 ID...

    $result = mysqli_stmt_get_result($stmt);
    $row = mysqli_fetch_assoc($result);
    $photoID = $row["PictureID"];     //new

这可能不会获取任何有意义的东西(据我所知)。

要获得自动增量值,您通常会调用...

$photoID = mysqli_insert_id($conn);

【讨论】:

  • Aahhh 对,我知道你从哪里来,我从来不知道,这是我第一次尝试获取 w3 学校或以前从未说过的自动增量编号。谢谢你的帮助,我会调查这个
  • 查看此功能,我可以看到您已经为我指明了正确的方向,非常感谢您的帮助,我将在了解该功能后立即尝试此功能。再次感谢奈杰尔,我非常感谢任何和所有的帮助
  • 您的解决方案运行良好,回复花了很长时间,因为直到我意识到我也忘记执行语句 smacks head
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-11-29
  • 1970-01-01
  • 2019-09-24
  • 1970-01-01
  • 2018-04-16
  • 2016-04-05
  • 1970-01-01
相关资源
最近更新 更多