gpt4 book ai didi

While 循环中的 PHP 更新

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

我已经广泛搜索了如何完成这样的事情,但没有结果。

我想要的是在按下按钮时将单行更新为 1,而不是更新每一行。

PHP:

if(isset($_GET['updateValidation'])) {
$stmt5 = $DB_con->prepare("UPDATE comment_imgs SET validation=1 WHERE id=".$id."");
$stmt5->execute();
}

表格:

<form method="get">
<input type="submit" name="updateValidation" id="updateValidation">
<label for="updateValidation" class="btn btn-primary">Update Validation</label>
</form>

..以及我如何定义 $id:

while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
$id = $row['id'];

编辑:请原谅我乱七八糟的代码。

    <body>
<div class="container">
<div class="page-header">
<h1>View Uploaded Projects <small>Admin backend</small> </h1>
</div>
<?php include("dbconfig.php");?>
<div class="panel panel-default">
<div class="panel-body">
<div class="formlayout">
<h1><small>Sort by:</small></h1>
<form method="get" class="form-inline">
<input id="sortDate" name="sortDate" type="submit">
<label for="sortDate" class="btn btn-outline-info" id="sortDateCSS">Date</label><br>
<input id="sortProject" name="sortProject" type="submit">
<label for="sortProject" class="btn btn-outline-info" id="sortProjectCSS">Project</label><br>
<input id="sortUser" name="sortUser" type="submit">
<label for="sortUser" class="btn btn-outline-info" id="sortUserCSS">User</label><br>
</form>
</div>
<?php

/*include("class.user.php");*/
$user_id = $_SESSION['user_session'];
$user_name = $_SESSION['user_name'];

/* Laddar sidan med nyaste datum först */
$stmt = $DB_con->prepare("SELECT * FROM comment_imgs ORDER BY date DESC");
$stmt->execute();

/* Sortera projekt */
if(isset($_GET['sortDate'])){
$stmt = $DB_con->prepare("SELECT * FROM comment_imgs ORDER BY date DESC");
$stmt->execute();
}

if(isset($_GET['sortProject'])){
$stmt = $DB_con->prepare("SELECT * FROM comment_imgs ORDER BY project_id DESC");
$stmt->execute();
}

if(isset($_GET['sortUser'])){
$stmt = $DB_con->prepare("SELECT * FROM comment_imgs ORDER BY user_id DESC");
$stmt->execute();
}
/* Sortering slut */


/* Loop för Card Display / Modals */
while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
$id = $row['id'];
$date = $row['date'];
$comment = $row['comment'];
$project_id = $row['project_id'];
$user_name = $row['user_name'];
$validation = $row['validation'];
?>

<!-- Card Display -->
<div class="card" style="width: 18rem;" id="display">
<div class="card-body">
<h5 class="card-title"><?php echo $project_id;?></h5>
<p class="card-text"><?php echo $date;?></p>
<p class="card-text"><?php echo $user_name;?></p>
<p class="card-text"><?php echo $comment;?></p>


<!-- Button trigger modal -->
<button type="button" class="btn btn-primary btn-block" data-toggle="modal" data-target="#exampleModal<?php echo $id;?>" id="formButtons">
Button
</button>


<!-- Modal -->
<div class="modal fade" id="exampleModal<?php echo $id;?>" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModal<?php echo $id;?>Label"><?php echo $project_id;?></h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
<?php echo $date;?>
<?php echo $comment;?>
<?php echo $user_name;?>
<form method="get">
<input type="submit" name="updateValidation" id="updateValidation">
<label for="updateValidation" class="btn btn-primary">Update Validation</label>
</form>
<?php
if(isset($_GET['updateValidation'])) {
$stmt5 = $DB_con->prepare("UPDATE comment_imgs SET validation=1 WHERE id=".$id."");
$stmt5->execute();
}
?>
<?php if ($validation == 1) {
echo "Avklarad";
} else {
echo "WIP";
}?>
<?php

$stmt2 = $DB_con->prepare("SELECT image_path, image_name, display_id FROM uploads LEFT JOIN comment_imgs ON uploads.display_id = comment_imgs.project_id");
$stmt2->execute();

while ($row2 = $stmt2->fetch(PDO::FETCH_ASSOC))
{
$image_path = $row2["image_path"]."/".$row2["image_name"];
$display_id = $row2['display_id'];

?><?php if ($project_id == $display_id){?>
<a href="<?php echo $image_path; ?>"><img src="<?php echo $image_path; ?>" class="images" /></a><?php }} ?>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
</div>
</div>
<?php
}
?>
</div>

<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="js/jQuery.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="js/bootstrap.min.js"></script>
</body>

最佳答案

您不需要运行 while 循环来使用唯一 ID 更新每条记录。在 MySQL 中,Update语句更新满足 where 子句的所有行。将其放入 while 循环中是多余且低效的。您只需运行更新一次即可更新包含 id 字段的所有行。

您还应该明白,如果字符串位于 while 循环之外,它会将字符串设置为当时 $id 的值,并在循环内部使用它。也许用未分解的代码更新您的问题

关于While 循环中的 PHP 更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49179661/

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