mysql - No confirmation or error message appearing for php foreach loop sql insert -
i have started learning php , doing php mysql database school named "dat501movie". have created input box user select movietitle (with movieid each movietitle stored option value), , multi select box user select multiple items (in case directorid) directorfistname , directorlastname displayed in display box directorid (for each directorfirstname , directorlastname) stored option value.
i want store both movieid , directorid in junction table movie , director tables called director_movietable.
but when select mutliple directorfirstname/directorlastname items , press input button "add" no confirmation or error message, telling me if data has been inserted dat501movie database or not, appears. , when check phpmyadmin cannot see new values in database.
i grateful if please advise me why not recieving message , why data not being inserted database.
below code multiple insert box , code when "add" button 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 database 1"); $query="select movieid, movietitle 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 database 2"); $query="select directorid, directorfirstname, directorlastname 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 $val){ $sql = "insert director_movietable( directorid, movieid) values ('$val','$movietitle')"; mysql_select_db('dat501movie'); $retval = mysql_query( $sql, $conn ); //this runs query if(! $retval ) { die('could not enter data director_movie table: ' . mysql_error()); } echo "entered data successfully\n"; }
Comments
Post a Comment