unemployment Posted February 20, 2012 Share Posted February 20, 2012 Is there any way to tell my php ajax file to run the update query if the data already exist and if not, then create the row in the database? I have both the update and the insert functions created, but was just wondering if I could tell php which one to use without passing through a parameter. Quote Link to comment https://forums.phpfreaks.com/topic/257372-insert-or-update-function-ajax/ Share on other sites More sharing options...
PFMaBiSmAd Posted February 20, 2012 Share Posted February 20, 2012 You would use an INSERT .... ON DUPLICATE KEY UPDATE ... query. See the example in this post - http://www.phpfreaks.com/forums/index.php?topic=354126.msg1672540#msg1672540 Quote Link to comment https://forums.phpfreaks.com/topic/257372-insert-or-update-function-ajax/#findComment-1319157 Share on other sites More sharing options...
unemployment Posted February 20, 2012 Author Share Posted February 20, 2012 You would use an INSERT .... ON DUPLICATE KEY UPDATE ... query. See the example in this post - http://www.phpfreaks.com/forums/index.php?topic=354126.msg1672540#msg1672540 Thanks for the suggestion. I can't seem to get it working though. It inserts a row everytime. I only want it to insert a row if one doesn't already exist and I want it to update the row if $int changes. What am I doing wrong? <?php public function send_pitch_feedback($cid, $gid, $int) { $cid = (int)$cid; $gid = (int)$gid; $int = (int)$int; mysql_query("INSERT INTO pitches_feedback (user_id, company_id, group_id, creation_time, interest) VALUES ({$GLOBALS['user_info']['uid']}, ${cid}, ${gid}, NOW(), ${int}) ON DUPLICATE KEY UPDATE interest = ${int}"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/257372-insert-or-update-function-ajax/#findComment-1319163 Share on other sites More sharing options...
PFMaBiSmAd Posted February 20, 2012 Share Posted February 20, 2012 Do you have a key defined in the table so that the ON DUPLICATE KEY part would have something to test to determine if the INSERT should be used or if the UPDATE should be used? Quote Link to comment https://forums.phpfreaks.com/topic/257372-insert-or-update-function-ajax/#findComment-1319164 Share on other sites More sharing options...
unemployment Posted February 20, 2012 Author Share Posted February 20, 2012 Probably not... Do you mean that I need to set like an index, or like a primary key? I don't really understand. I can't just choose to update the query if the primary key is the same. This was the only update query I had. I need to update the query if all of the WHERE clause is met. <?php public function update_pitch_feedback($cid, $gid, $int) { $cid = (int)$cid; $gid = (int)$gid; $int = (int)$int; $sql = "UPDATE `pitches_feedback` SET `interest` = {$int} WHERE `company_id` = ${cid} AND `user_id` = {$GLOBALS['user_info']['uid']} AND group_id = ${gid}"; return mysql_query($sql) or die(mysql_error()); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/257372-insert-or-update-function-ajax/#findComment-1319165 Share on other sites More sharing options...
Rifts Posted February 20, 2012 Share Posted February 20, 2012 you could just first do a check to see if their user ID exists and if it does update else insert. here is another idea $result = mysql_query("update test set col='test' where col_id='1';"); if (mysql_affected_rows()==0) { $result = mysql_query("insert into test (col_id, col) values ('1','test');"); } Quote Link to comment https://forums.phpfreaks.com/topic/257372-insert-or-update-function-ajax/#findComment-1319246 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.