Rectangle 27 0

mysql Prevent duplicate records to a table using PHP?


<?php
    if(isset($_POST['submit'])) {

    //get the name and comment entered by user
    $firstName = $_POST['firstName'];
    $lastName = $_POST['lastName'];

    //connect to the database
    $dbc = mysqli_connect('host', 'username', 'password', 'dbname') or die('Error connecting to MySQL server');
    $check=mysqli_query($dbc,"select * from clients where firstname='$firstname' and lastname='$lastname'");
    $checkrows=mysqli_num_rows($check);

   if($checkrows>0) {
      echo "customer exists";
   } else {  
    //insert results from the form input
      $query = "INSERT IGNORE INTO clients(firstName, lastName) VALUES('$firstName', '$lastName')";

      $result = mysqli_query($dbc, $query) or die('Error querying database.');

      mysqli_close($dbc);
    }
    echo "Customer Added";
    };
  ?>

Hi Sai, thanks for your reply. It was really helpful to see how to put such a script together. Unfortunately I am getting the following problems: For a unique first and last name: Warning: mysqli_query() expects at least 2 parameters, 1 given in C:\wamp\www\php\Database\form_script.php on line 21 Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in C:\wamp\www\php\Database\form_script.php on line 23 If I try to add a first and last name already in the database I get the exact same errors as above with "Customer Added" as the output. How do I solve this?

Hi Sai, my bad - it was working. Thanks for working / showing me the script.

Hi Sai, thanks for the post update. I tried the updated script but still get errors. I made a couple of changes: 1 - the concatenation and 2 - the if/else brackets. The first part works no problems and new clients are added to the database with the correct output message. The second part now shows both messages of the if/else "Customer already exists" and "Customer added" together. I have edited the post above - hopefully you might see the problem. Thanks again, Andy ;-)

just check for rows in your db for firstname and lastname if exists echo- your message else insert

Note
Rectangle 27 0

mysql Prevent duplicate records to a table using PHP?


1062
mysqli_errno($dbc)
or die('Error querying database.');
or die(mysqli_errno($dbc) == 1062 ? "Client already listed" : 'Error querying database.');

Since your primary key is (firstName, lastName), you don't need php to prevent insertion of duplicate values. Mysql does that for you, because primary keys have to be unique. (if it was'nt your primary key you could use a unique constraint)

Note
Rectangle 27 0

mysql Prevent duplicate records to a table using PHP?


<?php
    if(isset($_POST['submit'])) {

    //get the name and comment entered by user
    $firstName = $_POST['firstName'];
    $lastName = $_POST['lastName'];

    //connect to the database
    $dbc = mysqli_connect('host', 'username', 'password', 'dbname') or die('Error connecting to MySQL server');
    $check=mysqli_query($dbc,"select * from clients where firstname='$firstname' and lastname='$lastname'");
    $checkrows=mysqli_num_rows($check);

   if($checkrows>0) {
      echo "customer exists";
   } else {  
    //insert results from the form input
      $query = "INSERT IGNORE INTO clients(firstName, lastName) VALUES('$firstName', '$lastName')";

      $result = mysqli_query($dbc, $query) or die('Error querying database.');

      mysqli_close($dbc);
    }
    echo "Customer Added";
    };
  ?>

Hi Sai, thanks for your reply. It was really helpful to see how to put such a script together. Unfortunately I am getting the following problems: For a unique first and last name: Warning: mysqli_query() expects at least 2 parameters, 1 given in C:\wamp\www\php\Database\form_script.php on line 21 Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in C:\wamp\www\php\Database\form_script.php on line 23 If I try to add a first and last name already in the database I get the exact same errors as above with "Customer Added" as the output. How do I solve this?

Hi Sai, my bad - it was working. Thanks for working / showing me the script.

Hi Sai, thanks for the post update. I tried the updated script but still get errors. I made a couple of changes: 1 - the concatenation and 2 - the if/else brackets. The first part works no problems and new clients are added to the database with the correct output message. The second part now shows both messages of the if/else "Customer already exists" and "Customer added" together. I have edited the post above - hopefully you might see the problem. Thanks again, Andy ;-)

just check for rows in your db for firstname and lastname if exists echo- your message else insert

Note
Rectangle 27 0

mysql Prevent duplicate records to a table using PHP?


1062
mysqli_errno($dbc)
or die('Error querying database.');
or die(mysqli_errno($dbc) == 1062 ? "Client already listed" : 'Error querying database.');

Since your primary key is (firstName, lastName), you don't need php to prevent insertion of duplicate values. Mysql does that for you, because primary keys have to be unique. (if it was'nt your primary key you could use a unique constraint)

Note