【问题标题】:Whenever i pressed any of those post buttons, only the first on the list will be updated instead of the speacific button that i pressed每当我按下这些发布按钮中的任何一个时,只会更新列表中的第一个按钮,而不是我按下的特定按钮
【发布时间】:2015-02-25 08:23:53
【问题描述】:
<?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>';

    }

 ?>

【问题讨论】:

    标签: php mysql forms button while-loop


    【解决方案1】:

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

    假设,您有 10 条记录,id 为 1 2 3 ... 10

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

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

    所以迭代结束时,您的id 将只保留1

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

    我对你的逻辑有点困惑。

    试试这个:

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

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

    【讨论】:

    • 非常感谢您的帮助!终于成功了!!!!我用 $my_id = $_POST['update'] :D
    猜你喜欢
    • 2022-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 2015-04-04
    相关资源
    最近更新 更多