无法使用 PHP 更新 MYSQL

Cannot Update MYSQL using PHP

我有一个简单的 MYSQL 数据库,其中 field_3 是一个 varchar 键值。我正在尝试将数据库发布更新到两个名为开始和结束的时间字段。

但是我一直收到这个错误

Notice: Undefined variable: empd_end in C:\xampp\htdocs\b1\update.php on line 25 Could not update data: 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 ':12:00, end = WHERE field_3 = Berkay_Sebat@yahoo.com' at line 1

<html>
<head>
<title>Update a Record in MySQL Database</title>
</head>
<body>

<?php
if(isset($_POST['update']))
{
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'root';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}

$emp_id = $_POST['emp_id'];
$emp_salary = $_POST['emp_salary'];
$emp_end= $_POST['emp_end'];


$sql = "UPDATE usezas ".
       "SET start = $emp_salary, end = $empd_end".
       "WHERE field_3 = $emp_id" ;

mysql_select_db('db1');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not update data: ' . mysql_error());
}
echo "Updated data successfully\n";
mysql_close($conn);
}
else
{
?>
<form method="post" action="<?php $_PHP_SELF ?>">
<table width="400" border="0" cellspacing="1" cellpadding="2">
<tr>
<td width="100">EMAIL</td>
<td><input name="emp_id" type="text" id="emp_id"></td>
</tr>
<tr>
<td width="100">Start TIME</td>
<td><input name="emp_salary" type="text" id="emp_salary"></td>
</tr>
<tr>
<td width="100">END TIME</td>
<td><input name="emp_end" type="text" id="emp_end"></td>
</tr>
<td width="100"> </td>
<td> </td>
</tr>
<tr>
<td width="100"> </td>
<td>
<input name="update" type="submit" id="update" value="Update">
</td>
</tr>
</table>
</form>
<?php
}
?>
</body>
</html>

您需要将 php 值设为 ''

$sql = "UPDATE usezas ". "SET start = '$emp_salary', end = '$empd_end'". " WHERE field_3 = '$emp_id'" ;

您还缺少 end 值后的 space,您需要用引号将变量括起来,如下面的查询。

$sql = "UPDATE usezas ".
       "SET start = '$emp_salary', end = '$empd_end' ".
       "WHERE field_3 = $emp_id" ;

但是,您的代码容易受到 SQL 注入攻击。您确实准备好您的查询并且应该使用 PDO 或 MySQLi 扩展而不是旧的 mysql_query 扩展。