如何更改数据库中的布尔值

时间:2017-10-28 16:22:55

标签: javascript php html sql

我正在使用html / php / javascript编写程序。该程序的目的是为一个人创建一份工作列表(这些工作存储在数据库中)。当他们完成一项工作时,他们可以单击一个按钮,以便将工作更改为完成(或为真)。没有用户输入只是一个按钮,供用户单击以将作业标记为已完成。问题是如何更改"完成"单击特定作业按钮时数据库中的列?

<html>


<table>
    <thead>
        <tr>
            <th> Business Name</th>
            <th> Address </th>
            <th> Done </th>
        </tr>
    </thead>

    <tbody>
        <?php

            $servername = "localhost";
            $username = "root";
            $password = "";
            $dbname ="myDB";

            $conn = new mysqli($servername, $username, $password, $dbname);
            if($conn->connect_error){
            die("Connection failed: " . $conn->connect_error);
            }

            $sql = "Select * from jobs where done = '1'";
            $result = $conn->query($sql);

            while ($row = mysqli_fetch_assoc($result)):
            $businessname = $row['Business'];



        ?>

        <tr>
            <td><?php echo $row['Business']; ?> </td>
            <td><?php echo $row['Address']; ?> </td>
            <td><?php echo $row['Done']; ?> </td>
            <td> <button type = 'submit' name = 'Done' value = '{businessname}' >Accept</button></td>
        </tr>


        <?php endwhile; ?>
    </tbody>    
</table>
</html>

1 个答案:

答案 0 :(得分:0)

试试这个:

if($_POST['Done']){

global $conn;

$done = trim($_POST["Done"]);

$job_id = trim($_POST["job_id"]);

$query = "UPDATE jobs SET done='$done' WHERE business = $job_id";

$res = mysqli_query($conn, $query) or trigger_error($conn->error."[ DB ]");

    if(mysqli_affected_rows($conn) > 0){
        $_SESSION['answer'] = "<div class='success'>Updated!</div>";
        return true;
    }else{
        $_SESSION['edit_page']['res'] = "<div class='error'>Error!</div>";
        return false;
    }

}