php - UPDATE if exists else insert -
this question has answer here:
- how 'insert if not exists' in mysql? 11 answers
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
Post a Comment