Hi
I need to know how to avoid duplicate names to be entered in my database.
The field I'm trying to avoid duplicate names is Bloom_Name.
Can you please help me.

CREATE TABLE ihs (
id int(11) NOT NULL auto_increment,
Bloom_Name varchar(255) NOT NULL default '',
Pod_Parent text NOT NULL,
Pollen_Parent text NOT NULL,
Hybridiser text NOT NULL,
Origin text NOT NULL,
Grower text NOT NULL,
Color_Group text NOT NULL,
Type_of_Bloom text NOT NULL,
Regular_or_Mini text NOT NULL,
Size_Range text NOT NULL,
Propagation text NOT NULL,
Bloom_Color text NOT NULL,
Bloom_Characteristics text NOT NULL,
Leaf_Characteristics text NOT NULL,
Bush_Characteristics text NOT NULL,
Photo_Credit text NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY id (id),
KEY Bloom_Name (Bloom_Name)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='Created by phpFormGenerator' AUTO_INCREMENT=8729 ;

    Just run a SELECT query before you do any INSERT. If your SELECT returns a result, you have a duplicate and should not do the INSERT.

      you should really put a unique index restraint on the database column. You can also do the select check to give a friendly error message, but this is a database issue and you should let the database enforce this kind of restriction.

        Sorry!! I'm a newbie, I've been trying to add a unique index restraint on the database using myphpadmin, but I cant seem to get it right, can you please modify the table I post, is that possible? 🙂
        Thanks 🙂 🙂 🙂

          Wouldn't it be

          UNIQUE KEY `Bloom_Name` (`Bloom_Name`)

          instead of

          KEY `Bloom_Name` (`Bloom_Name`)

          ?
          (I'm not an SQL expert either)

            Thank you so much for your help, I got it working on the database, I used a unique index restraint, as you suggested, now I will have to work on the php script, any ideas?
            JD

              now I will have to work on the php script, any ideas?

              Ideas on what? How to run queries? You might start by reading the [man]mysql_query/man manual entry.

                I would like to check the field Bloom_Name, I have added the codes in red, but I get an error on line 7, can you please help me with this code.

                <?php
                include("global.inc.php");
                $result = mysql_query("SELECT Bloom_Name FROM ihs WHERE Bloom_Name = $Bloom_Name);
                if (mysql_num_rows($result) >= 1) {
                echo ("Cultivar Name Already Used, Please use another Name");
                } else {
                $insert = mysql_query("INSERT INTO ihs (Bloom_Name) VALUES ($Bloom_Name);
                }
                $errors=0;
                $error="The following errors occured while processing your form input.<ul>";
                pt_register('POST','BloomName');
                pt_register('POST','PodParent');

                  Your query is no good. Try...

                  $insert = mysql_query("INSERT INTO ihs (Bloom_Name) VALUES ('$Bloom_Name')");
                  

                  Also... instead of highlighting code in red (yellow), try using the boards custopm [ php ] [/ php ] tags without the sapces. It provides syntax highlighting... much easier to read.

                    I've changed the code you suggested but I get the following error.

                    Parse error: parse error, unexpected T_STRING in /home/wayne/public_html/regform/process.php on line 5

                      Hi,
                      For days I been trying to get this code to work, I’ve gone through all the posts in this forum without any success.
                      What I’m trying to do is, avoid duplicate names in my database, I’ve solved this problem in Mysql database (thanks to this forum) Now, I’m trying to do the same with the php script.
                      The text field in my form is BloomName” database field is Bloom_Name table name is “ihs”
                      The code I’m trying:

                      $link = mysql_connect("localhost","xxx_xx","xxxxx");
                      mysql_select_db("xxxx_xx",$link);
                      $result = mysql_query("SELECT Bloom_Name FROM ihs WHERE Bloom_Name = $BloomName);
                      $dupl=Cultivar Name Already Used, Please use another Name
                      if (mysql_num_rows($result) >= 1) {
                      echo $dupl;
                      } else { 
                      $insert = mysql_query("INSERT INTO ihs  (Bloom_Name,) values ('".$BloomName."')";
                      mysql_query($query);
                      ?>

                      I’m getting the following error:
                      Parse error: parse error, unexpected T_STRING in /home/wayne/public_html/regform/process.php on line 9

                        Write a Reply...