I have 2 table: 1 name is movie and another's name is screen. movie table contains fields like movie_id,movie_name etc. screen table contains fields like movie_id screen_id etc. I am going to add data in my screen table by user.

So i want that if user enter the movie_id which is not in movie table then it should prompt the user and display msg as plz enter correct movie_id. my code is as: plz verify this and answer me as soon as possible. plz

<?php require_once '.../dbconfig.php/'; 
//echo hi;
if(isset($_POST['submit']))
{
$sql=mysql_query("INSERT INTO screen(`movie_id`,`screen_id`,`theatre_id`,`capacity`)
VALUES('$_POST[t1]','$_POST[t2]','$_POST[t3]','$_POST[t4]')");


echo $sql." Record added Successfully";
}

?>
<html>
    <form action="" method="post" name="add_screen" id="movie" onSubmit="return validation();">
        <div>
            <fieldset>
    <table width="100%" cellpadding="5" cellspacing="5" align="center">
        <tr>
            <th colspan="2" valign="top" align="left" style="padding-left:263px;"><span style="border-bottom:#000000 2px solid;"></span></th>
        </tr>
        <tr><td>Enter Movie_Id</td><td><input type="text" name="t1" value="" size="20" /><br></td></tr>
        <tr><td>Enter Screen_Id</td><td><input type="text" name="t2" value="" size="20" /></td></tr>
        <tr><td>Enter Theatre_Id</td><td><input type="text" name="t3" value="" size="20" /></td></tr>
        <tr><td>Enter capacity of Theatre</td><td><input type="text" name="t4" value="" size="20" /></td></tr>
        <tr><td>  <center><input type="submit" value="Submit" name="submit" /></center></td></tr>
   </table>
            </fieldset>
    </form>
</html>

If your mysql data table types are InnoDB then I think you should movie_id screen table as foreign key. InnoDB rejects any INSERT or UPDATE operationb attempts to create a foreign key value in child table so user will be denied to inserting wrong movie_id number into screens table. Your script should check what whas MySQL response status and according to this either say that "OK, record was saved", or "please enter correct movie_id"

More info on foreign keys in mysql: http://dev.mysql.com/doc/refman/5.0/en/innodb-foreign-key-constraints.html

More user friendly solution would be to offer movie IDs in a drop down list (html select element). In that case user can not select an invalid ID and they also do not have to remember all IDs (you can display movie names in the select element). Of course if there are too many IDs (say hundreds) this option would not be appropriate.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.