使用PHP将数据插入mysql表[重复]

问题描述 投票:-2回答:2

这个问题在这里已有答案:

$dbc=mysql_connect('127.0.0.1', 'root', '1234','aliendatabase')
or die('Failed!');

$query = "INSERT INTO alien_abduction(first_name, last_name, when_it_happened, how_long, " .
"how_many, alien_description, what_they_did, fang_spotted, other, email) " .
"VALUES ('$first_name', '$last_name', '$when_it_happened', 
'$how_long', '$how_many', " .
"'$alien_description', '$what_they_did', '$fang_spotted', '$other', 
'$email')"; 

$result=mysql_query($query)
or die("Failed to upload!!!!");

mysql_close($dbc);

此代码无法执行$ result行(因此输出无法上传!!!!)但它能够建立连接。我已经交叉检查了表的列名和变量,看起来很好。

MySQL版本5.7

php mysql
2个回答
0
投票

请检查列的数据类型并执行以下操作以查找错误:

$ result = mysql_query($ query)或die(mysql_error($ dbc));


-2
投票

问题出在你的报价方式上。由于单引号不会用于保存变量。所以在单引号中,它们只是字符串,而不是变量。

如果你想用$variable替换为value,那么用单引号重写整个查询,如下所示

$query = 'INSERT INTO alien_abduction(first_name, last_name, when_it_happened, how_long, how_many, alien_description, what_they_did, fang_spotted, other, email) '.
 ' VALUES (' ".$first_name." ', ' ".$last_name . " ', ' ". $when_it_happened . " ', ' " .$how_long . " ', ' " . $how_many . " ', " . " " . $alien_description . " ', ' " . $what_they_did . " ', ' " . $fang_spotted . " ', ' " . $other . " ', ' " . $email . " ')' ; 

我希望能解决这个问题。

© www.soinside.com 2019 - 2024. All rights reserved.