Php 提交详细信息后如何检索id
我的数据库设计有一个主要缺陷,我遇到了麻烦。以下是我的四张表格: 会话表:Php 提交详细信息后如何检索id,php,mysql,sql,database,mysqli,Php,Mysql,Sql,Database,Mysqli,我的数据库设计有一个主要缺陷,我遇到了麻烦。以下是我的四张表格: 会话表: SessionId SessionName 3 EROEW QuestionId(PK) QuestionNo QuestionContent SessionId (FK) 11 1 Question1 3 12 2 Question2 3 13
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
SessionId SessionName
3 EROEW
QuestionId(PK) QuestionContent SessionId (FK)
11 Question1 3
12 Question2 3
13 Question3 3
ImageId (PK)
1
2
ImageId (FK) QuestionId (FK) -- (Composite primary key)
1 11
2 12
问题表:
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
SessionId SessionName
3 EROEW
QuestionId(PK) QuestionContent SessionId (FK)
11 Question1 3
12 Question2 3
13 Question3 3
ImageId (PK)
1
2
ImageId (FK) QuestionId (FK) -- (Composite primary key)
1 11
2 12
图像\u问题:
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
SessionId SessionName
3 EROEW
QuestionId(PK) QuestionContent SessionId (FK)
11 Question1 3
12 Question2 3
13 Question3 3
ImageId (PK)
1
2
ImageId (FK) QuestionId (FK) -- (Composite primary key)
1 11
2 12
图像:
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
SessionId SessionName
3 EROEW
QuestionId(PK) QuestionContent SessionId (FK)
11 Question1 3
12 Question2 3
13 Question3 3
ImageId (PK)
1
2
ImageId (FK) QuestionId (FK) -- (Composite primary key)
1 11
2 12
现在,您可以在图像\u Question
表中看到,QuestionNo
指的是一个非唯一的QuestionNo
,或者换句话说,是一个非唯一的字段。现在我认为这是一个坏习惯
现在我知道你要说为什么不使用QuestionId
。问题是,我不能使用QuestionId
,因为在提交问题之前,图像会上传到每个问题,而我们唯一能给出问题自己的QuestionId
的方法是在用户提交问题之后
因此,我试图通过从页面中获取QuestionNo
以及SessionId
来确定上传图像属于哪个问题
现在我听说这是一种不好的方法,我想将Image\u Question
中的QuestionNo(FK)
更改为QuestionId(FK)
。但在提交问题以获取问题ID
后,我将无法上传文件并插入上传的详细信息,这是无法做到的
因此,我的问题是,有没有一种方法可以将每个上传的图像存储到临时表中,获取每个图像所属的问题编号和会话ID,然后从中找到QuestionId
,并将QuestionId
值存储在image\u 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();
您最大的问题是您的模式没有规范化。这样做会对你有所帮助。
以下是我建议您构建数据库的方式: 会话:
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
SessionId SessionName
3 EROEW
QuestionId(PK) QuestionContent SessionId (FK)
11 Question1 3
12 Question2 3
13 Question3 3
ImageId (PK)
1
2
ImageId (FK) QuestionId (FK) -- (Composite primary key)
1 11
2 12
问题:
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
SessionId SessionName
3 EROEW
QuestionId(PK) QuestionContent SessionId (FK)
11 Question1 3
12 Question2 3
13 Question3 3
ImageId (PK)
1
2
ImageId (FK) QuestionId (FK) -- (Composite primary key)
1 11
2 12
图像:
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
SessionId SessionName
3 EROEW
QuestionId(PK) QuestionContent SessionId (FK)
11 Question1 3
12 Question2 3
13 Question3 3
ImageId (PK)
1
2
ImageId (FK) QuestionId (FK) -- (Composite primary key)
1 11
2 12
图像\u问题:
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
SessionId SessionName
3 EROEW
QuestionId(PK) QuestionContent SessionId (FK)
11 Question1 3
12 Question2 3
13 Question3 3
ImageId (PK)
1
2
ImageId (FK) QuestionId (FK) -- (Composite primary key)
1 11
2 12
表的插入顺序应为:
Session -> Question -
\
-- Image_Question
/
Image -
您将避开潜在的更新问题和数据冲突