Jump to content

using if/elseif to route error messages


phoebe

Recommended Posts

Hello,

 

I am hoping to get some advice on how to make my code work.  What I would like to do is display a "you are already subscribed" message if the user experiences error 1062 (duplicate entry) and die, or if it's a different error that occurs I would just like it to display the raw error message and die. 

 

I've tried it this and way and that but I'm stumped (obviously I'm also pretty new to this).  Here is my code:

 

<?php

$con = mysql_connect("localhost");

if (!$con)

{

die ('Could not connect: ' . mysql_error());

}

 

mysql_select_db("subscribe", $con);

 

$sql="INSERT INTO subscribe_init (id, notes)

values ('$_POST[id]','$_POST[notes]')";

 

if (!mysql_errno() == 1062)

echo 'You are already subscribed.';

 

elseif (!mysql_query($sql,$con))

die ('Error: ' . mysql_errno() . mysql_error());

 

else echo "Thank you for signing up!";

 

mysql_close($con)

?>

 

Any advice is greatly appreciated.

 

Thank you!

Link to comment
Share on other sites

ur missing {} after ur if elseif else

 

if (!mysql_errno() == 1062){
somethin here
}
elseif (!mysql_query($sql,$con)){
somethin here
}
else {
somethin here
}

 

if (!mysql_errno() == 1062){
echo 'You are already subscribed.';
}
elseif (!mysql_query($sql,$con)){
die ('Error: ' . mysql_errno() . mysql_error());
}
else {
echo "Thank you for signing up!";
}

Link to comment
Share on other sites

Thanks for the help, Hyster, but it doesn't seem to be functioning quite right.

 

I keep getting the "already subscribed" message, regardless of what id I enter. 

 

I tried without the ! in front of the if (!mysql_errno() == 1062) bit because it seemed odd to me but that didn't help either. 

 

Any thoughts?

Link to comment
Share on other sites

You are checking for the error before you execute the query; and then executing the query in the elseif. Try rearranging it a bit:

 

$con = mysql_connect("localhost");
if (!$con)
   {
   die ('Could not connect: ' . mysql_error());
   }
   
mysql_select_db("subscribe", $con);

$sql="INSERT INTO subscribe_init (id, notes)
values ('$_POST[id]','$_POST[notes]')";

if (!mysql_query($sql,$con))
  if (mysql_errno() == 1062)
    echo 'You are already subscribed.';
  else
    die ('Error: ' . mysql_errno() . mysql_error());
else
  else echo "Thank you for signing up!";

mysql_close($con)

 

or something like that.

 

Disclaimer: There are other issues with that code. This answer applies only to the original question.

Link to comment
Share on other sites

Thank you, David, that did it!

 

For future reference, when I post code here, how do I set it apart as code rather than a quote?

 

I made a minor change, and here's what I ended up doing:

 

 

<?php

$con = mysql_connect("localhost");

if (!$con)

{

die ('Could not connect: ' . mysql_error());

}

 

mysql_select_db("subscribe", $con);

 

$sql="INSERT INTO subscribe_init (id, notes)

values ('$_POST[id]','$_POST[notes]')";

 

if (!mysql_query($sql,$con))

if (mysql_errno() == 1062)

die ('It appears that you are already subscribed!');

else

die ('Error: ' . mysql_errno() . mysql_error());

 

echo "Thank you for signing up!";

 

mysql_close($con)

?>

 

Thanks again.

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.