gpt4 book ai didi

php - Mysql表格单元格在点击时不会改变值

转载 作者:行者123 更新时间:2023-11-29 21:00:21 24 4
gpt4 key购买 nike

我正在尝试编写一段 PHP 代码来更改某个公寓的可用性。这是我的代码:

<!DOCTYPE html>
<html>

<head>
<title></title>
</head>

<body>

<?php

$servername = "localhost";
$username = "bla";
$password = "blabla";
$dbname = "testDB";

// Create connection
$connect = mysqli_connect($servername, $username, $password, $dbname);

// Check connection
if ($connect->connect_error) {
die("Connection failed: " . $connect->connect_error);
}else{
echo "Connected successfully to the database: " . $dbname . "<br><br>";
}

$query = "SELECT * FROM test";
$result = mysqli_query($connect, $query);
$row = mysqli_fetch_array($result);

echo "id: " . $row["id"] . " // Address: " . $row["address"] . " // Rooms: " . $row["rooms"] . " // Availability: " . $row["availability"] . ".<br>";
?>
<form method ="post" action ="<?php $_PHP_SELF ?>">
<input name="update" type="submit" value="Change Availability">
</form>
<?php

if(isset($_POST["update"])) {
$avail = mysqli_query($connect,"SELECT availability FROM test WHERE id='1'");
$availCheck = mysqli_fetch_array($avail);
settype($availCheck, "int");

if($availCheck == 1){
$upAvail = mysqli_query($connect,"UPDATE test SET availability='0' WHERE id='1'");
}else{
$upAvail = mysqli_query($connect,"UPDATE test SET availability='1' WHERE id='1'");
}
}

?>

</body>

</html>

这是我得到的输出:

已成功连接数据库:testDB

id:1//地址:3787 cote des neiges//房间:2//可用性:1。更改可用性

这是我的问题。当可用性为 1 时,我按下按钮,它会变为 0。但之后,当我再次按下按钮时,它不会变回 1。

这是为什么?

感谢您的帮助。

最佳答案

您没有切换可用性mysqli_fetch_arraytest 表中获取与 id (id=1) 匹配的行。因此,在将非空数组转换为整数后,$availCheck 将始终等于 1

您可以使用单个 MySQL 查询替换所有逻辑来切换值。

UPDATE test SET availability = IF(availability = 1, 0, 1) WHERE id=1

我想你想做的是:

 $availCheck = mysqli_fetch_array($avail)['availability'];

关于php - Mysql表格单元格在点击时不会改变值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37288617/

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