isaac_cm Posted October 22, 2006 Share Posted October 22, 2006 Hi everybody,I Have a unique field key in my table and I want if the visitor enter the same data in that field to not see the mysql error about unique field , is there a way to direct the visitor to another page thanks Quote Link to comment Share on other sites More sharing options...
obsidian Posted October 22, 2006 Share Posted October 22, 2006 you need to check their data ahead of time and redirect them accordingly. for instance:[code]<?phpif (isset($_POST['submit'])) { $name = mysql_real_escape_string(trim($_POST['name'])); $sql = mysql_query("SELECT * FROM myTable WHERE name = '$name'"); if (mysql_num_rows($sql) > 0) { // that name has already been entered, redirect them or show an error } else { // that name is unique. go ahead and insert it. }}?>[/code]good luck! Quote Link to comment Share on other sites More sharing options...
fenway Posted October 22, 2006 Share Posted October 22, 2006 Alternatively, you can use INSERT IGNORE, depending on what you're actually doing. Quote Link to comment Share on other sites More sharing options...
isaac_cm Posted October 22, 2006 Author Share Posted October 22, 2006 thanks alot, I know I can check first with "select" but I would prefer "insert ignore" it works prefectly with me, I want to know also if there is any other way many thanks guys Quote Link to comment Share on other sites More sharing options...
obsidian Posted October 23, 2006 Share Posted October 23, 2006 [quote author=isaac_cm link=topic=112352.msg456028#msg456028 date=1161560772]thanks alot, I know I can check first with "select" but I would prefer "insert ignore" it works prefectly with me, I want to know also if there is any other way many thanks guys[/quote]are you using PHP5? if so, you can always create your own exception and throw it when the query fails. Quote Link to comment Share on other sites More sharing options...
fenway Posted October 24, 2006 Share Posted October 24, 2006 [quote author=isaac_cm link=topic=112352.msg456028#msg456028 date=1161560772]thanks alot, I know I can check first with "select" but I would prefer "insert ignore" it works prefectly with me, I want to know also if there is any other way many thanks guys[/quote]Well, there are really only two ways, both discussed above: either you check the DB directly, or you try and see what the DB throws back. For the latter, you have the DB not care, or you can have your PHP script care. Quote Link to comment Share on other sites More sharing options...
isaac_cm Posted October 25, 2006 Author Share Posted October 25, 2006 I can not understand , how you throw exception can you give me an example Quote Link to comment 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.