Jump to content

Need a fuction to check if a entered primary key from a form is in use ??????


Nathan88

Recommended Posts

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.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.