Jump to content

Help displaying a message


07960

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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.

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.