我的数据库设计存在重大缺陷。下面是我的四个表:
会话表:
SessionId SessionName
3 EROEW
问题表:
QuestionId(PK) QuestionNo QuestionContent SessionId (FK)
11 1 Question1 3
12 2 Question2 3
13 3 Question3 3
图片_问题:
ImageQuestionId (PK) ImageId (FK) SessionId (Fk) QuestionNo (FK)
1 1 3 1
2 2 3 2
图片:
ImageId (PK) SessionId (Fk) QuestionNo (FK)
1 3 1
2 3 2
现在正如您在Image_Question
表中看到的那样,QuestionNo
指的QuestionNo
是非唯一的或换句话说非唯一的字段。现在我认为这是不好的做法。
现在我知道你会说为什么不使用QuestionId
. 问题是我不能使用QuestionId
,因为图像是在提交问题之前上传到每个问题的,而我们可以自己提出问题的唯一方法QuestionId
是在用户提交问题之后。
所以我尝试做的是通过QuestionNo
从页面获取SessionId
.
现在我听说这是一种不好的做法,我想改成QuestionNo (FK)
. 但是在提交问题后,我将无法上传文件并插入上传的详细信息以获取,对我来说这是无法完成的。Image_Question
QuestionId (FK)
QuestionId
所以我的问题是,有没有一种方法可以将每个上传的图像存储到临时表中,获取每个图像所属的问题编号和 sessionid,然后从那里能够找到并将值QuestionId
存储在表中?QuestionId
Image_Question
下面是我当前的 php 代码,它在上传图像后插入值:
如果有人可以更新下面的代码,那就太好了,但任何答案都会有所帮助:
move_uploaded_file($_FILES["fileImage"]["tmp_name"],
"ImageFiles/" . $_FILES["fileImage"]["name"]);
$result = 1;
$imagesql = "INSERT INTO Image (ImageFile)
VALUES (?)";
//Dont pass data directly to bind_param store it in a variable
$insert->bind_param("s",$img);
//Assign the variable
$img = 'ImageFiles/'.$_FILES['fileImage']['name']; //GET THE IMAGE UPLOADED
$insert->execute();
$insert->close();
$lastImageID = $mysqli->insert_id;
$_SESSION['lastImageID'] = $lastImageID;
$_SESSION['ImageFile'] = $_FILES["fileImage"]["name"];
$sessid = $_SESSION['id'] . ($_SESSION['initial_count'] > 1 ? $_SESSION['sessionCount'] : ''); GET THE NAME OF THE SESSION
$sessionquery = "SELECT SessionId FROM Session WHERE (SessionName = ?)"; //FIND SESSIONID by finding it's SESSIONNAME
// Bind parameter for statement
$sessionstmt->bind_param("s", $sessid);
// Execute the statement
$sessionstmt->execute();
// This is what matters. With MySQLi you have to bind result fields to
// variables before calling fetch()
$sessionstmt->bind_result($sessionid);
// This populates $sessionid
$sessionstmt->fetch();
$sessionstmt->close();
$imagequestionsql = "INSERT INTO Image_Question (ImageId, SessionId, QuestionNo) //INSERT DETAILS INTO CURRENT IMAGE_QUESTION TABLE
VALUES (?, ?, ?)";
if (!$insertimagequestion = $mysqli->prepare($imagequestionsql)) {
// Handle errors with prepare operation here
echo "Prepare statement err imagequestion";
}
$qnum = (int)$_POST['numimage']; //QUESTION NUMBER IMAGE IS UPLOADED IN
$insertimagequestion->bind_param("iii",$lastImageID, $sessionid, $qnum);
$insertimagequestion->execute();
if ($insertimagequestion->errno) {
// Handle query error here
}
$insertimagequestion->close();