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 -
您将避开潜在的更新问题和数据冲突