【发布时间】:2023-04-10 05:17:01
【问题描述】:
我正在尝试做一个小项目。我的任务是使用 HTML 和 PHP 创建更新表单。但我收到以下错误:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 's standard dummy text ever since the 1500s, when an unknown printer.' , exp_time' at line 1
我将 Laragon 用于 php,将 HeidiSQL 9.5 用于 mysql 服务器。
我的数据库连接正常。我可以使用同一文件中的 SELECT 查询从数据库中获取数据。我认为我的代码有问题。所以请帮帮我,代码如下:
<?php
require('auth.php');
require('db.php');
$id=$_REQUEST['id'];
$query = "SELECT * FROM experience where expid='".$id."'";
$result = mysqli_query($con,$query) or die ( mysqli_error($con));
$row = mysqli_fetch_assoc($result);
$status = "";
if(isset($_POST['new']) && $_POST['new']==1)
{
$exp_title = $_REQUEST['exp_title'];
$exp_description = $_REQUEST['exp_description'];
$exp_time = $_REQUEST['exp_time'];
$update="UPDATE experience SET exp_title='".$exp_title."' , exp_description='".$exp_description."' , exp_time='".$exp_time."'
WHERE expid='".$id."'";
mysqli_query($con, $update) or die ( mysqli_error($con));
$status = "Record Updated Successfully. </br></br>
<a href='dashboard.php'>View Updated Record</a>";
echo '<p style="color:#FF0000;">'.$status.'</p>';
}else {
?>
【问题讨论】:
-
您的变量值中包含引号,因此您最终会得到像
UPDATE table SET column='my value's' ...这样无效的 SQL。使用带参数的预处理语句。 -
您的脚本对SQL Injection Attack 甚至if you are escaping inputs, its not safe! 都是开放的,在
MYSQLI_或PDOAPI 中使用prepared parameterized statements -
另外请停止使用
$_REQUEST。对正在发出的请求使用相关的超全局变量。