到目前为止,这是我的代码......
if (isset($_POST['projectName']))
{
$projectName = $_POST['projectName'];
$dueDate = $_POST['dueDate'];
$description = $_POST['description'];
$requestor = $_POST['requestor'];
$status = $_POST['status'];
//Update into the DB
$updateSQL = mysql_query("INSERT INTO projects
(projectName,
dueDate,
description,
requestor,
status
)
VALUES
('".$projectName."','
".$dueDate."','
".$description."','
".$requestor."','
".$status."')");
/*
header('Location:manage.php?update=insertSuccess');
exit();
*/
}
除了$ status之外,每个值都会成功插入到数据库中。我在这里错过了什么吗?
答案 0 :(得分:1)
语法看起来不错但是......
$updateSQL = mysql_query("INSERT...") or die(mysql_error());
会发现这些错误。
mysql_
功能。它们不再被维护and are officially deprecated。请参阅red box?转而了解prepared statements,并使用PDO或MySQLi - this article将帮助您确定哪个。如果您选择PDO here is a good tutorial。这是让你入门的东西:
$link = mysqli_connect('localhost', 'my_user', 'my_password', 'world');
/* check connection */
if (!$link) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$stmt = mysqli_prepare($link, "INSERT INTO projects (projectName, dueDate, description, requestor, status) VALUES (?, ?, ?, ?, ?)");
mysqli_stmt_bind_param($stmt, 'sssss', $projectName, $dueDate, $description, $requestor, $status);
/* execute prepared statement */
mysqli_stmt_execute($stmt);
printf("%d Row inserted.\n", mysqli_stmt_affected_rows($stmt));
/* close statement and connection */
mysqli_stmt_close($stmt);
/* close connection */
mysqli_close($link);