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

Link to comment
Share on other sites

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/>'; 
          }

 };

?>

Link to comment
Share on other sites

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.

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.