07960 Posted August 26, 2012 Share Posted August 26, 2012 Hi I have wrote some code which when executed, it updates something in a database. When it has done this, it displays the message "database successfully updated". What I am having an issue with, is though it does update the db fine, if I enter an invalid case id number, instead of saying "invalid id" it displays the same "database successfully updated" message. Can anyone tell me the correct format to use to display the message ? The code I am using is : if($idhtml) { $sql= "UPDATE problem SET resolved = 'resolved' where id=$idhtml"; $rs = mysql_query($sql,$conn) or die ("Could not execute SQL query"); } //if the above is succesful then display message if ($rs) {$msg = "Database Successfully updated!!!"; echo $msg;} mysql_close($conn); ?> The update works fine on the correct case ref, just want it to display an error message if an invalid case id is typed in.. Quote Link to comment https://forums.phpfreaks.com/topic/267593-help-displaying-a-message/ Share on other sites More sharing options...
Christian F. Posted August 26, 2012 Share Posted August 26, 2012 Before trying to update the database, you'll need to check if the ID exists in the database. You can also check how many rows were affected by the query, and if 0 then you know it was (probably) an invalid ID. Though, IIRC you're going to get 0 rows affected if it was a valid ID marked "resolved" already. Quote Link to comment https://forums.phpfreaks.com/topic/267593-help-displaying-a-message/#findComment-1372561 Share on other sites More sharing options...
tibberous Posted August 27, 2012 Share Posted August 27, 2012 Instead of: $msg = "Database Successfully updated!!!"; echo $msg; Put: echo mysql_affected_rows() ? "Database Successfully updated!!!" : "Error updating database"; Btw, your doing some really weird stuff. Like: $rs = mysql_query($sql,$conn) can just be $rs = mysql_query($sql); unless your actually connecting to more than one database Saving the result in $rs is weird, since your doing an update, not a query Saying "if($rs)" is weird. Why? Because $rs will be false if there is an sql error (technically this never occurs because you die() if there is one), but also if no variable was sent. mysql_close is unnecessary Quote Link to comment https://forums.phpfreaks.com/topic/267593-help-displaying-a-message/#findComment-1372784 Share on other sites More sharing options...
Christian F. Posted August 27, 2012 Share Posted August 27, 2012 I don't agree with using the ternary constructor to echo out a proper string, much better to use a proper IF-statement and then save the string into a variable. Gives a much clearer code, which will make it easier to read and maintain. Secondly, using the SQL connection variable can be considered "safe practice", though (as you said) it is generally pointless to use with the mysql_* () functions for most. However, that being said the mysql_* () functions are deprecated, which means that the mysqli_* () functions should be used instead. They require the connection data to be present, unless you're using the OOP notation. Quote Link to comment https://forums.phpfreaks.com/topic/267593-help-displaying-a-message/#findComment-1372805 Share on other sites More sharing options...
trq Posted August 27, 2012 Share Posted August 27, 2012 The mysql_* functions are not deprecated, there use has been discouraged. I very much doubt they are going anywhere soon however. Quote Link to comment https://forums.phpfreaks.com/topic/267593-help-displaying-a-message/#findComment-1372852 Share on other sites More sharing options...
Christian F. Posted August 27, 2012 Share Posted August 27, 2012 Hmm... I could have swore I read they were deprecated. Oh, well. The recommendation to use MySQLi still stands. Quote Link to comment https://forums.phpfreaks.com/topic/267593-help-displaying-a-message/#findComment-1372856 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.