0

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";
           }
  • The first step would be to echo out the `$sql` you're generating. I'm guessing that it's not correct - it looks like you're adding to a `DirectorID` column which is probably going to be an integer, but the value you're using is coming from `$_POST['Director']`, which I assume is a text field. – andrewsi Oct 30 '15 at 23:01
  • @andrewsi It looks like a numeric field to me. The options in his ` – Barmar Oct 30 '15 at 23:05
  • If you're not getting the confirmation or error message, the script is probably getting a syntax error. Check the PHP error log on the server. – Barmar Oct 30 '15 at 23:07
  • my are you doing a db use on every iteration of a tight loop ? – Drew Oct 30 '15 at 23:10
  • sql injection, 2nd order, http://stackoverflow.com/a/860963/1816093 – Drew Oct 30 '15 at 23:14
  • The value I am posting in the $_POST['Director'] is the DirectorID which I am then setting to equal $DirectorName. – Alex Buckley Oct 30 '15 at 23:52
  • I am using a MAMP local server to store this database and php files on as I cannot find reference on the internet to where you find the PHP error log so where do you find the error logs on MAMP server? – Alex Buckley Oct 30 '15 at 23:56

0 Answers0