我已经广泛搜索了如何在没有结果的情况下完成这样的事情。
我想要的是在按下按钮时将单行更新为1,而是更新每一行。
腓:
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">×</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>
答案 0 :(得分:1)
您无需运行while循环即可使用唯一ID更新每条记录。在MySQL中,Update语句更新满足where子句的所有行。将它置于while循环中是多余且低效的。您只需运行一次更新即可使用id
字段更新所有行。
你还应该明白,如果字符串在while循环之外,它会将字符串设置为当时$id
的值,并在循环内使用它。也许使用未分解的代码更新您的问题