插入连接到多个选择选项ID的相同ID

时间:2015-04-13 23:55:23

标签: php html

我需要让多个FacultyID的数组也连接到FACULTYPUBLICATION表中的同一个PubID。

  • FacultyName是一个多选选项字段
  • 发布是单个插入并创建自动生成的ID
  • 将出版物ID插入PUBLICATION表
  • FacultyID来自预定义的查找表
  • 将PubID和FacultyID插入到FACULTYPUBLICATIONS表中
  • 问题:当选择多个教师时,只有1个教师插入到FACULTYPUBLICATIONS中。我需要找到一种方法将单个PubID连接到FACULTYPUBLICATIONS表中的多个教师

        //insert form values into database                                                                                                                                                   
    $sql = "SELECT JournalName, JournalID, Rating, JournalActive from JOURNAL where JournalActive = 1;";      
    //Can take out JournalActive if we do not want it                                                                                                                        
    $result = mysqli_query($conn, $sql);                                                                                                                                                 
    if (!$result) {                                                                                                                                                                      
        $message  = 'Invalid query: ' . mysql_error() . "\n";                                                                                                                            
        $message .= 'Whole query: ' . $query;                                                                                                                                            
        die($message);                                                                                                                                                                   
        echo "there was an issue";                                                                                                                                                       
    } 
    
    $sql2 = "SELECT FName, LName, FacultyID from FACULTY where FacultyActive = 1;"; 
    //Can take out JournalActive if we do not want it                                                                                                                        
    $result2 = mysqli_query($conn, $sql2);                                                                                                                                                 
    if (!$result2) {                                                                                                                                                                      
        $message  = 'Invalid query: ' . mysql_error() . "\n";                                                                                                                            
        $message .= 'Whole query: ' . $query;                                                                                                                                            
        die($message);                                                                                                                                                                   
        echo "there was an issue";                                                                                                                                                       
    } 
    
        //array to hold all of the data                                                                                                                                                      
    $journals = array();                                                                                                                                                                  
        //print out all of the first names in the database   
    $rownumber = 0;                                                                                                                                 
    while ($row = mysqli_fetch_assoc($result)) {                                                                                                                                         
        $journals[$rownumber][0] = $row['JournalName'];
        $journals[$rownumber][1] = $row['JournalID'];
        $journals[$rownumber][2] = $row['JournalRating'];
        $journals[$rownumber][3] = $row['JournalActive'];
        $rownumber++;                                                                                                                                                       
    }
    
    $faculty = array();                                                                                                                                                                  
        //print out all of the first names in the database   
    $rownum = 0;                                                                                                                                 
    while ($row = mysqli_fetch_assoc($result2)) {                                                                                                                                         
        $faculty[$rownum][0] = $row['FName'];
        $faculty[$rownum][1] = $row['LName'];
        $faculty[$rownum][2] = $row['FacultyID'];
        $rownum++;                                                                                                                                                       
    }
    
    ?>
    
    <!DOCTYPE html>
    
    <head>
    <link href="styles.css" rel="stylesheet">
    <h1> Miami University </h1>
    <h4> Information Systems and Analytics Department </h4>
    
    
    </head>
    <body>
    
    <div class="StyleDiv" > 
    <!-- coding for journal -->
    <form id="form1" name="form1" method="post" action="RR2.php">
    
    
    <label for="FacultyName">Faculty Name</label>
    <select multiple="multiple" name="FacultyID" id="FacultyID">
    <?php
        for($i = 0; $i < sizeof($faculty); $i++) {
        print "<option value=\"" . $faculty[$i][2] . "\">" . $faculty[$i][0] .' '. $faculty[$i][1] . "</option>\r\n";
        }
    ?>
    </select>
    
    
    <br class="clear" />
    
    <br class="clear" /> 
    <label for="JournalID">Journal Name</label>
    <select name="JournalID" id="JournalID">
    <?php
        for($i = 0; $i < sizeof($journals); $i++) {
        print "<option value=\"" . $journals[$i][1] . "\">" . $journals[$i][0] . "</option>\r\n";
        }
    ?>
    </select>
    
    <br class="clear" />
    
    <label for="JournalRating">Journal Rating</label><input type="text" name="JournalRating" id="JournalRating" />
    <br class="clear" /> 
    
    
    <!-- coding for publication --> 
    <label for="Title">Publication Title</label><input type="text" name="PubID" id="PubID" />
    <br class="clear" /> 
    <label for="Year">Year</label><input type="text" name="Year" id="Year" />
    <br class="clear" /> 
    <label for="Volume">Volume</label><input type="text" name="Volume" id="Volume" />
    <br class="clear" /> 
    <label for="Issue">Issue</label><input type="text" name="Issue" id="Issue" />
    <br class="clear" /> 
    <label for="Comments">Comments</label><textarea name="Comments" id="Comments" cols="45" rows="5"></textarea>
    <br class="clear" /> 
    
    
    
    <input type="submit" name="Submit" id="Submit" value="Submit" />
    <br class="clear" /> 
    </br>
    </br>
    
    </div>
    </form>
    
    <?php
    
    //Post Parameters 
    $JournalID = $_POST['JournalID'];
    //for($i = 0; $i < sizeof($journals); $i++) {
        //if ($JournalID = $journals[$i][1]) {
        //$JournalName = $journals[$i][0];
        //}
        //}
    $Year = $_POST['Year'];  
    $Comments = $_POST['Comments'];  
    $Volume = $_POST['Volume'];  
    $Issue = $_POST['Issue'];  
    $Title = $_POST['Title'];
    $JournalRating = $_POST['JournalRating'];
    $FacultyMemID = $_POST['FacultyID'];
    
    
    //Query 
    
    
    
     //INSERT 
     $stmt = $conn->prepare(" INSERT INTO PUBLICATION ( JournalID, Year, Comments, Volume, Issue, Title, JournalRating )  VALUES ( '$JournalID', '$Year', '$Comments', '$Volume', '$Issue', '$Title', '$JournalRating' )"); 
     $stmt->execute(); 
    
    // would need to add inserts for JournalName if we re-add it in 
    
    
     $stmt = $conn->prepare(" INSERT INTO FACULTYPUBLICATIONS ( FacultyID, PubID )  VALUES ( '$FacultyMemID', last_insert_id() )"); 
     $stmt->execute();
    
    
    mysqli_close($conn);
    ?>
    
    
    
    </body>
    </html>
    

