PHP尝试使用准备好的语句将数据插入两个不同的表中

时间:2018-10-29 14:26:54

标签: php sql database insert prepared-statement

我已经为此工作了几天,似乎无法找到我要去的地方,我认为这很愚蠢,但是由于我的大学导师从来没有使用过准备好的陈述,所以他几乎没有采用。

第一个语句的处理没有问题,第二个语句没有将我的任何数据输入数据库。我的目标是获取通过表单传递的信息(我可以确定不想被信息轰炸,因为我确定这不是问题),并获取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");
        }

1 个答案:

答案 0 :(得分:2)

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

await bot1.Start();

据我所知,这可能不会带来任何有意义的事情。

要获取自动增量值,通常可以调用...

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