Checking form data against SQL data

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
SonoguN
Forum Newbie
Posts: 1
Joined: Wed Oct 27, 2010 9:55 am

Checking form data against SQL data

Post by SonoguN »

Hi

I am really new at PHP. I have created a form that writes data to a database incuding integet, string and date types.
I need to write a conditional if statement that checks the data posted by the form against the data in the database.
Pls help!

This is what I have so far:

Code: Select all

<?php

$con = mysql_connect("localhost","root","root");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
  
  mysql_select_db("studio_db", $con);
  
  $teststudio = mysql_query("SELECT Room FROM Bookings");
  $testslot = mysql_query("SELECT Slot FROM Bookings");
  $testdate = mysql_query("SELECT Date FROM Bookings");
  
  $frmstudio = '$_POST[studio]';
  $frmslot = '$_POST[slot]';
  $frmdate = '$_POST[orderdate]';
  
  if ($teststudio==$frmstudio && $testslot==$frmslot && $testdate==$frmdate)
  {
	  echo "this slot is booked!";
  }
  
  else
  
  {


		$sql="INSERT INTO Bookings (Room, Slot, Booked, Band, Date)
		VALUES ('$_POST[studio]', '$_POST[slot]', '1', '$_POST[band]', '$_POST[orderdate]')";
		
		if (!mysql_query($sql,$con))
		  {
		  die('Error: ' . mysql_error());
		  }
		echo "1 record added";
		
		mysql_close($con)

  }
?>
Last edited by Eran on Wed Oct 27, 2010 10:07 am, edited 1 time in total.
Reason: Added [syntax=php][/syntax] tags. Please use those in the future
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Checking form data against SQL data

Post by Jonah Bron »

I see your psudo-if there... is there only one row in tables Bookings? If not, you need a WHERE clause in there.

I cleaned up your code a bit.

Code: Select all

<?php

$con = mysql_connect("localhost", "root", "root");
if (!$con) {
    die('Could not connect: ' . mysql_error());
}

mysql_select_db("studio_db", $con);

// These queries need to be narrowed down to one row
$test = mysql_query("SELECT Room, Slot, Date FROM Bookings");
$test = mysql_fetch_assoc($teststudio);

$frmstudio = $_POST['studio'];
$frmslot = $_POST['slot'];
$frmdate = $_POST['orderdate'];
$frmband = $_POST['band'];
$frmdate = $_POST['orderdate'];

if ($test['Room'] == $frmstudio && $test['Slot'] == $frmslot && $test['Date'] == $frmdate) {
    echo "this slot is booked!";
} else {
    $sql = "INSERT INTO Bookings (Room, Slot, Booked, Band, Date)
        VALUES ('" . $frmstudio . "', '" . $frmslot . "', '1', '" . $frmband . "', '" . $frmdate . "')";
    if (!mysql_query($sql, $con)) {
        die('Error: ' . mysql_error());
    }
    echo "1 record added";
    mysql_close($con);
}
?>
In future, format/indent your code as I have here.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Checking form data against SQL data

Post by Jonah Bron »

Oh, looking at the code, it looks like you're trying to find out if there's a place open at the specified parameters (slot, room, date)? Is that the case? If it is, that if needs to go into the SQL statement.
Post Reply