在将数据插入MYSQL表时出现一些错误

时间:2017-08-02 12:31:13

标签: php mysql mysqli

我试图将数据插入数据库。但是我的代码给了我一个错误“你的SQL语法有错误;请查看与你的MySQL服务器版本相对应的手册,以便在第2行附近使用正确的语法',')' >” 我的代码:

$dbhost="localhost";
$dbuser="admin";
$dbpass="password";
$dbname="form-recieve";
$connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
if(mysqli_connect_errno()) {
    die("Database connection failed: ".
    mysqli_connect_errno().
    " (" . mysqli_connect_errno() . ")"
    );
}
if (isset($_POST['submit'])) {
    $Username = $_POST["user_name"];
    $Email = $_POST["email"];
    $Phone = $_POST["phone"];
    $Message = $_POST["message"];
    $Thanks = "Hi {$Username}, We will get to you as fast as we can.";
} 
$Username = mysqli_real_escape_string($connection, $Username);
$Email = mysqli_real_escape_string($connection, $Email);
$Phone = mysqli_real_escape_string($connection, $Phone);
$Message = mysqli_real_escape_string($connection, $Message);
$query = "INSERT INTO contacted (name, email, phone, message)
VALUES ('{$Username}', {$Email}, {$Phone}, '{$Message}')";
$result = mysqli_query($connection, $query);
if (!$result) {
    die("Database Query Failed" . mysqli_error($connection));
}

1 个答案:

答案 0 :(得分:4)

当您在表中编写插入或更新记录的查询时,重要的是将这些符号用于列的名称`,并且对于使用单引号的字符串值(如varchar,char,date,text ..)很重要

$query = "INSERT INTO contacted (`name`, `email`, `phone`, `message`)
VALUES ('{$Username}', '{$Email}', '{$Phone}', '{$Message}')";