erik345 Posted January 10, 2011 Share Posted January 10, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/223996-if-row-exsist-update-else-insert/ Share on other sites More sharing options...
Pikachu2000 Posted January 10, 2011 Share Posted January 10, 2011 Something like this? INSERT . . . ON DUPLICATE KEY UPDATE Post any questions you might have after reading it. Quote Link to comment https://forums.phpfreaks.com/topic/223996-if-row-exsist-update-else-insert/#findComment-1157541 Share on other sites More sharing options...
erik345 Posted January 10, 2011 Author Share Posted January 10, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/223996-if-row-exsist-update-else-insert/#findComment-1157588 Share on other sites More sharing options...
Maq Posted January 10, 2011 Share Posted January 10, 2011 Why can't you use REPLACE INTO? Quote Link to comment https://forums.phpfreaks.com/topic/223996-if-row-exsist-update-else-insert/#findComment-1157595 Share on other sites More sharing options...
erik345 Posted January 10, 2011 Author Share Posted January 10, 2011 Why can't you use REPLACE INTO? What's the diffrence between UPDATE and REPLACE? Quote Link to comment https://forums.phpfreaks.com/topic/223996-if-row-exsist-update-else-insert/#findComment-1157603 Share on other sites More sharing options...
Maq Posted January 10, 2011 Share Posted January 10, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/223996-if-row-exsist-update-else-insert/#findComment-1157606 Share on other sites More sharing options...
erik345 Posted January 10, 2011 Author Share Posted January 10, 2011 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 :/ Quote Link to comment https://forums.phpfreaks.com/topic/223996-if-row-exsist-update-else-insert/#findComment-1157641 Share on other sites More sharing options...
Maq Posted January 10, 2011 Share Posted January 10, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/223996-if-row-exsist-update-else-insert/#findComment-1157646 Share on other sites More sharing options...
erik345 Posted January 10, 2011 Author Share Posted January 10, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/223996-if-row-exsist-update-else-insert/#findComment-1157649 Share on other sites More sharing options...
Maq Posted January 10, 2011 Share Posted January 10, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/223996-if-row-exsist-update-else-insert/#findComment-1157651 Share on other sites More sharing options...
erik345 Posted January 10, 2011 Author Share Posted January 10, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/223996-if-row-exsist-update-else-insert/#findComment-1157655 Share on other sites More sharing options...
Maq Posted January 10, 2011 Share Posted January 10, 2011 Can you print out your table definition? Quote Link to comment https://forums.phpfreaks.com/topic/223996-if-row-exsist-update-else-insert/#findComment-1157657 Share on other sites More sharing options...
erik345 Posted January 10, 2011 Author Share Posted January 10, 2011 What's that and how do you do it? Remember im kinda new to MySQL :/ Quote Link to comment https://forums.phpfreaks.com/topic/223996-if-row-exsist-update-else-insert/#findComment-1157662 Share on other sites More sharing options...
Maq Posted January 10, 2011 Share Posted January 10, 2011 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]; Quote Link to comment https://forums.phpfreaks.com/topic/223996-if-row-exsist-update-else-insert/#findComment-1157666 Share on other sites More sharing options...
erik345 Posted January 11, 2011 Author Share Posted January 11, 2011 Will i tested around with some settings in myPHPadmin and when i removed the ID column it worked. Why is this? The ID column was and INT set to auto_increment. Was the ID interfering in some way? Quote Link to comment https://forums.phpfreaks.com/topic/223996-if-row-exsist-update-else-insert/#findComment-1158084 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.