bryanptcs Posted January 15, 2007 Share Posted January 15, 2007 Ok, I have a form that I am having submitted to the db. The only catch is that I need it to validate the field table_id does not have the same value twice. I am using it as sort of a table reservation form where there are 50 tables to choose from, and each table can only be used once. Here is my below code. Right now, it is running the program and not doing anything...not posting to the db and not creating any errors...it just brings the form back up. Here is a link to see it actually working (or not working): http://www.giftfromgodcomputerfoundation.org/staff/reservetable.php[code]<?phpinclude('incl_db.php');include('function.inc'); $connection = mysql_connect($host, $user, $password) or die ("Couldn't connect to the server."); $db = mysql_select_db($database, $connection) or die ("Couldn't select database."); ?><?php include("reservetable_form.inc"); if ($_POST['Submit']){include("incl_db.php");$connection = mysql_connect($host, $user, $password) or die ("Couldn't connect to the server."); $db = mysql_select_db($database, $connection) or die ("Couldn't select database."); $table_id = Trim(stripslashes($_POST['table_id'])); $company = Trim(stripslashes($_POST['company'])); $amt_tix = Trim(stripslashes($_POST['amt_tickets'])); $comments = Trim(stripslashes($_POST['comments'])); $sql = "SELECT table_id FROM `Table`"; $result = mysql_query($sql) or die("could not execute1"); $num = mysql_numrows($result); if ($num > 0) { print "That table has already been reserved.)"; include ("reservetable_form.inc"); exit; } else { $sql2 = "INSERT INTO `Table` ( `table_id` ,`company` , `table_amt` , `comments` ) VALUES ('$table_id' , '$company', '$amt_tix', '$comments')"; $result2 = mysql_query($sql2) or die("Couldn't execute query."); header("Location: index.php"); } //}} ?>[/code]here is the form:[code]<form action="reservetable_action.php" method="POST"><table border="0"> <tr> <td align="right"><b>Corporation</b></td> <td><input name="company" type="text" id="company" size="30" maxsize="50" /></td></tr> <tr> <td width="120" align="right"><b>Amount of Tickets </b></td> <td><select name="amt_tickets" id="amt_tickets"> <option value="2">2</option> <option value="4">4</option> <option value="6">6</option> </select></td> </tr> <tr> <td align="right">Table Required </td> <td><select name="table_id"> <?php $tableName=getTableName(); $tableCode=getTableCode(); for ($n=1; $n<=54; $n++) { $table=$tableName[$n]; $tcode=$tableCode[$n]; echo "<option value='$tcode'"; if ($scode== "A1") echo " selected"; echo ">$table\n"; } ?></select> <?php echo $new_message; ?> </td> </tr> <tr> <td align="right"><strong>Comments</strong></td> <td rowspan="4" valign="top"><label> <textarea name="comments" cols="30" rows="6" id="comments"></textarea> </label></td> </tr> <tr> <td align="right"> </td> </tr> <tr> <td align="right"> </td> </tr> <tr> <td align="right"> </td> </tr> <tr><td align="center" colspan="2"><br /><input name="Submit" type="submit" id="Submit" value="Submit" /> </td></tr> </table> </form>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/34274-validate-db-entry-does-not-exist/ Share on other sites More sharing options...
Philip Posted January 15, 2007 Share Posted January 15, 2007 [code]<?phpinclude('incl_db.php');include('function.inc');if ($_POST['Submit']) { $connection = mysql_connect($host, $user, $password) or die ("Couldn't connect to the server."); // Connect $db = mysql_select_db($database, $connection) or die ("Couldn't select database."); $table_id = Trim(stripslashes($_POST['table_id'])); //Set variables $company = Trim(stripslashes($_POST['company'])); $amt_tix = Trim(stripslashes($_POST['amt_tickets'])); $comments = Trim(stripslashes($_POST['comments'])); $sql = "SELECT * FROM `Table` WHERE `table_id`='".$table_id."'"; // SQL statement - select all from table where the table id matchs the inputted one $result = mysql_query($sql) or die("could not execute! ".mysql_error()); $num = mysql_num_rows($result); // If any matches... if($num > 0) { print "That table has already been reserved.)"; // tell the user that is has been taken include ("reservetable_form.inc"); //show form exit; // exit script } else { $sql2 = "INSERT INTO `Table` ( `table_id` ,`company` , `table_amt` , `comments` ) VALUES ('".$table_id."' , '".$company."', '".$amt_tix."', '".$comments."')"; //SQL statement for inserting into the DB $result2 = mysql_query($sql2) or die("Couldn't execute query. ".mysql_error()); header("Location: index.php"); // Go back to index }} ?>[/code]I would recommend having something to tell the user that they successfully reserved that table, instead of just redirecting them instantly Quote Link to comment https://forums.phpfreaks.com/topic/34274-validate-db-entry-does-not-exist/#findComment-161197 Share on other sites More sharing options...
bryanptcs Posted January 15, 2007 Author Share Posted January 15, 2007 ok, here is my updated code, but still nothing is happening...no errors, no post to the db...it just refreshes the page basically.[code]<?phpinclude('incl_db.php');include('function.inc');if ($_POST['Submit']) { $connection = mysql_connect($host, $user, $password) or die ("Couldn't connect to the server."); // Connect $db = mysql_select_db($database, $connection) or die ("Couldn't select database."); $table_id = Trim(stripslashes($_POST['table_id'])); //Set variables $company = Trim(stripslashes($_POST['company'])); $amt_tix = Trim(stripslashes($_POST['amt_tickets'])); $comments = Trim(stripslashes($_POST['comments'])); $sql = "SELECT * FROM `Table` WHERE `table_id`='".$table_id."'"; // SQL statement - select all from table where the table id matchs the inputted one $result = mysql_query($sql) or die("could not execute! ".mysql_error()); $num = mysql_num_rows($result); // If any matches... if($num > 0) { print "That table has already been reserved.)"; // tell the user that is has been taken include ("reservetable_form.inc"); //show form exit; // exit script } else { $sql2 = "INSERT INTO `Table` ( `table_id` ,`company` , `table_amt` , `comments` ) VALUES ('".$table_id."' , '".$company."', '".$amt_tix."', '".$comments."')"; //SQL statement for inserting into the DB $result2 = mysql_query($sql2) or die("Couldn't execute query. ".mysql_error()); header("Location: index.php"); // Go back to index }}else{include('reservetable_form.inc');//show form if it has not been submitted} ?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/34274-validate-db-entry-does-not-exist/#findComment-161302 Share on other sites More sharing options...
Philip Posted January 15, 2007 Share Posted January 15, 2007 Please post what it outputs:[code]<?phpinclude('incl_db.php');include('function.inc');if ($_POST['Submit']) { echo "Submit - OK<Br />"; //delete after debugging $connection = mysql_connect($host, $user, $password) or die ("Couldn't connect to the server."); // Connect $db = mysql_select_db($database, $connection) or die ("Couldn't select database."); $table_id = Trim(stripslashes($_POST['table_id'])); //Set variables $company = Trim(stripslashes($_POST['company'])); $amt_tix = Trim(stripslashes($_POST['amt_tickets'])); $comments = Trim(stripslashes($_POST['comments'])); $sql = "SELECT * FROM `Table` WHERE `table_id`='".$table_id."'"; // SQL statement - select all from table where the table id matchs the inputted one $result = mysql_query($sql) or die("could not execute! ".mysql_error()); $num = mysql_num_rows($result); // If any matches... if($num > 0) { echo "That table has already been reserved.)"; // tell the user that is has been taken include ("reservetable_form.inc"); //show form exit; // exit script } else { $sql2 = "INSERT INTO `Table` ( `table_id` ,`company` , `table_amt` , `comments` ) VALUES ('".$table_id."' , '".$company."', '".$amt_tix."', '".$comments."')"; //SQL statement for inserting into the DB echo $sql2." SQL - OK<br />"; // delete after done debugging $result2 = mysql_query($sql2) or die("Couldn't execute query. ".mysql_error()); //header("Location: index.php"); // Go back to index -- uncomment when done debugging }}else{include('reservetable_form.inc');//show form if it has not been submitted} ?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/34274-validate-db-entry-does-not-exist/#findComment-161306 Share on other sites More sharing options...
bryanptcs Posted January 15, 2007 Author Share Posted January 15, 2007 I found out the problem...when I changed the form action to call the same page...I forgot to change it on the .inc file...Your code def. did the trick though...Thanks a lot... Quote Link to comment https://forums.phpfreaks.com/topic/34274-validate-db-entry-does-not-exist/#findComment-161314 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.