gpt4 book ai didi

php - 每当我按下任何一个帖子按钮时,只有列表中的第一个按钮会被更新,而不是我按下的特定按钮

转载 作者:行者123 更新时间:2023-11-29 22:56:06 25 4
gpt4 key购买 nike

<?php

$sql = mysql_query("SELECT * FROM testimonial ORDER BY id DESC");
//This block grabs the whole list for viewing
while($row = mysql_fetch_array($sql)){
$id = $row['id'];
$name = $row['name'];
$comment = $row['comment'];
$visibility = $row['visibility'];
$date_added = strftime("%b %d, %Y", strtotime($row['date_added']));}

This section is for buttons if i press the button named update it should update the field named "visibility" to approved and if i press the button named update1 it should update the field named "visibility" to hide

if(isset($_POST['update']))
{
$a = addslashes(strip_tags($_POST['update']));
$selected = mysql_query("SELECT * FROM `testimonial` WHERE id = '$id'");
$info = mysql_fetch_assoc($selected);
$sql = mysql_query("UPDATE `testimonial` SET `visibility` = 'approved' WHERE id = '$id'");
}
if(isset($_POST['update1']))
{
$a = addslashes(strip_tags($_POST['update1']));
$selected = mysql_query("SELECT * FROM `testimonial` WHERE id = '$id'");
$info = mysql_fetch_assoc($selected);
$sql = mysql_query("UPDATE `testimonial` SET `visibility` = 'hide' WHERE name='$name'");
}

这一行是我开始循环的表的标题

    $sql = mysql_query("SELECT * FROM testimonial");
echo '<form onSubmit="return validateForm()" action="testimonials.php" enctype="multipart/form-data" method="post">

<div align="center">
<table width="1273" border="1" cellspacing="0" cellpadding="6">
<tr>
<th width="119" bgcolor="#333333" scope="col"><strong>ID</strong></th>
<th width="88" bgcolor="#333333" scope="col"><strong>User</strong></th>
<th width="629" bgcolor="#333333" scope="col"><div align="center"><strong>Content</strong></div></th>
<th width="107" bgcolor="#333333" scope="col"><strong>Status</strong></th>
<th width="111" bgcolor="#333333" scope="col"><strong>Date Added</strong></th>
<th width="111" bgcolor="#333333" scope="col"><strong>Action</strong></th>
<th width="111" bgcolor="#333333" scope="col"><strong>Action</strong></th>
</tr>';

此条件是,如果可见性等于已批准,则将表字段名称“可见性”更新为已发布,并且如果按下另一个按钮,则应将表字段“可见性”更新为未发布

    while($row = mysql_fetch_array($sql)){

echo'<tr>';
echo'<th width="119" scope="col">'.$row['id'].'</th>';
echo'<th width="88" scope="col">'.$row['name'].'</th>';
echo'<th width="629" scope="col"><div align="center">'.$row['comment'].'</div></th>';
echo'<th width="107" scope="col">'.$row['visibility'].'</th>';
echo'<th width="111" scope="col">'.$row['date_added'].'</th>';

$activ= $row['visibility'];
if ($activ=='approved'){ $visibility="Posted";} if ($activ=='hide'){$visibility="Not Posted";}
echo '<th width="107" scope="col">'.$visibility.'</th>';
echo'<th width="111" scope="col"><button type="submit" name="update" class="button" value='.$row['id'].'>Post</button><button type="submit" name="update1" class="button" value='.$row['id'].'>Unpost</button></th>';

echo'</tr>';

}

?>

最佳答案

第一个代码块中的 while 循环有问题。您正尝试将 ID 存储在变量 id 中。但是您会在每次迭代时覆盖 id 的值。

假设您有 10 条 ID 为 1 2 3 ... 10 的记录

在第一次迭代中,您将值 10(因为 SQL 为 DESC)存储在 id

在第二次迭代中,您将用 9 覆盖该值。

因此,迭代结束时,您的 id 将仅包含 1

这个值为 1id 是为 UPDATE 查询提供的,这就是每次更新第一行/记录时的原因。

我对你的逻辑有点困惑。

试试这个:

由于您为每个 submit 按钮分配 ID 作为值,因此在提交表单时,请尝试获取提交按钮的值,例如 $my_id = $_POST['update']

在您的 UPDATE 查询中使用此 $my_id

关于php - 每当我按下任何一个帖子按钮时,只有列表中的第一个按钮会被更新,而不是我按下的特定按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28714270/

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