Jump to content

validate DB entry does not exist


bryanptcs

Recommended Posts

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]
<?php
include('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">&nbsp;</td>
  </tr>
<tr>
  <td align="right">&nbsp;</td>
  </tr>
<tr>
  <td align="right">&nbsp;</td>
  </tr>
<tr><td align="center" colspan="2"><br /><input name="Submit" type="submit" id="Submit" value="Submit" />
</td></tr>
</table>
</form>
[/code]
Link to comment
https://forums.phpfreaks.com/topic/34274-validate-db-entry-does-not-exist/
Share on other sites

[code]<?php
include('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
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]
<?php
include('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]
Please post what it outputs:
[code]<?php
include('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]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.