是否在不使用按钮的情况下更新单个值?

时间:2018-11-28 18:05:07

标签: php html sql

我有一个清单数据库,并在表中显示该数据库,一个表用于列表中已完成的项目,另一个用于表中未完成的项目。我在未完成表的每一行上都有一个按钮,当我单击该按钮时,我希望它更新数据库并将记录发送到已完成表。

        <div class="table_undone"> 
<!--Display undone checklist items-->
    <h2>STILL TO DO</h2>
    <?php
        $sql = "SELECT * FROM checklist WHERE done = '' ORDER BY id";
        $result = $conn->query($sql);
        if ($result->num_rows > 0) {
            //display the header of the table
            echo "<table>
                    <tr>
                        <th>ITEM</th>
                        <th>DUE DATE</th>
                        <th></th>
                        <th></th>
                    </tr>";
            while ($row = $result->fetch_assoc()) {
                //display the contents of the table
                echo "<tr>
                        <td>".$row['item']."</td>
                        <td>".$row['due_date']."</td>   
                        <td>
                            <form id='complete_item' name='complete_item' action='index.php' method='get'>
                                <button class='button' type='button' name='complete' value=".row['id'].">COMPLETE</button>
                            </form>
                      </tr>";
            } 
            echo "</table>";
        } else {
            echo "0 RESULTS";
        }
    ?>
</div>

我有这个要显示表格。

    <?php
//once an item is complete send it to the completed table using the complete button
$id=$_POST['id'];
$item=$_POST['item'];
$due_date=$_POST['due_date'];
$done=$_POST['done'];
if(isset($_POST['complete'])) {
    $sql = "UPDATE checklist SET done = 'y' WHERE id=$id";
    $result = mysqli_query($conn,$sql);
    if ($sql) {
        echo "Success in updating the record!";
    } else {
        echo "Failure in updating the record!";
    }
}

?>

这将更新记录,将完成的值更改为“ y”,然后它将在正确的表中显示它,但这不起作用吗? 任何帮助,将不胜感激!

1 个答案:

答案 0 :(得分:0)

将查看代码更改为以下内容...不需要Bcoz按钮。

while ($row = $result->fetch_assoc()) {
                //display the contents of the table
                echo "<tr>
                        <td>".$row['item']."</td>
                        <td>".$row['due_date']."</td>   
                        <td><a href='update.php?id=".$row['id']."'>complete</a></td>
                      </tr>";
            } 

您的 update.php 应该类似于(您的相同代码,但有很小的变化),

<?php
//once an item is complete send it to the completed table using the complete button

   // make sure the $conn is exist

    $id=$_GET['id'];
    $sql = "UPDATE checklist SET done = 'y' WHERE id='$id'";
    $result = mysqli_query($conn,$sql);
    if ($sql) {
        echo "Success in updating the record!";
    } else {
        echo "Failure in updating the record!";
    }
}
?>