I have a problem, if someone can help great!!

Every time the form is submitted to delete the stabletitle it says successful even when the title entered is not in the database,

if its not in the database it should say unsucessful, can someone see what is wrong with my code?

Any help is great!

<?php

session_start();

include('dbconnect.php');

$stabletitle = $_POST["stabletitle"];

$SQLcmd = "DELETE FROM stables WHERE stabletitle ='$stabletitle'";


if (!$_POST['stabletitle'])
{ ?>

<center> <h3> <font color=#C11B17> You have not entered any information!! Please try again.... </font> </h3> </center> 
<?php } 

else if (mysql_query($SQLcmd, $con)){
		print ("**The information has been successfuly deleted** <br>");

$result = mysql_query("Select * From stables ORDER BY displayorder, stabletitle"); ?>

	 <br> </br> <center> <h3> Below is the remaining information which can be displayed within the stables page: </h3> <br> </br> </center>

<?php
	  print ' <table class="border"><th> Content Title <th> Content <th> Display Order </th>';
	  
	  while ($row = mysql_fetch_array($result))
	  {
	   print
	   "<tr><td>".$row['stabletitle']."</td><td>".$row['stablecontent']."</td><td>".$row['displayorder']."</td></tr>";
   }

   
  print "</table>";

}
else{ ?>

<font color=#C11B17> <p> You were unsuccessfull in deleting the information you requested! Please try again... </p> </font>
<?php	
}

mysql_close($con);

?>

Recommended Answers

All 4 Replies

Member Avatar for diafol

Test for a post var thus:

if(isset($_POST['stabletitle']){
...
}else{
...
}

Test for successful deletion thus:

//do delete here
if(mysql_affected_rows() > 0){ //this tests for an affected row e.g. delete or update
  echo "Deleted successfully";
}else{
  echo "Unsuccessful deletion";
}

Some weird code here:

$stabletitle = $_POST["stabletitle"];
$SQLcmd = "DELETE FROM stables WHERE stabletitle ='$stabletitle'";
if (!$_POST['stabletitle'])
{ ?>
<center> <h3> <font color=#C11B17> You have not entered any information!! Please try again.... </font> </h3> </center>
<?php }
else if (mysql_query($SQLcmd, $con)){
print ("**The information has been successfuly deleted** <br>");

So you start to delete even if $stabletitle is empty. A better way to do it is this:

$stabletitle = isset($_POST['stabletitle']) ? $_POST['stabletitle'] : null;
if ($stabletitle == null) {
   echo "<center><h3><font color=#C11B17>You have not entered any information!! 
        Please try again... </font><h3></center>";
} 
else {
   // we have something to delete
   $sqlcmd = "DELETE FROM stables WHERE stabletitle = '$stabletitle'";
   mysql_query($SQLcmd, $con);
   // but did we delete anything....
   if (mysql_affected_rows() == 0) {
      echo "<p>This information was not found in the database, nothing deleted</p>";
   }
   else {
      echo "<p>The information has been successfully deleted</p>";
   }
}

Thanks for the help! got it working

Member Avatar for diafol

OK - mark as solved

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.