0

orgI am trying to get this query correct. I want to insert a record into the database upon form submission but only if the record does not already exist. If the record exists, then I want it to be updated in the database.

What is happening: Upon form submit, a new record is inserted into the database every time. Even if it is a duplicate.

UPDATE: I added a column called "u_id" which holds unique information for each contact in the database. So, I made this my Unique Key column.

 if($_POST['submit']){
       $con=mysqli_connect("localhost","username","password","database_name");
       // Check connection
    if (mysqli_connect_errno())
    {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    $contact = ($_POST['contact']);
    $u = ($_POST['uid']);
    $org = mysql_real_escape_string($_POST['organization']);
    $namefirst = mysql_real_escape_string($_POST['firstName']);
    $namelast = mysql_real_escape_string($_POST['lastName']);
    $emailaddy = mysql_real_escape_string($_POST['email']);
    $phonenum = mysql_real_escape_string($_POST['phone']);
    $appquestion = mysql_real_escape_string($_POST['appquestion']);
    $banner = mysql_real_escape_string($_POST['banner']);
    $bulletin = mysql_real_escape_string($_POST['bulletin']);
    $giveaway = mysql_real_escape_string($_POST['giveaway']);
    $app = mysql_real_escape_string($_POST['app']);
    $tshirt = mysql_real_escape_string($_POST['tshirt']);
    $tshirtp = mysql_real_escape_string($_POST['tshirtp']);
    $print = mysql_real_escape_string($_POST['print']);
    $party = mysql_real_escape_string($_POST['party']);
    $orgnotes = mysql_real_escape_string($_POST['notes']);


    $sql="INSERT INTO database_name (contact_id, u_id, first_name, last_name, email_address, phone_number, org, appquestion, banner, bulletin, giveaway, app, tshirt, promised_tee, print, party, org_notes)
      VALUES
          ('$contact', '$u', '$namefirst','$namelast','$emailaddy','$phonenum','$org','$appquestion','$banner','$bulletin','$giveaway','$app','$tshirt','$tshirtp','$print','$party','$orgnotes')   

      ON DUPLICATE KEY UPDATE first_name = '$namefirst', last_name = '$namelast', email_address = '$emailaddy', phone_number = '$phonenum', org = '$org', appquestion = '$appquestion', banner = '$banner', bulletin = '$bulletin', giveaway = '$giveaway', app = '$app', tshirt = '$tshirt', promised_tee = '$tshirtp', print = '$print', party = '$party', org_notes = '$orgnotes'" ;



    if (!mysqli_query($con,$sql))
    {
      die('Error: ' . mysqli_error($con));
    }
      echo "1 record added";



    mysqli_close($con);
    }

From everything I have read, I need to use ON DUPLICATE KEY UPDATE to replace the old information with new information in the database upon form submission. While the insert part of my code is working, the portion with ON DUPLICATE KEY UPDATE is not working.

Why might this portion of the code not be working? Is there a better way to insert else update the information?

I have also tried REPLACE INTO (instead of INSERT and ON DUPLICATE KEY UPDATE), it didn't work either. Here is my column structure in my MySQL database:

+-------------+-------------+------+-----+-----------+-------------------+
Field         |  Type       | Null | Key |  Default  |  Extra   
+-------------+-------------+------+-----+-----------+-------------------+
contact_id    | int(1)      |   NO | PRI |  NULL     |   auto_increment
u_id          | char(32)    |   NO | UNI |  NULL     |
title         | varchar(80) |   NO |     |  NULL     |
first_name    | varchar(100)|   NO |     |  NULL     |
last_name     | varchar(100)|   NO |     |  NULL     |
job_title     | varchar(255)|   NO |     |  NULL     |
address_1     | varchar(255)|   NO |     |  NULL     |
address_2     | varchar(255)|   NO |     |  NULL     |
org_city      | varchar(100)|   NO |     |  NULL     |
org_state     | varchar(100)|   NO |     |  NULL     |
zip_code      | varchar(8)  |   NO |     |  NULL     |
country       | varchar(100)|   NO |     |  NULL     |
phone_number  | varchar(15) |   NO |     |  NULL     |
email_address | varchar(100)|   NO |     |  NULL     |
org           | varchar(150)|   NO |     |  NULL     |
appquestion   | tinyint(1)  |   NO |     |  NULL     |
banner        | tinyint(1)  |   NO |     |  NULL     |
bulletin      | tinyint(1)  |   NO |     |  NULL     |
giveaway      | tinyint(1)  |   NO |     |  NULL     |
app           | tinyint(1)  |   NO |     |  NULL     |
tshirt        | tinyint(1)  |   NO |     |  NULL     |
promised_tee  | tinyint(1)  |   NO |     |  NULL     |
print         | tinyint(1)  |   NO |     |  NULL     |
party         | tinyint(1)  |   NO |     |  NULL     |
org_notes     | varchar(255)|   NO |     |  NULL     |
notes         | varchar(255)|   NO |     |  NULL     |
+-------------+-------------+------+-----+-----------+-------------------+

Thank you for any help or guidance you can give me! I am new to PHP and MySQL. I've been working on this concept for three days and have read a ton of information about it, but am still not able to get it to work.

  • Can you double check if contact_id is indeed set as primary key and therefore unique. Two records with contact_id = '' should not be possible with these restrictions. – Barry Oct 04 '14 at 15:23
  • Hi Barry! Thank you for your response. I have double-checked and contact_id is indeed set as primary key and also unique. – MissScarlett Oct 04 '14 at 15:39
  • Is Kartik right that you have defined column_id as an AUTO INCREMENT? And that the contacts are both added with a different contact_id? – Barry Oct 04 '14 at 15:40
  • Yes, Kartik is correct. When setting up my database initially, I set column_id as my primary and auto increment. When duplicate contacts are inserted, they are given different contact_ids. – MissScarlett Oct 04 '14 at 15:44
  • So that is the cause why it is not working. You have to offer an existing key to fire the ON DUPLICATE KEY part for existing contacts. But can you? – Barry Oct 04 '14 at 15:53
  • I think the problem is that some of the contacts in the database share the same information like email address, last name, etc. so I can't set another unique key with any of those parameters. – MissScarlett Oct 04 '14 at 16:01
  • That is exactly the functional problem you are facing. You have no means to identify a user. I cannot help you on that part. – Barry Oct 04 '14 at 16:05
  • That's not a problem. Now that I know why the values aren't updating and that I was using the correct "thought process" with ON DUPLICATE KEY UPDATE, I can go about assigning each person a unique key -- from the data that is already there. Then, code up a function that automatically appends a unique identifier to new contacts. I appreciate your help and Kartik's help with the explanation of the auto increment factor. – MissScarlett Oct 04 '14 at 16:46

1 Answers1

1

I guess contact id is your Key and it is an identity value which increments automatically? In that case try this insert statement.

INSERT INTO database_name (first_name, last_name, email_address, phone_number, org, appquestion, banner, bulletin, giveaway, app, tshirt, promised_tee, print, party, org_notes) VALUES ('$namefirst','$namelast','$emailaddy','$phonenum','$org','$appquestion','$banner','$bulletin','$giveaway','$app','$tshirt','$tshirtp','$print','$party','$orgnotes')
ON DUPLICATE KEY UPDATE first_name = '$namefirst', last_name = '$namelast', email_address = '$emailaddy', phone_number = '$phonenum', org = '$org', appquestion = '$appquestion', banner = '$banner', bulletin = '$bulletin', giveaway = '$giveaway', app = '$app', tshirt = '$tshirt', promised_tee = '$tshirtp', print = '$print', party = '$party', org_notes = '$orgnotes'" ;

Kartik
  • 78
  • 1
  • 7
  • can you please update the table structure?. am slightly confused with the KEYS – Kartik Oct 04 '14 at 15:27
  • I think this will never fire the ON DUPLICATE part because the key is not included in the insert statement making the ON DUPLICATE part redundant. – Barry Oct 04 '14 at 15:31
  • Please Refer the First Answer and there are some other options also. http://stackoverflow.com/questions/548541/insert-ignore-vs-insert-on-duplicate-key-update – Kartik Oct 04 '14 at 15:35
  • I actually tried it this way and it didn't update on duplicate key. I'm starting to think the reason has to do with my contact_id as it is my primary key and is also unique. I also want to confirm that contact_id is set to auto increment. – MissScarlett Oct 04 '14 at 15:41