gpt4 book ai didi

php - 更新如果存在否则插入

转载 作者:行者123 更新时间:2023-12-04 00:28:13 26 4
gpt4 key购买 nike

<分区>

在这种情况下,如果存在 else insert,UPDATE 的正确格式是什么 - 我已经设法单独进行更新和插入,但需要将它们放在一起?

我想要的是 - 如果日期存在则更新为全天,如果不存在则插入包含新详细信息的新行。

$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 * FROM 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' WHERE date_of_reservation='$date'";
}
else {
"INSERT INTO reservations (date_of_reservation, reservationType) VALUES ('$date', '$reservationType')";
}
}
}

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com