如何在PHP中更新db中的行

时间:2018-03-26 10:39:32

标签: php jquery html mysql ajax

我有一张桌子和一张桌子。当用户在文本框中键入并单击“提交”时,需要在表中添加注释列,以便更新注释。我已经尝试了下面的代码,但无法理解为什么它无法正常运行。我已经对此做了很多研究,但在路障之后遇到了障碍,所以我真的希望你能帮上这个,非常感谢!

var countdown = function(value, newList, element) {
  if (value > 0) {
    console.log(value);

    newList = includesKey(newList, element)
    console.log("newList", newList);

    return countdown(value - 1, newList, element);
  } else {
    return includesKey(newList, element)
  }
};

//returns a boolean if the new list includes a filter element
function includesKey(newList, element) {
  return newList.filter((a, p, s) =>
    a.includes(element)
  )
}

//prune to a simpler list
function pruneResult(list, filterArray) {
  var container = []; //create holder

  var newList = list.split(",").map((a) => a.split('/')[1]);
  filterArray.forEach(function(element) {


    var li = countdown(2, newList, element);
    console.log("li", li)


    container.push(li);
  })

  //return a flat array - simpler list
  return container.reduce(function(prev, curr) {
    return prev.concat(curr);
  });
}


var list = "application/pdf, application/x-pdf, application/acrobat, applications/vnd.pdf, text/pdf, text/x-pdf, application/ppt, application/vnd.ms-powerpoint";
var filterArray = ["pdf", "ppt"];

console.log("list1", pruneResult(list, filterArray));


//error handling

var fileType = "image/jpeg";
var wrongTypeMss = "x,y,z";

console.log('File is incorrect type (' + fileType + ') it must be one of the following extensions ' + wrongTypeMss);

1 个答案:

答案 0 :(得分:-1)

虽然循环范围减少到仅包括表中数据的输出。虽然之前包含在while循环中,但数据库更新块现在已移至最顶层,以便更新已显示在下面的表输出中。

还更新了if语句中的条件。

<?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);
}
//-------------------------------
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" class="table table-condensed" id="example">
     <!--      <table cellpadding="0" cellspacing="0" border="0" class="table table-bordered" 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"; ?>" class="reject">reject</a>
                    <a href="tea_appview.php?app_id=<?php echo $row['app_id'] . "&" . "state=accept"; ?>" class="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>