dongmubei7950 2016-12-30 18:58
浏览 32
已采纳

使用PHP更新MySQL条目

I have a MySQL database with a table which has 4 columns (id, tvDate, tvCourse, tvRoom)

I have created a php page with connects to the database and returns the rows of the database table in an HTML table. I have added an edit link on each row to be able to edit the entries. The link calls up a php file with the row id (eg: edit.php?id=1) and shows the current content which you can then change.

Screenshot

Everything up to this point works fine but my problem is that when you change the content and click the update button I get the following error:

You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'WHERE id=1' at line 638

(same url edit.php?id=1) and the record does not get updated.

This is my code:

<?php include('includes/database.php'); ?>
<?php
    //Assign get variable
    $id = $_GET['id'];

    //Create select query
    $query ="SELECT * FROM tvdbase
             WHERE id = $id";
    $result = $mysqli->query($query) or die($mysqli->error.__LINE__);
    if($result = $mysqli->query($query)){
        //Fetch object array
        while($row = $result->fetch_assoc()) {
            $tvDate = $row['tvDate'];
            $tvCourse = $row['tvCourse'];
            $tvRoom = $row['tvRoom'];
        }
        $result->close();
    }
?>
<?php
    if($_POST){
        //Assign get variable
        $id = $_GET['id'];

        //Assign Variables
        $tvDate = mysql_real_escape_string($_POST['tvDate']);
        $tvCourse = mysql_real_escape_string($_POST['tvCourse']);
        $tvRoom = mysql_real_escape_string($_POST['tvRoom']);

        //Create update
        $query = "UPDATE tvdbase
                  SET
                  tvDate='$tvDate',
                  tvCourse='$tvCourse',
                  tvRoom='$tvRoom',
                  WHERE id=$id
                  ";
        $mysqli->query($query) or die($mysqli->error.__LINE__);
        $msg="Updated";
        header('Location:index.php?msg='.urlencode($msg).'');
        exit;
    }

?>
<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Edit Page</title>
  </head>
  <body>

         <form role="form" method="post" action="new.php?id=<?php echo $id; ?>">
                <label>Date</label>
                <input name="tvDate" type="text" value="<?php echo $tvDate; ?>" placeholder="Enter Date">
                <label>Course</label> 
                <input name="tvCourse" type="text" value="<?php echo $tvCourse; ?>" placeholder="Enter Course">
                <label>Room</label>
                <input name="tvRoom" type="text" value="<?php echo $tvRoom; ?>" placeholder="Enter Room">
            <input type="submit" value="Update Room" />
        </form>


  </body>
</html>

I suspect my problem is in the POST method somewhere. I'm still fairly new to PHP and mySQL so I appologise in advance for any bad coding :)

  • 写回答

2条回答 默认 最新

  • dtgr6303 2016-12-30 19:24
    关注

    Here are the problems with your code.

    You're mixing mysql_ functions with the MySQLi_ API and they do not intermix.

    This being all instances of mysql_real_escape_string() which need to be replaced with mysqli_real_escape_string($mysqli, $_POST['var']).

    • Use a prepared statement instead, they're much safer.

    Then you have a trailing comma in the UPDATE query's WHERE clause:

    tvRoom='$tvRoom',
    

    in here:

    UPDATE tvdbase
      SET
      tvDate='$tvDate',
      tvCourse='$tvCourse',
      tvRoom='$tvRoom', <<< right there, remove it
      WHERE id=$id
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题