Find the answer to your Linux question:
Results 1 to 3 of 3
Hi I how can I prevent duplicate enteries in mysql database. I set slim browser to auto refresh every second, as I was hit n trying codes, learning my myself ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux Newbie
    Join Date
    Aug 2005
    Posts
    213

    how to prevent duplicate enteries


    Hi
    I how can I prevent duplicate enteries in mysql database.
    I set slim browser to auto refresh every second, as I was hit n trying
    codes, learning my myself from online sites.

    suddenly i saw in phpmyadmin theat there were 44 duplicate entries already. Ho can i make an error appear if similar entry exists.

    PHP Code:
    <?php
    include 'conn.php';
    //create table record

    mysql_query 

    ("CREATE TABLE record(
    id INT AUTO_INCREMENT NOT NULL,
    names VARCHAR (33),
    pwd1 VARCHAR(44),
    pwd2 VARCHAR(33),
    email VARCAR (33),
    notes VARCHAR (99))"
    );


    echo 
    '<br>';
    echo 
    'table record created!!';

    ?>
    Cool Surfer - Registered linux user #397629

  2. #2
    Linux Enthusiast
    Join Date
    Apr 2004
    Location
    UK
    Posts
    682
    You don't say how many columns need to match to be a duplicate, but it looks like a table of users so I'd assume duplicates mean two records with the same name.

    From the Mysql documentation

    Code:
    CREATE TABLE record(
    id INT AUTO_INCREMENT NOT NULL,
    names VARCHAR (33),
    pwd1 VARCHAR(44),
    pwd2 VARCHAR(33),
    email VARCAR (33),
    notes VARCHAR (99),
    primary key(names))
    I've not tested this code because I don't have mysql handy, but if it doesn't work then you can refer to the documentation to fix it up.

    This puts a primary key index on the names colmun, requiring that the field be populated and unique. If you try to put in another record with a duplicate name then it will raise a primary key violation error and prevent you from doing it.

    Let us know how you get on,

    Chris...
    To be good, you must first be bad. "Newbie" is a rank, not a slight.

  3. #3
    Linux Newbie
    Join Date
    Aug 2005
    Posts
    213
    Thanks, it seems logical n should work. Will try it.
    Cool Surfer - Registered linux user #397629

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •