No confirmation or error message appearing for php foreach loop sql insert

时间:2015-10-30 22:54:23

标签: php mysql

I have just started learning php and I am doing a php mysql database for school which is named "dat501movie". I have created input box for user to select a MovieTitle (with the MovieID for each MovieTitle stored as option value), and then a multi select box for the user to select multiple items (in this case DirectorID) with the DirectorFistName and DirectorLastName displayed in the display box but the DirectorID (for each DirectorFirstName and DirectorLastName) stored as the option value.

I want to store both the MovieID and DirectorID in a junction table for the Movie and Director tables called director_movietable.

But when I select the mutliple DirectorFirstName/DirectorLastName items and press the input button "add" no confirmation or error message, telling me if the data has been inserted into dat501movie database or not, appears. And when I check my phpmyadmin I cannot see the new values in this database.

I would be very grateful if someone please advise me as to why I am not recieving a message and why my data is not being inserted into the database.

Below is the code for the multiple insert box and the code for when "add" button is selected.

<form method="post" action="<?php $_PHP_SELF ?>">
<table width="400" border="0" cellspacing="1" cellpadding="2">



<tr>
<td>MovieTitle List </td>
<td> <Select name="Movie">
<option value=""></option>


 <?php
 mysql_select_db($dbname, $conn) or die ("Could not connect to database 1");

 $query="SELECT MovieID, MovieTitle from movietable";
 $result=mysql_query($query) or die ("could not insert data: ".mysql_error());

while ($row=mysql_fetch_array($result)){
$MovieTitle=$row["MovieTitle"];
$MovieID=$row["MovieID"];
echo "<option value=\"$MovieID\">
$MovieTitle
</option>";
}
?>
 </Select>
</td>
</tr>   


<tr>
<td>Director type List </td>
<td> <Select size="10" name="Director[]" multiple="multiple">
<option value=""></option>

<?php
mysql_select_db($dbname, $conn) or die ("Could not connect to    database     2");

$query="SELECT DirectorID,  DirectorFirstName, DirectorLastName from     directortable";
$result=mysql_query($query) or die ("could not insert data:   ".mysql_error());

while ($row=mysql_fetch_array($result)){
$DirectorFirstName=$row["DirectorFirstName"];
$DirectorLastName=$row["DirectorLastName"];
$DirectorID=$row["DirectorID"];
echo "<option value=\"$DirectorID\">
$DirectorFirstName
$DirectorLastName
</option>";
}

?>       

</Select>
</td>
</tr>  
               </table>  

              <input name="add" type="submit" id="add" value="Add Entry">
           </form>
           <?php
     if(isset($_POST['add']))
     {

        if(! get_magic_quotes_gpc() )
        {
           $MovieTitle = addslashes ($_POST['Movie']);
           $GenreName = addslashes ($_POST['Genre']);
           $ActorName = addslashes ($_POST['Actor']);
           $DirectorName = addslashes ($_POST['Director']);

        }
        else
        {
            $MovieTitle = $_POST['Movie'];
           $GenreName = $_POST['Genre'];
           $ActorName = $_POST['Actor'];
           $DirectorName = $_POST['Director'];

        }


 foreach ($DirectorName as $val){
 $sql = "INSERT INTO director_movietable( DirectorID, MovieID) Values      ('$val','$MovieTitle')";

        mysql_select_db('dat501movie');
        $retval = mysql_query( $sql, $conn ); //This actually runs the query

        if(! $retval )
        {
           die('Could not enter data into director_movie table: ' . mysql_error());
        }
        echo "Entered data successfully\n";
           }

0 个答案:

没有答案