简体   繁体   English

通过PHP将数据插入MySQL表时出错

[英]Error inserting data into MySQL Table via PHP

I have a MySQL database with a table "AllParties". 我有一个带有表“ AllParties”的MySQL数据库。 When I INSERT data into that table using PHP (below) I don't get an error, however, the data is not inserted into the table. 当我使用PHP将数据插入该表时(如下所示),我没有收到错误消息,但是该数据未插入该表中。 I tried getting an error report from the MySQL server and, rather ambiguously, it said there is an error in MySQL Syntax. 我尝试从MySQL服务器获取错误报告,但模棱两可,它说MySQL语法中有错误。 Please note I'm running my code in Mac CodeRunner, possibly this is the problem? 请注意,我在Mac CodeRunner中运行我的代码,可能这是问题所在吗? Also, $con is a successful connection. 另外,$ 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")');
?>

Any column name with spaces in it, and naming columns like that is a great way to make developers very upset, needs to be escaped: 任何带有空格的列名以及类似的命名列都是使开发人员非常沮丧的好方法,需要转义:

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

The backticks, not regular quotes, are used for escaping database, table and column names. 反引号(不是常规引号)用于转义数据库,表和列的名称。 Regular quotes ' and " are used for strings. 正则引号'"用于字符串。

<?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} will eval the x string {$ x}将评估x字符串

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM