php - UPDATE if exists else insert -


this question has answer here:

what correct format update if exists else insert in situation - have managed update , insert separately need them together?.

what want - if date exists gets updated fullday, , if doesn't exist new row new details gets inserted.

$date = (isset($_post['date']) ? $_post['date'] : null); $reservationtype = (isset($_post['reservation-type']) ? $_post['reservation-type'] : null);  $connect = new mysqli($servername, $username, $password, $dbname);   // check connection   if ($connect->connect_error) {     die("connection failed: ". $connect->connect_error); }  $sql = "select * reservations"; $result = $connect->query($sql);  if ($result -> num_rows > 0) {     while ($row = $result->fetch_assoc()) {         if ($date == $row['date_of_reservation']) {            "update reservations set reservationtype = 'fullday'   date_of_reservation='$date'";     }     else {         "insert reservations (date_of_reservation, reservationtype) values ('$date', '$reservationtype')";         }     } } 

from the manual:

insert table (a,b,c) values (1,2,3)   on duplicate key update c=c+1; 

Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -