bryanptcs Posted January 12, 2007 Share Posted January 12, 2007 I need to check to see if the value for table_id already exists in the db. If it does, a message needs to post and allow the user to choose a dif table id.Here is what i have...i get the die message of could not execute query for $sql. Any help?[code]<?phpinclude("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 execute"); $num = mysql_numrows($result); if ($num > 0) { echo "That table is already reserved"; header("Location: reservetable.php"); } 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."); if ($result2 == "yes") { header("Location: index.php"); exit; } }?>[/code] Quote Link to comment Share on other sites More sharing options...
trq Posted January 12, 2007 Share Posted January 12, 2007 Table is a reserved word in SQL. You will need to surround it in backticks or change the filed name.[code=php:0]$sql = "SELECT table_id FROM `Table`";[/code] Quote Link to comment Share on other sites More sharing options...
ShogunWarrior Posted January 12, 2007 Share Posted January 12, 2007 And table_id needs to be $table_id Quote Link to comment Share on other sites More sharing options...
bryanptcs Posted January 12, 2007 Author Share Posted January 12, 2007 Ok, I have made the changes...but it does not seem like the program is running. It just puts the message that the table already exists, even when it doesn't exist yet...here is my updated code[code]<?php ob_start();?><?phpinclude("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 execute"); $num = mysql_numrows($result); if ($num > 0) { $new_message = "That table is already reserved"; //header("Location: reservetable.php"); include("reservetable.php"); 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."); if ($result2 == "yes") { header("Location: index.php"); exit; } }?>[/code] Quote Link to comment Share on other sites More sharing options...
trq Posted January 12, 2007 Share Posted January 12, 2007 I think you missed ShowgunWarrior's post.[code=php:0]$sql = "SELECT $table_id FROM `Table`";[/code] Quote Link to comment Share on other sites More sharing options...
bryanptcs Posted January 12, 2007 Author Share Posted January 12, 2007 putting the $ in front of table_id errors the sql command and causes it not to execute... Quote Link to comment Share on other sites More sharing options...
trq Posted January 12, 2007 Share Posted January 12, 2007 Your entire section of code should be within an if().[code]<?php if ($_POST['submit']) { // your code }?>[/code] Quote Link to comment Share on other sites More sharing options...
bryanptcs Posted January 12, 2007 Author Share Posted January 12, 2007 i did that (updated code below) and it just produced a blank page.[code]<?php ob_start();?><?phpif ($_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 execute"); $num = mysql_numrows($result); if ($num > 0) { $new_message = "That table is already reserved"; //header("Location: reservetable.php"); include("reservetable.php"); 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."); if ($result2 == "yes") { header("Location: index.php"); exit; } }}?>[/code] Quote Link to comment Share on other sites More sharing options...
trq Posted January 12, 2007 Share Posted January 12, 2007 Are you submitting a from to this script?(By the way, your indentation is terrible). Quote Link to comment Share on other sites More sharing options...
bryanptcs Posted January 12, 2007 Author Share Posted January 12, 2007 yes...[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]Sorry about the indention...I am usually pretty good about it. Quote Link to comment Share on other sites More sharing options...
trq Posted January 12, 2007 Share Posted January 12, 2007 Your using a capitol S in Submit.[code=php:0]if ($_POST['Submit'])[/code] Quote Link to comment 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.