如何在PHP中将接受/拒绝插入到db中

时间:2018-03-02 02:24:14

标签: javascript php jquery html mysql

我创建了一个约会表,其中user1将添加约会,user2将接受/拒绝约会。 现在我的问题是当点击接受和拒绝时它分别显示在表中但我不能理解如何将插入查询添加到其中以便它可以插入到数据库中。我尝试了几种方法试图将状态[接受/拒绝]插入数据库,但我没有找到成功。我会请求有人帮我解决这个问题。感谢。

app (apportionment db)

 <form method="post" action="delete.php" > 

       <table cellpadding="0" cellspacing="0" border="0" class="table table-condensed" id="example">
               <thead>
                          <tr>
                                <th>appoinment ID</th>
                              <th>Date</th>
                                <th>time</th>
                              <th>teacher</th>
                             <th>parent</th>
                              <th> accept/reject </th>
                              <th>label</th>
                            </tr>
                   </thead>
                   <tbody>
                        <?php 
                            $query=mysqli_query($conn, "select * from `app` left join `par` on par.par_id=app.par_id
                            left join `tea` on tea.tea_id=app.tea_id
                            ORDER BY app_id DESC");

                                if($query === false)
                                {
                                    throw new Exception(mysql_error($conn));
                                }
                                while($row=mysqli_fetch_array($query))
                                {
                                    $ann_id=$row['app_id'];
                                    $date=$row['date'];
                                    $msg=$row['time'];

                                    $username = $row['username'];
                                     $username = $row['p_username'];
                        ?>
                <tr>
                    <td><?php echo $row['app_id'] ?></td>
                     <td> <?php echo date('j/m/y',strtotime($row['date'])); ?></td>
                    <td><?php echo $row['time'] ?></td>
                   <td><?php echo $row['p_username'] ?></td>
                <td><?php echo $row['username'] ?></td>

                    <td>
        <a href="#" class="reject">reject</a> 
        <a href="#" class="accept">accept</a>
    </td>
    <td>
        <div class="chgtext">PENDING</div>
    </td>

                </tr>

                        <?php   
                                }

                       ?>

                  </tbody>
              </table>
         </div>
      </form>

1 个答案:

答案 0 :(得分:1)

一种方法是通过查询字符串(GET)传递ID。因此,如果给出了适当的查询字符串键对,您将更新约会。

就个人而言,我相信你不应该在输出时混合查询。首先将数据库放在顶部,然后将输出保留在底部。

注意:我对mysqli_不太熟悉,但它会是这样的:

<?php 
// accept or reject appointment with ID
if (isset($_GET['state'], $_GET['app_id'])) {
    $stmt = mysqli_prepare($conn, "UPDATE app SET state = ? WHERE app_id = ?");
    mysqli_stmt_bind_param($stmt, "sd", $_GET['state'], $_GET['app_id']);
    mysqli_stmt_execute($stmt);
    mysqli_stmt_close($stmt);
}

// query appointments
$query = mysqli_query($conn, "
    SELECT * FROM app 
    LEFT JOIN par ON par.par_id = app.par_id
    LEFT JOIN tea on tea.tea_id = app.tea_id
    ORDER BY app_id DESC
");
?>

<form method="post" action="delete.php" > 
    <table cellpadding="0" cellspacing="0" border="0" class="table table-condensed" id="example">
        <thead>
            <tr>
                <th>appoinment ID</th>
                <th>Date</th>
                <th>time</th>
                <th>teacher</th>
                <th>parent</th>
                <th>accept/reject</th>
                <th>label</th>
            </tr>
        </thead>
        <tbody>
        <?php while($row = mysqli_fetch_array($query)) : ?>
            <tr>
                <td><?= $row['app_id'] ?></td>
                <td><?= date('j/m/y', strtotime($row['date'])) ?></td>
                <td><?= $row['time'] ?></td>
                <td><?= $row['p_username'] ?></td>
                <td><?= $row['username'] ?></td>
                <td>
                    <!-- upon clicking a link, it will redirect to the same page with a query string -->
                    <a href="?state=reject&app_id=<?= $row['app_id'] ?>" class="reject">reject</a> 
                    <a href="?state=accept&app_id=<?= $row['app_id'] ?>" class="accept">accept</a>
                </td>
                <td>
                    <div class="chgtext">PENDING</div>
                </td>
            </tr>
        <?php endwhile ?>
        </tbody>
    </table>
</form>