簡體   English   中英

PHP嘗試使用准備好的語句將數據插入兩個不同的表中

[英]PHP Trying to insert data into two different tables using prepared statment

我已經為此工作了幾天,似乎無法找到我要去的地方,我認為這很愚蠢,但是由於我的大學導師從未使用過准備好的陳述,而他幾乎沒有用。

第一條語句可以很好地解決問題,第二條語句不會將我的任何數據輸入數據庫。 我的目標是獲取通過表單傳遞的信息(我可以確定不想被信息轟炸,因為我確定這不是問題),並獲取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");
        }

我認為問題是您正在嘗試從INSERT語句獲取照片ID。

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

據我所知,這可能不會獲取任何有意義的信息。

要獲得自動增量值,通常會調用...

$photoID = mysqli_insert_id($conn);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM