jquery - how to update a row in db in PHP

133

I have a table & need to add a comments column in the table where comments could be updated when the user types in the text box and click submit. I have tried the code below but cant understands why it does not function correctly. I've done a lot of research over this but hit a roadblock after road block, so I really hope your help in this, many thanks!

<form method="post" action="tea_appview.php">
<table cellpadding="0" cellspacing="0" border="0" id="example">
<!--   <table cellpadding="0" cellspacing="0" border="0" id="example"> -->
  <thead>
    <tr>
      <th>appoinment ID</th>
      <th>Date</th>
      <th>time</th>
      <th>subject</th>
      <th>Appointment from [parent]</th>
      <th>Appointment to (teacher) </th>
      <th> accept/reject </th>
      <th>state</th>
      <th>comm</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(mysqli_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'];
      $sub = $row['sub'];
  ?>
    <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['sub'] ?></td>
      <td><?php echo $row['p_username'] ?></td>
      <td><?php echo $row['username'] ?></td>
      <td>
        <a href="tea_appview.php?app_id=<?php echo $row['app_id'] . "&" . "state=reject"; ?>" >reject</a>
        <a href="tea_appview.php?app_id=<?php echo $row['app_id'] . "&" . "state=accept"; ?>" >accept</a> 
      </td>
      <td><?php echo $row['state'] ?></td>
      <td><input type="text" name="comm">
        <input type="submit" name="submit" value="submit">
      </td>
    </tr>
    <?php
      //---------PROBLEM IS HERE
247

Answer

--- //if(isset($_GET['app_id'], $_POST['submit'])!="") if (isset($_GET['app_id']) && $_POST['submit'] != ""){ $stmt = mysqli_prepare($conn, "UPDATE app SET comm = ? WHERE app_id = ?"); mysqli_stmt_bind_param($stmt, "sd", $_GET['comm'], $_GET['app_id']); $stmt->execute(); $stmt->close(); } //
920

Answer

- 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']); $stmt->execute(); $stmt->close(); } } ?> </tbody> </table>
611

Answer

Solution:

While loop scope is reduced to just include outputing of data in table. While, previously included in the while loop, database update block is now moved to the very top so that updates will already show in the table output below.

Also updated the conditions in the if statements.

<?php

if (isset($_POST['app_id']) && $_POST['app_id'] != "" && isset($_POST['submit'])) {
    $stmt = mysqli_prepare($conn, "UPDATE app SET comm = ? WHERE app_id = ?");
    mysqli_stmt_bind_param($stmt, "si", $_POST['comm'], $_POST['app_id']);
    mysqli_stmt_execute($stmt);
    mysqli_stmt_close($stmt);
}
//
487

Answer

- if (isset($_GET['state'], $_GET['app_id'])) { $stmt = mysqli_prepare($conn, "UPDATE app SET state = ? WHERE app_id = ?"); mysqli_stmt_bind_param($stmt, "si", $_GET['state'], $_GET['app_id']); mysqli_stmt_execute($stmt); mysqli_stmt_close($stmt); } ?> <table cellpadding="0" cellspacing="0" border="0" id="example"> <!-- <table cellpadding="0" cellspacing="0" border="0" id="example"> --> <thead> <tr> <th>appoinment ID</th> <th>Date</th> <th>time</th> <th>subject</th> <th>Appointment from [parent]</th> <th>Appointment to (teacher) </th> <th> accept/reject </th> <th>state</th> <th>comm</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(mysqli_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']; $sub = $row['sub']; ?> <form method="post" action="tea_appview.php"> <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['sub'] ?></td> <td><?php echo $row['p_username'] ?></td> <td><?php echo $row['username'] ?></td> <td> <a href="tea_appview.php?app_id=<?php echo $row['app_id'] . "&" . "state=reject"; ?>" >reject</a> <a href="tea_appview.php?app_id=<?php echo $row['app_id'] . "&" . "state=accept"; ?>" >accept</a> </td> <td><?php echo $row['state'] ?></td> <td><input type="text" name="comm" value="<?php echo $row['comm']; ?>"><input type="hidden" name="app_id" value="<?php echo $row['app_id']; ?>"> <input type="submit" name="submit" value="submit"> </td> </tr> </form> <?php } ?> </tbody> </table>

People are also looking for solutions to the problem: php - Laravel app storage images failed to load and redirect to 404

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.