gpt4 book ai didi

php - mysql 数据库的更新逻辑问题

转载 作者:行者123 更新时间:2023-11-29 15:10:12 25 4
gpt4 key购买 nike

我有以下 PHP 表单,它回发到 mysql 数据库。我的问题是更新查询似乎有效,但总是被“checked”覆盖。我想要做的是检查从数据库中获取当前值,然后如果 post 中有值,则获取该值。现在...为什么这不起作用?检查它是否在 _POST 中时是否需要有 else 子句?如果是这种情况,我是否需要使用 $checkDeleted = ""; 初始化变量?

<?php
error_reporting(E_ALL);
if (isset($_GET["cmd"]))
$cmd = $_GET["cmd"]; else
if (isset($_POST["cmd"]))
$cmd = $_POST["cmd"]; else die("Invalid URL");
if (isset($_GET["pk"])) {
$pk = $_GET["pk"];
}
$checkDeleted = "";
$con = mysqli_connect("localhost","user","pw", "db");
$getformdata = $con->query("select ARTICLE_NO, deleted from STATUS where ARTICLE_NO = '$pk'");
while ($row = mysqli_fetch_assoc($getformdata)) {
$ARTICLE_NO = $row['ARTICLE_NO'];
$checkDeleted = $row['deleted'];
}
$checkboxes = (isset($_POST['checkboxes'])? $_POST['checkboxes'] : array());
if (in_array('deleted', $checkboxes)) $checkDeleted = 'checked';
if($cmd=="submitinfo") {
if ($ARTICLE_NO == null) {
$statusQuery = "INSERT INTO STATUS VALUES (?, ?)";
if ($statusInfo = $con->prepare($statusQuery)) {
$statusInfo->bind_param("ss", $pk, $checkDeleted);
$statusInfo->execute();
$statusInfo->close();
} else {
print_r($con->error);
}
} else if ($ARTICLE_NO == $pk) {
$statusQuery = "UPDATE STATUS SET deleted = ? WHERE ARTICLE_NO = ?";
if ($statusInfo = $con->prepare($statusQuery)) {
$statusInfo->bind_param("ss", $checkDeleted, $pk);
$statusInfo->execute();
$statusInfo->close();
} else {
print_r($con->error);
}
}
}
if($cmd=="EditStatusData") {
echo "<form name=\"statusForm\" action=\"test.php?pk=".$pk."\" method=\"post\" enctype=\"multipart/form-data\">
<input type=\"checkbox\" name=\"checkboxes[]\" value=\"deleted\" ".$checkDeleted." />
<label for=\"deleted\">Delete</label>
<input type=\"hidden\" name=\"cmd\" value=\"submitinfo\" />
<input name=\"Submit\" type=\"submit\" value=\"submit\" />
</form>";
}
?>

我尝试更改该行以将 checkDeleted 设置为以下内容,这没有什么区别......尽管应该如此?

if (in_array('deleted', $checkboxes)) {
$checkDeleted = 'checked';
} else {
$checkDeleted = '';
}

编辑:好的,我已经设法让它工作,但只有在更改为

之后

$checkDeleted = in_array('已删除', $checkboxes) ? “已检查”:“”;

按照下面的答案,但这仍然不起作用。为了让它工作,我必须删除数据库查询,并将其替换为submitinfo分支中的一个和EditStatusData分支中的一个......为什么?为什么不能只有一个查询?

if($cmd=="submitinfo") {

$getformdata = $con->query("select ARTICLE_NO from STATUS where ARTICLE_NO = '$pk'");

while ($row = mysqli_fetch_assoc($getformdata)) {
$ARTICLE_NO = $row['ARTICLE_NO'];
}
if ($ARTICLE_NO == null) { etc

if($cmd=="EditStatusData") {
$getformdata = $con->query("select deleted from STATUS where ARTICLE_NO = '$pk'");

while ($row = mysqli_fetch_assoc($getformdata)) {
$checkDeleted = $row['deleted'];
} etc

最佳答案

这与您的其他问题几乎相同

mysql not updating from php form

代码没有任何问题,它完全按照你想要的方式工作

What I want to do is get the current value from the database, and then if there is a value in post, get that instead.

情况1:不带勾的html表单

  1. 从数据库读取 $checkDeleted = 'checked'
  2. 如果 $_POST['checkboxes']['deleted'] 未设置,则保留 $checkDeleted 不变
  3. 将“已检查”写入数据库

案例2.带勾的html表单

  1. 从数据库读取 $checkDeleted = 'checked'
  2. 如果设置了 $_POST['checkboxes']['deleted'],则更改 $checkDeleted = 'checked'
  3. 将“已检查”写入数据库

所以无论你是否打勾,一旦你将数据库值改为checked,就无法更改它了

我会假设您想要做的始终是使用复选框设置的内容覆盖数据库值,在这种情况下

替换此行

 if (in_array('deleted', $checkboxes)) $checkDeleted = 'checked';

有了这个

 $checkDeleted = in_array('deleted', $checkboxes) ? 'checked' : '';

关于php - mysql 数据库的更新逻辑问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/906744/

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