2 个答案:

答案 0 :(得分:0)

要获得多个选择,请将[]添加到输入名称:

<select multiple="multiple" name="FacultyID[]" id="FacultyID">

这告诉PHP使$_POST['FacultyID']成为所有值的数组。然后你可以遍历它们:

$stmt = $conn->prepare(" INSERT INTO PUBLICATION ( JournalID, Year, Comments, Volume, Issue, Title, JournalRating )  VALUES ( ?, ?, ?, ?, ?, ?, ? )"); 
$stmt->bind_param('sssssss', $JournalID, $Year, $Comments, $Volume, $Issue, $Title, $JournalRating);
$stmt->execute(); 

$pubID = $conn->insert_id;

$stmt = $conn->prepare(" INSERT INTO FACULTYPUBLICATIONS ( FacultyID, PubID )  VALUES ( ?, ? )"); 
$stmt->bind_param('si', $FacultyMemID, $pubID);
for ($_POST['FacultyID'] as $FacultyMemID) {
    $stmt->execute();
}

请注意,您不能在循环中使用SQL LAST_INSERT_ID(),因为在第一次迭代之后它将包含刚插入FACULTYPUBLICATIONS的行的ID,而不是在循环之前插入PUBLICATION的行。所以我使用PHP $stmt->insert_id来获取ID。

我还使用bind_param重新编码以防止SQL注入。

答案 1 :(得分:0)

这使它也可以工作,因为从原始函数调用数组。

 $stmt = $conn->prepare(" INSERT INTO FACULTYPUBLICATIONS ( FacultyID, PubID )  VALUES ( ?, ? )"); 
    $stmt->bind_param('ii', $facmemid, $pubID);
     //for ($_POST['FacultyID'] as $FacultyMemID) {
     for($i = 0; $i < sizeof($FacultyMemID); $i++) {
        $facmemid = $FacultyMemID[$i]; 
        $stmt->execute();
        }