How to Avoid Duplicate Entries With PHP in MySQL

Written by steve mcdonnell Google
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Avoid Duplicate Entries With PHP in MySQL
Check if a record exists before adding it to a database. (Jupiterimages/Photos.com/Getty Images)

Many PHP applications will allow users to enter data that is then inserted into a MySQL database. It's important for these applications to check whether or not a record already exists in the MySQL database before adding it. A reactive way to do this is to set a unique index on the key field in the MySQL database, add the record and check to see if an error occurred. However, the more graceful, proactive approach is to check for a duplicate record before adding data to the database.

Skill level:
Easy

Other People Are Reading

Instructions

  1. 1

    Launch an editor and create a new PHP script file. For example, type:

    nano adddata.php

  2. 2

    Create the HMTL form to accept user input. Set the method to "post" and the action to "update.php" to post the submitted results to an "update.php" file you will create next. For example, type:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">

    <head>

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

    <title>Data Entry/title>

    </head>

    <body>

    <form method="post" action="update.php">

    <input type="hidden" name="completed" value="1" />

    <p>Customer Number: <input type="text" name="custnum" size="10" /></p>

    <p>Customer Name: <input type="text" name="custname" size="30" /></p>

    <p><input type="submit" name="submit" value="submit" /></p>

    </form>

    </body>

    </html>

  3. 3

    Exit the editor and save the file. Launch the editor again and create the "update.php" file. For example, type:

    nano update.php

  4. 4

    Check the hidden field form to ensure that the "update.php" file was called from the "post" operation. For example, type:

    <?php

    $completed = $_POST['completed'];

    if ($completed != "1") die("<p>Form not completed!</p>");

  5. 5

    Extract the customer number and customer name from the posted results. Check to make sure each value contains data. For example, type:

    $number = (isset($_POST['custno']) ? $_POST['custno'] : "";

    $name = (isset($_POST['custname'] ? $_POST['custname'] : "";

    if ($number == "" || $name == "") die("<p>Form not filled out correctly!</p>");

  6. 6

    Connect to the MySQL server and select the desired database. For example, type:

    $dbc = mysql_connect("localhost", "username", "password") or die("<p>Could not connect to server! Error is " . mysql_error());

    $dB = mysql_select_db("customers");

  7. 7

    Create and execute a query on the database that searches for the customer number you are supposed to add. Make sure you properly escape user input strings with "mysql_real_escape_string." Check the return value and exit if the customer number already exists. For example, type:

    $query = 'SELECT custno FROM customers WHERE custom="' . mysql_real_escape_string($number) . '"';

    $result = mysql_query($query);

    if ($result && mysql_num_rows($result) > 0) die("<p>Customer already exists!</p>");

  8. 8

    Create and execute a query to insert the new customer into the database. Make sure you properly escape user input strings with "mysql_real_escape_string." For example, type:

    $query = 'INSERT INTO customers (custno, custname) VALUES ("' . mysql_real_escape_string($number) . '", "' . mysql_real_escape_string($name) . '")';

    $result = mysql_query($query);

    if (!$result) die("<p>Error inserting customer record!</p>");

    echo "<p>Customer successfully added!</p>";

    ?>

Don't Miss

Filter:
  • All types
  • Articles
  • Slideshows
  • Videos
Sort:
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the eHow.co.uk site, you consent to the use of cookies. For more information, please see our Cookie policy.