gpt4 book ai didi

php - 如何从数据库中读取数据更改它们并更新数据库

转载 作者:行者123 更新时间:2023-11-29 07:18:32 25 4
gpt4 key购买 nike

我需要从数据库中读取数据然后用 php 编辑这些数据并用这些数据更新数据库。

我可以从数据库中获取数据但无法将它们写回(我添加了另一个查询以查看值是否已更改)

我已经尝试使用 phpMyAdmin 来查看值是否发生了变化,但它们仍然存在一样

$POWER = "";
if ($pressed == "1") {
echo "pressed\n";
$sql = "SELECT `POWER` FROM `VolleyTest` WHERE ID = 1";
$result = mysqli_query($conn, $sql);
while ($row = mysqli_fetch_row($result)) {
$POWER = $row[0];
echo "pressed power request $row[0]\n";
}
if ($POWER == "ON") {
$sql = 'UPDATE `VolleyTest` SET `POWER`=\"OFF\" WHERE ID = 1';
$conn->query($sql);
echo " POWER ON to OFF\n";
} else if ($POWER == "OFF") {
$sql = 'UPDATE `VolleyTest` SET `POWER`=\"ON\" WHERE ID = 1';
$conn->query($sql);
echo " POWER OFF to ON\n";
}
}
$sql = "SELECT `POWER` FROM `VolleyTest` WHERE ID = 1";
$result = mysqli_query($conn, $sql);
while ($row = mysqli_fetch_row($result)) {
$POWER = $row[0];
echo "$row[0]\n";
}
echo $POWER; //. " ". $pressed . " " . $_POST['key'];

最佳答案

您不需要在单引号字符串中转义双引号。

这样做会生成这样的字符串

UPDATE `VolleyTest` SET `POWER`=\"ON\" WHERE ID = 1

所以这可能行得通

$POWER = "";
if ($pressed == "1") {
echo "pressed\n";
$sql = "SELECT `POWER` FROM `VolleyTest` WHERE ID = 1";
$result = mysqli_query($conn, $sql);
while ($row = mysqli_fetch_row($result)) {
$POWER = $row[0];
echo "pressed power request $row[0]\n";
}
if ($POWER == "ON") {
$sql = 'UPDATE `VolleyTest` SET `POWER`="OFF" WHERE ID = 1';
$conn->query($sql);
echo " POWER ON to OFF\n";
} else if ($POWER == "OFF") {
$sql = 'UPDATE `VolleyTest` SET `POWER`="ON" WHERE ID = 1';
$conn->query($sql);
echo " POWER OFF to ON\n";
}
}
$sql = "SELECT `POWER` FROM `VolleyTest` WHERE ID = 1";
$result = mysqli_query($conn, $sql);
while ($row = mysqli_fetch_row($result)) {
$POWER = $row[0];
echo "$row[0]\n";
}
echo $POWER; //. " ". $pressed . " " . $_POST['key'];

关于php - 如何从数据库中读取数据更改它们并更新数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57811356/

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