Jump to content

Recommended Posts

Hi! Im kinda' new to MySQL was wondering how i execute this.

 

Im making a scheduler, and i want it to be possible to make one note a day.

 

 

something like

 

if(row exsist $_POST["date"]){

UPDATE}

 

else{

INSERT}

 

So can someone help me creating this if statement perhaps?

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/223996-if-row-exsist-update-else-insert/
Share on other sites

mysql_query("INSERT INTO struktur2(datum, sort, amne, beskrivning, betyg) VALUES ('$datum', '$sort', '$amne', '$beskrivning','$betyg')

  ON DUPLICATE KEY UPDATE datum='$datum', amne='$amne', beskrivning='$beskrivning', betyg='$betyg', sort='$sort'");

 

I wrote this code based what the article said, though the  on duplicate key update part dosen't seem too work for me. What am i doing wrong?

 

 

Why can't you use REPLACE INTO?

 

What's the diffrence between UPDATE and REPLACE?

 

Did you read the link?

REPLACE works exactly like INSERT, except that if an old row in the table has the same value as a new row for a PRIMARY KEY or a UNIQUE  index, the old row is deleted before the new row is inserted.

 

It essentially updates the table if the pkey or unique index exists, if not, it will insert.

I tried to read the link, i just don't understand their explenation (english is not my main langauge), and i have no idea what the index does :/

 

You mean the unique index?  It's unique so if that index exists or primary key exists then it will delete the row and insert a new one, which essentially is updating it.  If they don't exist, it will insert a new row.  I'm not sure how else to explain it.  Maybe I'm just confused about what you want.

I'll try explaining it again.

 

I want my table only to have one row for every date of the year. (As i said it's a scheduler). So if this row doesn't exist i want it too insert it. If the row does exist i just want it too update and change the data from the other columns in the same row as the date that already exist.

 

I hope the explanation readable

I'll try explaining it again.

 

I want my table only to have one row for every date of the year. (As i said it's a scheduler). So if this row doesn't exist i want it too insert it. If the row does exist i just want it too update and change the data from the other columns in the same row as the date that already exist.

 

I hope the explanation readable

 

Makes perfect sense.  So you want to make your date column the primary key.  Then you can use REPLACE INTO to insert/update your data.  If the date exists, it will essentially update the row, if it doesn't then it will insert the fresh data with the new date.

It still doesn't work it won't update.

 

mysql_query("REPLACE INTO struktur2(datum, sort, amne, beskrivning, betyg) VALUES ('$datum', '$sort', '$amne', '$beskrivning','$betyg')");

 

This is the script.

 

I put the index to primary as type and date as field. Btw im using my PHP Admin if you didn't already know that.

What's that and how do you do it? Remember im kinda new to MySQL :/

 

Not through phpMyAdmin, I haven't used it in a very long time.  There should be some sort of option to print out the table definition.  If you have command line access then you can print it using:

 

describe [table_name];

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.