wright67uk Posted January 26, 2013 Share Posted January 26, 2013 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/>'; } }; ?> Quote Link to comment https://forums.phpfreaks.com/topic/273665-echo-only-if-sql-query-is-successful/ Share on other sites More sharing options...
Barand Posted January 26, 2013 Share Posted January 26, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/273665-echo-only-if-sql-query-is-successful/#findComment-1408361 Share on other sites More sharing options...
wright67uk Posted January 26, 2013 Author Share Posted January 26, 2013 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/>'; } }; ?> Quote Link to comment https://forums.phpfreaks.com/topic/273665-echo-only-if-sql-query-is-successful/#findComment-1408366 Share on other sites More sharing options...
PFMaBiSmAd Posted January 26, 2013 Share Posted January 26, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/273665-echo-only-if-sql-query-is-successful/#findComment-1408376 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.