PHP MySQLi不插入表

时间:2013-03-07 17:26:50

标签: php mysql forms mysqli

我看了一些其他类似的问题,但似乎仍然无法让它发挥作用。 FWIW,我没有收到任何具体的错误信息。

<?php

// Create connection
$con=mysqli_connect("localhost","username","password");
mysqli_select_db($con, "database");
//let's start our session, so we have access to stored data
session_start();

$class = mysqli_real_escape_string($_SESSION['exclass']);
$fullname = mysqli_real_escape_string($_SESSION['fullname']);
$june = mysqli_real_escape_string($_SESSION['June']);
$july = mysqli_real_escape_string($_SESSION['July']);
$email = mysqli_real_escape_string($_POST['email']);
$phone = mysqli_real_escape_string($_POST['phone']);

//let's create the query
$insert_query = "INSERT INTO Responses (class,fullname,June,July,email,phone) VALUES ('$class','$fullname',$june,$july,'$email','$phone',)";

//let's run the query
$result = mysqli_query($con,$insert_query);

if($result){
    echo "Your response has been recorded. Thank you!";

}else{

    printf("Insert failed: %s\n", mysqli_error());
}
?>

2 个答案:

答案 0 :(得分:3)

MySQL不支持尾随逗号表示法。

因此,替换:

,'$phone',)

,'$phone')

另外,添加一个回退条件以查看错误输出缓冲区:

mysqli_query($con, $insert_query) or die(mysqli_error($con));

答案 1 :(得分:1)

"INSERT INTO Responses (class,fullname,June,July,email,phone) VALUES    ('$class','$fullname',$june,$july,'$email','$phone',<--- LOOK AT THIS COMMA)

这很可能解释了插入数据库的失败。从SQL字符串中删除逗号,它将正确解析。