Nathan88 Posted April 18, 2008 Share Posted April 18, 2008 I have a form that lets the user input "employeeNo" as the primary key. what i would like to know is how can i create a function that will check the database to see if this primary key has been used and prompt a message to the user saying this. At the moment if a user enters a primary key thats in use it just says error updating database. Any help will be much appreciated. Sample of the code i have for the form so far: The line in bold and in red is the field that the user will enter a primary key. <html> <head> <title>Form</title> <script> function checkform (form) { if (form.name.value == "" || form.name.value ==null) { alert( "One or more of the required fields was not entered. Please fill the fields." ); return false ; } if (form.comment1.value == "" || form.comment1.value ==null) { alert( "One or more of the required fields was not entered. Please fill the fields." ); return false ; } if (form.comment2.value == "" || form.comment2.value ==null) { alert( "One or more of the required fields was not entered. Please fill the fields." ); return false ; } if (form.comment3.value == "" || form.comment3.value ==null) { alert( "One or more of the required fields was not entered. Please fill the fields." ); return false ; } if (form.comment4.value == "" || form.comment4.value ==null) { alert( "One or more of the required fields was not entered. Please fill the fields." ); return false ; } return true ; } </script> </head> <body> <table border="0"> <form action="insertdata.php" method="post"> <tr><td>First Name: <br /> <input type="text" name="name"></tr></td> [b]<tr><td>Employee Number: <br /> <input type="text" name="employeeNo"></tr></td>[/b] <tr><td><b>Comments:</b> <br /> <input type="text" name="comment1"></tr></td> <tr><td><b>Comments:</b> <br /> <input type="text" name="comment2"></tr></td> <tr><td><b>Comments:</b> <br /> <input type="text" name="comment3"></tr></td> <tr><td><b>Comments:</b> <br /> <input type="text" name="comment4"></tr></td> <tr><td><input type="submit" value="Submit" onclick="return checkform (form)" style="width:100;height:25"> <input type="reset" value="Reset" style="width:100;height:25"></tr></td> </form> </table> </body> </html> The line in bold and in red is the field that the user will enter a primary key. Link to comment https://forums.phpfreaks.com/topic/101639-need-a-fuction-to-check-if-a-entered-primary-key-from-a-form-is-in-use/ Share on other sites More sharing options...
marcus Posted April 18, 2008 Share Posted April 18, 2008 If the row in the database is primary key a new record will not be written over. Link to comment https://forums.phpfreaks.com/topic/101639-need-a-fuction-to-check-if-a-entered-primary-key-from-a-form-is-in-use/#findComment-520046 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.