Jump to content

check to see if db entry exists


bryanptcs

Recommended Posts

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]
<?php
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)
{
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]
Link to comment
Share on other sites

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();
?>
<?php
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]
Link to comment
Share on other sites

i did that (updated code below) and it just produced a blank page.

[code]
<?php ob_start();
?>
<?php
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 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]
Link to comment
Share on other sites

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">&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]

Sorry about the indention...I am usually pretty good about it.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.