PHP更新功能无法通过ID正确更新字段

时间:2019-01-15 11:03:14

标签: php html mysql

我正在尝试使用此查询更新表

                                      $query = "UPDATE Events SET ";
                                      $query .="name = '{$f_tit}', ";
                                      $query .="role = '{$f_stat}', ";
                                      $query .= "WHERE id = {$the_feedback_id}";

下面是完整的代码,但是我遇到了上面的错误,我找不到代码的问题。

有两个正在使用的类editfeedback.php和reviews.php,在reviews中,我单击HTML表格上的编辑,然后打开带有相应ID的更新表单。

基本上,我想要的是,当用户单击查看所有反馈项然后编辑其中一项时,它会打开一个包含当前详细信息的新表单,然后可以对其进行编辑和保存,但是当我保存时,我得到了错误如下所示。

REview.php

//only section of code shown                        
                             while($row = mysqli_fetch_assoc($select_feedback)){
                              $fid = $row['id'];
                              $name = $row['name'];   
                              $role = $row['role'];
                              $feedback = $row['feedback'];


        //display          
                              echo "<tr>";
                              echo "<td>$fid</td>";
                              echo "<td>$name</td>";
                              echo "<td>$role</td>";
                              echo "<td>$feedback</td>";

//f_id is the linking field to the editfeedback.php
                              echo "<td><a href='editfeedbackpage.php?source=editfeedback&f_id={$fid}'>Edit</a></td>";

                              echo "</tr>";

                                   }    

                                ?>

                        </tbody>
                    </table>

EDITFEEDACK.php

                //the update function

                    <?php
             //gets the id from the reviews page on the edit button when clicked                       
                    if(isset($_GET['f_id'])){
                        $the_feedback_id = $_GET['f_id'];
                    }

            // where I am having the problem
                                   $query = "SELECT * FROM feedback WHERE id = $the_feedback_id";
                                   $select_feedback_by_id = mysqli_query($connection, $query);      


//while loop                 
                                   while($row = mysqli_fetch_assoc($select_feedback_by_id)){

                                    $f_status = $row['role'];
                                    $f_title = $row['name'];   

                                   }
       //using post for button
                                   if(isset($_POST['update_feedback'])){

                                      $f_tit = $_POST['name'];
                                      $f_stat =  $_POST['role'];

        //the update query 

                                        $query = "UPDATE Events SET ";
                                      $query .="name = '{$f_tit}', ";
                                      $query .="role = '{$f_stat}', ";

            //I think it's having the problem here possible its the variable feedback id causing the problem
                                      $query .= "WHERE id = {$the_feedback_id}";
                                      echo $query;

                                      $update_feedback = mysqli_query($connection, $query);
                                      QueryConfirm($update_feedback);
                                   }

                    ?>

    //the update form 
                    <form action="" method="post" enctype="multipart/form-data">

                        <div class="form-group">
                            <label for="name">name</label>
                            <input value="<?php echo $f_title; ?>" type="text" class="form-control" name="name">
                        </div>


                        <div class="form-group">
                            <label for="role">role</label>
                            <input value="<?php echo $f_status; ?>"type="text" class="form-control" name="role">
                        </div>


                        <div class="form-group">
                            <input class="btn btn-primary" type="submit" name="update_feedback" value="Update Feedback">
                        </div>

                    </form>

这是我遇到的错误

  

查询失败。您的SQL语法有误;查看手册   对应于您的MySQL服务器版本的正确语法,   在第1行的“ WHERE id = 5”附近使用

2 个答案:

答案 0 :(得分:1)

从此处删除最后一个逗号

$query .="role = '{$f_stat}', ";

答案 1 :(得分:0)

就像错误消息所示,您的SQL语句中有错误。在“位置”之前,您有一个冒号。将您的声明更改为此

UPDATE Events SET name = '{$f_tit}', role = '{$f_stat}' WHERE id = {$the_feedback_id}

应该这样做:)