Jump to content

Echo only if sql query is successful!


wright67uk

Recommended Posts

I have my database set-up to only accept unique competition names.

 

How can I change my code below to display a success message only if a entry is added to my database.

 

Everything appears fine, apart from my last IF statement.

 

I've been playing around with this code for ages, and I don't appear to be getting anywhere with it!

 

<?php


if(isset($_POST['processForm'])) 
{
 $compname = $_POST['compname']; 
 $compname = trim($compname);
 $compname = htmlentities($compname); 

               if (empty($compname)) {echo "You haven't entered a competition name<br/>"; 
                                                     }  

   else if (isset($compname)) {$sql = "INSERT INTO Competitions (Competition) VALUES ('$compname')";
                               mysql_query($sql);
                                                 }

               if ((!mysql_query($sql)) && (!empty($compname)) ) {echo "This Name is already taken";
                                                                                                 }			   

   if (mysql_query($sql) === TRUE) {echo '<p>Your New Competition is now up and running <a href="comptable.php"><img src="refresh.png"/><a/></p><br/>'; 
                                                           }

 };

?>

Link to comment
https://forums.phpfreaks.com/topic/273665-echo-only-if-sql-query-is-successful/
Share on other sites

Do not keep calling mysql_query($sql) as you execute the query every time you do. Assign it to a $result variable when you first call it and check that value for false to see if it failed.

 

Secondly, if it did not fail, it will contain a result resource, not TRUE so your test for "===TRUE" will always fail.

Thank you Barand,

 

I'm now using a $result and everything is now working.

 

<?php


if(isset($_POST['processForm'])) 
{
 $compname = $_POST['compname']; 
 $compname = trim($compname);
 $compname = htmlentities($compname); 

               if (empty($compname)) {echo "You haven't entered a competition name<br/>"; 
                         }  

   else if (isset($compname)) {$sql = "INSERT INTO Competitions (Competition) VALUES ('$compname')";
                               $result = mysql_query($sql);
                       }

               if ((!$result) && (!empty($compname))) {echo "This Name is already taken";
                                                      }			   

   if ($result === TRUE) {echo '<p>Your New Competition is now up and running <a href="comptable.php"><img src="refresh.png"/><a/></p><br/>'; 
          }

 };

?>

An INSERT query that fails due to an error, doesn't necessarily mean that the data you tried to insert already exists. There are other things that can cause a query to fail, especially since you are not escaping the string data being put into the query and the other functions you are using on the data won't prevent single-quotes from causing the query to fail with an error.

 

Likewise, an INSERT query that runs without producing an error, doesn't necessarily mean that the row was inserted correctly, such as string data that was longer then the field size and was truncated.

 

For an INSERT query, you need test if the query executed without any errors, then use mysql_affected_rows to test if the row was actually inserted.

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.