0

我有一个带有“AllParties”表的 MySQL 数据库。当我使用 PHP(如下)将数据插入到该表中时,我没有收到错误,但是,数据没有插入到表中。我尝试从 MySQL 服务器获取错误报告,但很模糊,它说 MySQL 语法有错误。请注意我在 Mac CodeRunner 中运行我的代码,这可能是问题所在?此外,$con 是一个成功的连接。

<?php
$con=mysqli_connect("sql3.freemysqlhosting.net","*********","********","*********");

$name = "Will's Party";
$date = 'October 1st, 2013';
$housenum = '333 East Street';
$city = "Golden Gate";
$state = "California"; 
$time = "7:00";
$tag = "Serra";

mysqli_select_db('AllParties', $con);

$alpha = mysqli_query($con, 'INSERT INTO AllParties(Party Name, Date, House number and       street name, City, State, Time, Tag)  VALUES("$name","$date","$housenum","$city","$state","$time","$tag")');
?>
4

2 回答 2

0

任何包含空格的列名,以及像这样命名列是让开发人员非常沮丧的好方法,都需要转义:

INSERT INTO AllParties (`Party Name`, ...)

反引号,而不是常规引号,用于转义数据库、表和列名。常规引号'"用于字符串。

于 2013-10-28T16:40:38.933 回答
-3
<?php
$con=mysqli_connect("sql3.freemysqlhosting.net","*********","********","*********");

$name = 'Will\'s Party';
$date = 'October 1st, 2013';
$housenum = '333 East Street';
$city = 'Golden Gate';
$state = 'California'; 
$time = '7:00';
$tag = 'Serra';

mysqli_select_db('AllParties', $con);

$alpha = mysqli_query($con, "INSERT INTO AllParties(`Party Name`, `Date`, `House   number and street name`, `City`, `State`, `Time`, `Tag`)    VALUES('{$name}','{$date}','{$housenum}','{$city}','{$state}','{$time}','{$tag}')");
?>

{$x} 将评估 x 字符串

于 2013-10-28T16:55:17.003 回答