phoebe Posted September 27, 2011 Share Posted September 27, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/247942-using-ifelseif-to-route-error-messages/ Share on other sites More sharing options...
hyster Posted September 27, 2011 Share Posted September 27, 2011 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!"; } Quote Link to comment https://forums.phpfreaks.com/topic/247942-using-ifelseif-to-route-error-messages/#findComment-1273200 Share on other sites More sharing options...
phoebe Posted September 27, 2011 Author Share Posted September 27, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/247942-using-ifelseif-to-route-error-messages/#findComment-1273203 Share on other sites More sharing options...
DavidAM Posted September 27, 2011 Share Posted September 27, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/247942-using-ifelseif-to-route-error-messages/#findComment-1273218 Share on other sites More sharing options...
phoebe Posted September 27, 2011 Author Share Posted September 27, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/247942-using-ifelseif-to-route-error-messages/#findComment-1273259 Share on other sites More sharing options...
Buddski Posted September 27, 2011 Share Posted September 27, 2011 echo 'This is a [code=php:0] tag'; echo 'This is a [code] tag'; These are the 2 main tags used for displaying code/php Quote Link to comment https://forums.phpfreaks.com/topic/247942-using-ifelseif-to-route-error-messages/#findComment-1273270 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.