簡體   English   中英

它沒有在表中插入數據

[英]It is not inserting data into a Table

我下面有一些php代碼,可將數據添加到2個表中,一個稱為“圖像表”,另一個稱為“ Image_Question”表。 我面臨的問題是,當它插入“ Image”表時,它沒有將任何數據插入“ Image_Question”表中。

現在我知道PHP代碼很好,因為它曾經能夠毫無問題地將數據插入兩個表中。

只有將索引和外鍵添加到表中后,此問題才開始發生。

以下是將數據插入兩個表中的PHP代碼:

move_uploaded_file($_FILES["fileImage"]["tmp_name"],"ImageFiles/" . $_FILES["fileImage"]["name"]);

$imagesql = "INSERT INTO Image (ImageFile) VALUES (?)";

if (!$insert = $mysqli->prepare($imagesql)) {
      // Handle errors with prepare operation here
}

//Don't pass data directly to bind_param store it in a variable
$insert->bind_param("s",$img);

//Assign the variable
$img = 'ImageFiles/'.$_FILES['fileImage']['name'];

$insert->execute();

if ($insert->errno) {
   // Handle query error here
}

$insert->close();

$lastID = $mysqli->insert_id;         

$imagequestionsql = "INSERT INTO Image_Question (ImageId, SessionId, QuestionId)  VALUES (?, ?, ?)"; 

if (!$insertimagequestion = $mysqli->prepare($imagequestionsql)) { 
   // Handle errors with prepare operation here 
   echo "Prepare statement err imagequestion"; 
} 

$qnum = (int)$_POST['numimage'];

$insertimagequestion->bind_param("isi",$lastID, $sessid, $qnum); 

$sessid =  $_SESSION['id'] . ($_SESSION['initial_count'] > 1 ? $_SESSION['sessionCount'] : ''); 

    $insertimagequestion->execute(); 

                if ($insertimagequestion->errno) { 
          // Handle query error here 
        } 

        $insertimagequestion->close(); 

這是圖像表,圖像_問題表以及問題表的SHOW CREATE TABLE的輸出,因為圖像_問題表與該表相關:

圖片表:

CREATE TABLE `Image` (
 `ImageId` int(10) NOT NULL AUTO_INCREMENT,
 `ImageFile` varchar(250) NOT NULL,
 PRIMARY KEY (`ImageId`)
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8

Image_Question表:

CREATE TABLE `Image_Question` (
 `ImageQuestionId` int(10) NOT NULL AUTO_INCREMENT,
 `ImageId` int(10) NOT NULL,
 `SessionId` varchar(10) NOT NULL,
 `QuestionId` int(5) NOT NULL,
 PRIMARY KEY (`ImageQuestionId`),
 KEY `FK_QuestionImage` (`ImageId`),
 KEY `questionId` (`QuestionId`),
 KEY `sessionId` (`SessionId`),
 CONSTRAINT `FK_Image_Question` FOREIGN KEY (`SessionId`) REFERENCES `Question` (`SessionId`) ON DELETE CASCADE,
 CONSTRAINT `FK_question` FOREIGN KEY (`QuestionId`) REFERENCES `Question` (`QuestionId`) ON DELETE CASCADE,
 CONSTRAINT `FK_QuestionImage` FOREIGN KEY (`ImageId`) REFERENCES `Image` (`ImageId`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8

問題表:

CREATE TABLE `Question` (
 `SessionId` varchar(10) NOT NULL DEFAULT '',
 `QuestionId` int(5) NOT NULL,
 `QuestionContent` varchar(5000) NOT NULL,
 `NoofAnswers` int(2) NOT NULL,
 `AnswerId` int(10) NOT NULL AUTO_INCREMENT,
 `ReplyId` varchar(2) NOT NULL,
 `QuestionMarks` int(4) NOT NULL,
 `OptionId` varchar(3) NOT NULL,
 PRIMARY KEY (`SessionId`,`QuestionId`),
 KEY `FK_Option_Table` (`OptionId`),
 KEY `FK_IndividualQuestion` (`QuestionId`),
 KEY `FK_Reply` (`ReplyId`),
 KEY `FK_AnswerId` (`AnswerId`)
) ENGINE=InnoDB AUTO_INCREMENT=76 DEFAULT CHARSET=utf8
$img = 'ImageFiles/'.$_FILES['fileImage']['name'];

$insert->bind_param("s",$img);

用這個代替

$insert->bind_param("s",$img);

//Assign the variable
$img = 'ImageFiles/'.$_FILES['fileImage']['name'];

暫無
暫無

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

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