如何使用php发送id到数据库进行动态下拉

时间:2015-04-02 20:15:18

标签: php jquery html mysql forms

我有一个名为FACULTY的表格,其中包含FacultyIDFNameLName。它预先填充了信息。我可以使用动态下拉菜单来显示页面上显示的值。

我的问题是我还有一个与每个教师相关的出版物表。我希望能够选择多个教师并将这些ID(在各行中)发送到教师出版物表。我希望输入的发布信息进入发布表,然后从中自动生成的id也进入教师发布表。

表名:FACULTYPUBLICATIONFACULTYPUBLICATIONS

到目前为止,这是我的代码:

    
<?php
    include_once 'dbc.php';
    function connect() {
        mysql_connect(DB_HOST, DB_USER, DB_PASS) or die('Could not connect to database'.mysql_error());
        mysql_select_db(DB_NAME);
    }

    function close() {
        mysql_close();
    }

    function query() {
        $myData = mysql_query("SELECT * FROM FACULTY");
        while($record = mysql_fetch_array($myData)) {
            echo '<option value="'. $record['FName'] .$record['LName']. '">' . $record['FName'] .' '. $record['LName'] . ' </option>';
        }
    }
    
<?php
    include_once 'pullDataR2.php';
    connect();
?>
<!DOCTYPE html>
    <head>
        <link href="styles.css" rel="stylesheet">
        <h1> help </h1>
    </head>
    <body>

        <div class="StyleDiv" > 
            <!-- coding for journal -->
            <form id="form1" name="form1" method="post" action="RR2.php">
                <label for="FName">Faculty Name</label>
                <select multiple="multiple" name="select" id="Faculty">
                    <?php query() ?>
                </select>
                <?php close() ?>
                <br class="clear" />

                <input type="hidden" name="JournalID" id="JournalID" class="textbox" />
                <br class="clear" /> 
                <label for="JournalName">Journal Name</label>
                <input type="text" name="JournalName" id="JournalName" />
                <br class="clear" /> 
                <label for="Rating">Journal Rating</label>
                <select name="Rating" id="Rating">
                    <option value="A+">A+</option>
                    <option value="A">A</option>
                    <option value="A-">A-</option>
                    <option value="B+">B+</option>
                    <option value="B">B</option>
                    <option value="B-">B-</option>
                    <option value="C+">C+</option>
                    <option value="C">C</option>
                    <option value="C-">C-</option>
                    <option value="D+">D+</option>
                    <option value="D">D</option>
                    <option value="D-">D-</option>
                    <option value="F">F</option>
                </select>
               <br class="clear" />

               <!-- coding for publication --> 
               <input type="hidden" name="PubID" id="PubID" />
               <br class="clear" /> 
               <label for="Title">Publication Title</label>
               <input type="text" name="Title" id="Title" />
               <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 
    $JournalName = $_POST['JournalName'];  
    $Rating = $_POST['Rating'];

    $Year = $_POST['Year'];  
    $Comments = $_POST['Comments'];  
    $Volume = $_POST['Volume'];  
    $Issue = $_POST['Issue'];  
    $Title = $_POST['Title'];

    //create connection
    $conn = new mysqli('localhost','root','isasurvey','isasurvey');
    if($conn->connect_errno) {
        echo 'failure</br>';
    }

    //Query 

     //INSERT 
     $stmt = $conn->prepare (" INSERT INTO JOURNAL ( JournalName, Rating )  VALUES ( '$JournalName', '$Rating' )"); 
     $stmt->bind_param("sssssss", $JournalName, $Rating);
     $stmt->execute();

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

?>

    </body>
</html>

1 个答案:

答案 0 :(得分:0)

此编码有效,因为您添加for循环,然后继续直到发布。

<?php
    include 'dbc.php';
    connect();

$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";                                                                                                                                                       
} 

$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++;                                                                                                                                                       
}

?>

<head>
<link href="styles.css" rel="stylesheet">
<h1> Miami University </h1>
<h4> Information Systems and Analytics Department </h4>
</head>
<body>


<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" /> 

$JournalID = $_POST['JournalID'];
$Year = $_POST['Year'];  
$Comments = $_POST['Comments'];  
$Volume = $_POST['Volume'];  
$Issue = $_POST['Issue'];  
$Title = $_POST['Title'];
$JournalRating = $_POST['JournalRating'];
$FacultyMemID = $_POST['FacultyID'];

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