loozah Posted February 11, 2010 Share Posted February 11, 2010 I'm not sure if I'm posting in the proper area for this question, if not I apologize... I am trying to place the "Duplicate entry" error next to the text input field in the script below. The script works fine but if the input is duplicated of course it dies when the mysql_error is reached. I need it to show the form input field with the date still in the field and something like "That input is already in the database", I have done multiple searches but not sure if I'm wording my searches properly... still a newbie at this stuff. Bye the way the column is set to "Unique" in the db to prevent duplicates. Thank in advance for any help <?php include 'my_connect.php'; if ($_POST['submit']) { $title = $_POST['title']; mysql_query("INSERT INTO my_dbtable (title) VALUES ( '$title')") or die(mysql_error()); } ?> <form id="form1" name="form1" method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <input name="title" type="text" value="" /> <label> <input type="submit" name="submit" id="submit" value="submit" /> </label> </form> Quote Link to comment https://forums.phpfreaks.com/topic/191739-how-do-i-show-error-messages-on-the-input-form/ Share on other sites More sharing options...
PravinS Posted February 11, 2010 Share Posted February 11, 2010 You need to check the posted value in database using SELECT query, if record it found in database then show the error message else run the insert query. Quote Link to comment https://forums.phpfreaks.com/topic/191739-how-do-i-show-error-messages-on-the-input-form/#findComment-1010607 Share on other sites More sharing options...
loozah Posted February 11, 2010 Author Share Posted February 11, 2010 This seems to be working... <?php include 'my_connect.php'; if ($_POST['submit']) { $title = $_POST['title']; $result = mysql_query("SELECT * FROM my_dbtable"); while($row = mysql_fetch_array($result)) if ($title == $row['title']) $error = "YerSoooFukt!"; else mysql_query("INSERT INTO my_dbtable (title) VALUES ( '$title')") or die(mysql_error()); } ?> <form id="form1" name="form1" method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <input name="title" type="text" value="" /> <label> <input type="submit" name="submit" id="submit" value="submit" /> </label> <?php echo $error; ?></form> Thanks for the speedy assistance Quote Link to comment https://forums.phpfreaks.com/topic/191739-how-do-i-show-error-messages-on-the-input-form/#findComment-1010618 Share on other sites More sharing options...
jl5501 Posted February 11, 2010 Share Posted February 11, 2010 <?php include 'my_connect.php'; $msg = ''; if ($_POST['submit']) { $title = $_POST['title']; $res = mysql_query("select * from my_dbtable where title='$title"); if(mysql_num_rows($res)) { $msg = "Duplicate entry"; } else { mysql_query("INSERT INTO my_dbtable (title) VALUES ( '$title')") or die(mysql_error()); } } ?> <form id="form1" name="form1" method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <input name="title" type="text" value="<?php if(isset($_POST)) echo $_POST['title'];?>" /> <?php if(isset($msg)) echo $msg;?> <label> <input type="submit" name="submit" id="submit" value="submit" /> </label> </form> Quote Link to comment https://forums.phpfreaks.com/topic/191739-how-do-i-show-error-messages-on-the-input-form/#findComment-1010621 Share on other sites More sharing options...
loozah Posted February 11, 2010 Author Share Posted February 11, 2010 My apologies to all for forgetting to remove the naughty error message, it's in poor taste for a forum. That'll teach me to get some sleep. Once again sorry. Quote Link to comment https://forums.phpfreaks.com/topic/191739-how-do-i-show-error-messages-on-the-input-form/#findComment-1010622 Share on other sites More sharing options...
loozah Posted February 11, 2010 Author Share Posted February 11, 2010 Thank you Jl5501 your solution is far more elegant Quote Link to comment https://forums.phpfreaks.com/topic/191739-how-do-i-show-error-messages-on-the-input-form/#findComment-1010628 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.