PHP Mysql 查询(插入)问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15999609/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP Mysql Query (INSERT) Issue
提问by Jon
I am having an issue with a MySQL query as follows:
我在使用 MySQL 查询时遇到问题,如下所示:
My script generates this as an example query:
我的脚本将此生成为示例查询:
INSERT INTO `contacts`(`name`, `phone`, `email`, `city`, `state`, `date`) VALUES ('Test2', '123-456-7890', '[email protected]', 'mesa', 'az', '04-14-2013')
Which if I drop directly into PHPMyA, works fine. However, the PHP script I am trying to use to send the query from my website is not working and I can't get it figured out. Here it is:
如果我直接进入 PHPMyA,效果很好。但是,我试图用来从我的网站发送查询的 PHP 脚本不起作用,我无法弄清楚。这里是:
$sql = "INSERT INTO `contacts`(`name`, `phone`, `email`, `city`, `state`, `date`) VALUES ('$name', '$phone', '$email', '$city', '$state', '$date')";
mysql_query($sql);
$result = mysql_query($sql);
if($result)
{
echo("<br>Data Input OK");
}
else
{
echo("<br>Data Input Failed");
}
Nothing makes it to the MySQL DB and no PHP errors are displayed, however, if I echo $sql
I get the exact query I posted previously.
没有任何东西进入 MySQL 数据库,也没有显示 PHP 错误,但是,如果我回显,$sql
我会得到我之前发布的确切查询。
回答by 5ervant
Just remove the single line mysql_query($sql);
on your code and you will be fine.. But you should better start practicing PHP MySQLi which stands for PHP MySQL Improved, such:
只需删除mysql_query($sql);
代码中的单行就可以了..但是你最好开始练习 PHP MySQLi,它代表PHP MySQL 改进,例如:
$con = mysqli_connect($host, $user, $password, $password);
$sql = "INSERT INTO `contacts`(`name`, `phone`, `email`, `city`, `state`, `date`) VALUES ('$name', '$phone', '$email', '$city', '$state', '$date')";
$result = mysqli_query($con, $sql);
if($result) {
echo("<br>Data Input OK");
} else {
echo("<br>Data Input Failed");
}
回答by rajesh
$sql = 'INSERT INTO Table_name (`id`, `name`) VALUES ("1", "php");
回答by Ali Mohammadi
Try to use mysql_query($sql,$con);
instead of mysql_query($sql);
.
尝试使用mysql_query($sql,$con);
而不是mysql_query($sql);
.
回答by Manish Nagar
use this
用这个
"INSERT INTO `contacts`(`name`, `phone`, `email`, `city`, `state`, `date`) VALUES ('$_POST[name]', '$_POST[phone]', '$_POST[email]', '$_POST[city]', '$_POST[state]', '$_POST[date]')";
回答by dev4092
if(isset($_POST['submit']))
{
$name=$_POST['name'];
$age=$_POST['age'];
$address=$_POST['address'];
$ins="insert into table_name(`name`,`age`,`address`)values('".$name."','".$age."','".$address."')";
mysql_query($ins);
echo 'data inserted successfully';
}
回答by Harpreet
You are running mysql_query twice. Reason of the error. Try running the following code.
您正在运行 mysql_query 两次。错误原因。尝试运行以下代码。
$sql = "INSERT INTO `contacts`(`name`, `phone`, `email`, `city`, `state`, `date`) VALUES ('$name', '$phone', '$email', '$city', '$state', '$date')";
$result = mysql_query($sql) or die(mysql_error());
if($result){
echo("<br>Data Input OK");
} else{
echo("<br>Data Input Failed");
}
回答by Fabio
You are executing $sql
twice in your script wich is causing the error, please remove
您$sql
在脚本中执行了两次导致错误,请删除
mysql_query($sql);
And it will be ready to go
它会准备好去
I would also suggest to stop using mysql_query
please switch to mysqli
or PDO
我也建议停止使用mysql_query
请切换到mysqli
或PDO
回答by manojtc
Are you sure there is a valid connection (..mysql_connect())? Try using the full syntax like so..
你确定有一个有效的连接 (..mysql_connect())?尝试使用像这样的完整语法..
$conn = mysql_connect(...);
$result = mysql_query($query, $conn);
Also try forcing a commit after you execute the statement -
还尝试在执行语句后强制提交 -
$mysql_query("COMMIT", $conn);