简体   繁体   English

如何更新一行(如果存在),或者创建一个新行(如果在mysql中不存在)

[英]how do I update a row if it exists, or create a new one if it doesnt exist in mysql

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. 更新:我添加了一个名为“ u_id”的列,该列保存数据库中每个联系人的唯一信息。 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. 从阅读的所有内容中,我需要使用ON DUPLICATE KEY UPDATE在提交表单时用数据库中的新信息替换旧信息。 While the insert part of my code is working, the portion with ON DUPLICATE KEY UPDATE is not working. 当我的代码的插入部分起作用时,带有ON DUPLICATE KEY UPDATE的部分不起作用。

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. 我也尝试过REPLACE INTO(而不是INSERT和ON DUPLICATE KEY UPDATE),它也不起作用。 Here is my column structure in my MySQL database: 这是我的MySQL数据库中的列结构:

+-------------+-------------+------+-----+-----------+-------------------+
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. 我是PHP和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. 我已经研究了这个概念三天了,已经阅读了很多有关它的信息,但是仍然无法使它起作用。

I guess contact id is your Key and it is an identity value which increments automatically? 我想联系人ID是您的密钥,它是一个自动递增的身份值? 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'" ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM