谁能发现为什么这个mysql_query没有插入到DB中

时间:2013-01-08 21:24:35

标签: php mysql

到目前为止,这是我的代码......

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之外,每个值都会成功插入到数据库中。我在这里错过了什么吗?

1 个答案:

答案 0 :(得分:1)

语法看起来不错但是......

  • 您没有捕获错误,因此它可能是某种列约束。
$updateSQL = mysql_query("INSERT...") or die(mysql_error());

会发现这些错误。

这是让你入门的东西:

$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);