PHP Isset & MySQL If Statement

Associate
Joined
24 Sep 2005
Posts
209
Would anyone be able to tell me where I could put an IF statement into the following code, to prevent it from running when $Status is not equal to "Working"?

Code:
if (isset($_POST['FaultType'])):
  // The author's details have been updated.

  $id = $_POST['id'];
  
  $sql = "INSERT INTO Faults SET ComputerID='$ComputerID', FaultStatus='Attention', FaultType='$FaultType', FaultDescription='$FaultDescription', StaffID='$StaffID', TechID='Unknown'";  // Add details of fault to table Faults
		
$sql2 = "UPDATE Workstation SET Status='Attention' WHERE ComputerID='$id'"; // Change Computer Status to Attention
		 

  if (@mysql_query($sql) AND @mysql_query($sql2)) {
    echo '<p>Fault logged - thank you. </p>';
  } else {
    echo '<p>Error logging fault. Fault not logged successfully. ' .
        mysql_error() . '</p>';
  }

I guess the main problem here is that Status is set to "Attention" in the sql2 query - so I need to be able to test the content of $Status prior to the SQL queries.

Thanks for any pointers,

Steven
 
You would need to put an if around the whole thing, however its difficult to say with out knowing where 'working' is set however on the assumption $_POST['FaultType'] is what would contain 'working' then do this...

PHP:
if ($_POST['FaultType'] == "working") {

// CODE

}

Also I noticed your not checking your variables for illegals, so you need to put mysql_real_escape_string() around any thing being passed into a SQL statment... strip_slashes() around any thing being passed in with the exception of a mysql statment, and html_entities() around any thing with is being outputted to the user's screen which you dont want to contain html, because people can put in nasty code and stuff into the page like that.

PHP:
if (isset($_POST['FaultType'])):
  // The author's details have been updated.

  $id = mysql_real_escape_string($_POST['id']);
  
  $sql = "INSERT INTO Faults SET ComputerID='$ComputerID', FaultStatus='Attention', FaultType='$FaultType', FaultDescription='$FaultDescription', StaffID='$StaffID', TechID='Unknown'";  // Add details of fault to table Faults
		
$sql2 = "UPDATE Workstation SET Status='Attention' WHERE ComputerID='$id'"; // Change Computer Status to Attention
		 

  if (@mysql_query($sql) AND @mysql_query($sql2)) {
    echo '<p>Fault logged - thank you. </p>';
  } else {
    echo '<p>Error logging fault. Fault not logged successfully. ' .
        mysql_error() . '</p>';
  }

Please correct me if im wrong above
 
Back
Top Bottom