我是新手更新数据库(我已经跳过了一些步骤,以便我更快地做一些事情)我有这个代码,但数据库没有更新,我没有得到任何错误。
<?php
require_once '../includes/configuration.php';
if (isset($_POST['enter']))
{
$_POST['employee_id_passport'] = $employee_id_passport;
$_POST['first_name'] = $first_name;
$_POST['surname'] = $surname;
$_POST['mobile_number'] = $mobile_number;
$sql_query_update = "UPDATE employee_master
SET first_name = '$first_name',
surname = '$surname',
mobile_number = '$mobile_number'
WHERE employee_id_passport = '$employee_id_passport'";
$result = mysql_query($sql_query_update, $connection);
echo "this";
}
else
{
//display form with employee details
if (isset($_GET['idno']))
{
$employee_id_passport = $_GET['idno'];
$sql_get_employee_details_query = "SELECT * FROM employee_master WHERE employee_id_passport = '$employee_id_passport'";
$result = mysql_query($sql_get_employee_details_query, $connection);
$row = mysql_fetch_assoc($result);
echo $row['employee_id_passport'] . "<br /> " . $row['surname'] . "<br /> " . $row['first_name'] . "<br /> " . $row['mobile_number'] . "<br /> ";
echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"";
echo "\"http:www.w3.org/TR/xhthml1/DTD/xhtml1-transitional.dtd\">";
echo "<html xmls=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">";
echo "<head>";
echo "<title> </title>";
echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\" />";
echo "</head>";
echo "<body>";
echo "<form name=\"view_employee\" method=\"POST\" action=\"" . $_SERVER['PHP_SELF'] . " \" />";
echo "Employee ID/Passport: <input type=\"text\" name=\"id_passport\" disabled=\"disabled\" value=\" " . $row['employee_id_passport'] . "\" /> <br />";
echo "First Name: <input type=\"text\" name=\"first_name\" value=\" ". $row['first_name'] . "\" /> <br />";
echo "Surname: <input type=\"text\" name=\"surname\" value=\" " . $row['surname'] . "\" /> <br />";
echo "Mobile Number: <input type=\"text\" name=\"mobile_number\" value=\" " . $row['mobile_number'] . "\"/> <br />";
echo "<input type=\"submit\" value=\"enter\" name=\"submit\" />";
echo "</form>";
echo "</body>";
echo "<html>";
}
}
?>
答案 0 :(得分:4)
您没有执行更新查询。在echo "this"
行
if (false === mysql_query($sql_query_update)) {
throw new Exception(mysql_error());
}
你的脚本也容易受到SQL注入......你知道吗,我已经厌倦了喷出这一行。阅读SQL注入并使用PDO,预处理语句和参数绑定。
答案 1 :(得分:1)
看起来您已将sql查询字符串分配给变量但实际上并未运行它。
if (isset($_POST['enter']))
{
$_POST['employee_id_passport'] = $employee_id_passport;
$_POST['first_name'] = $first_name;
$_POST['surname'] = $surname;
$_POST['mobile_number'] = $mobile_number;
$sql_query_update = "UPDATE employee_master SET first_name = '$first_name', surname = '$surname', mobile_number = '$mobile_number' WHERE employee_id_passport = '$employee_id_passport'";
mysql_query( $sql_query_update ); // This line will run the update query
echo "this";
}
添加&#39; mysql_query($ sql_query_update);&#39; line应该为你执行查询。