How to update row in PHP/mysql?

Mathias Kirchweger

I import outlook contacts file into my database and then display it into html table. The table is created and everything is ok, but when I try to upload it a second time, all names and information is doubled. So what I want to do is if the first name and the last name already exists just to update the row and all cells in that row and not insert that row over and over again.

"CREATE TABLE contacts(PID INT NOT NULL AUTO_INCREMENT,PRIMARY KEY(PID),first_name CHAR(80),middle_name CHAR(80),last_name CHAR(80)"

I tried with on duplicate key update/replace but it didnt work.

"INSERT INTO contacts (first_name,middle_name,last_name)
VALUES ('$first_name','$middle_name','$last_name')"

I guess my problem is in the auto_increment and primary key but I don't really understand how they work and how to do that.

Marc B

Your INSERT would never "update" your existing record, because you're missing the "on duplicate key" business:

INSERT INTO sometable (field1, field2) 
ON DUPLICATE KEY UDPATE field1=VALUES(field1), field2=VALUES(field2)

and of course, this will only work if the INSERT would actually cause a duplicate key violation. The only unique index you have in your table is the primary key, and you're not inserting a value into it at all, so there will NEVER be a duplicate key violation.

You probably want an UPDATE query instead:

UPDATE yourtable
SET field1='value for field1', field2='value for field2'
WHERE id=$your_record_id

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to Update a Row with Nhibernate

From Dev

How to Update a Row with Nhibernate

From Dev

How to update a MySQL row based on previous row?

From Dev

How to update a row with a substring of a string in that particular row?

From Dev

SQL: how to update a row based on a previous row

From Dev

How to update table row in Libgdx?

From Dev

How to update specific row in MYSQL

From Dev

How to update every nth row

From Dev

How to update a specific row in SQLite?

From Dev

How to update every nth row

From Dev

How to update specific row in SQLite

From Dev

How to update a row by clicking on update hyperlink of that row through java script?

From Dev

How to write trigger to update row in another table?

From Dev

How to update multiple row data in a single query?

From Dev

How to update the data in particular table row in HTML?

From Dev

How to use ROW_NUMBER() in UPDATE clause?

From Dev

How to update an input value while dragging a row?

From Dev

How to update a single column on a single row?

From Dev

SQL How to Update only first Row

From Dev

How to update a table if a row is deleted in another one

From Dev

How to update the nth row in a SQL database table?

From Dev

How to update field when delete a row in laravel

From Dev

How to update each row of a column by the results of a select?

From Dev

How to update Kendo Grid row from window

From Dev

How to solve "Cannot add or update child row"

From Dev

How to update TableView Row using javaFx

From Dev

How to update an input value while dragging a row?

From Dev

Android How to update single row in listview?

From Dev

How to asynchronously update Mysql row count?

Related Related

HotTag

Archive