[英]PHP not Inserting into MySQL database all statements are there
I was wondering if anyone had input as to why this statement isn't inserting into my MySQL database. 我想知道是否有人输入了为什么此语句未插入到我的MySQL数据库中。 It's not showing any errors and when I enter the SQL statement in manually it inserts the info.
它没有显示任何错误,当我手动输入SQL语句时,它会插入信息。
<?php
$host="mysql16.000webhost.com";
$user_name="a1611480_akaash";
$pwd="*****";
$database_name="a1611480_akaash";
$db=mysql_connect($host, $user_name, $pwd);
$sql = "INSERT INTO mydata VALUES ('dude1', 'dude2', 'dude3', 'dude4', 'dude5')";
mysql_query($sql);
?>
This is due to the fact that mysql does not know which database to use for this SQL statement. 这是由于mysql不知道该SQL语句使用哪个数据库。 Include
mysql_select_db
. 包括
mysql_select_db
。
mysql_select_db($database_name);
To get any type of error in php (except fatals) enclose your code with a try block 要在php中获取任何类型的错误(致命错误除外),请使用try块将代码括起来
try{
// db code
}catch(Exception $e){
// something is wrong
echo "Oh God! I got this ". $e->getMessage();
}
To see the error do this: 要查看错误,请执行以下操作:
mysql_query($sql) or die("Error:".mysql_error());
And from your query i am assuming that you have one column and you want to add multiple values So this maybe the format: 从您的查询中,我假设您只有一列,并且想要添加多个值,所以这可能是以下格式:
$sql = "INSERT INTO mydata VALUES
('dude1'), ('dude2'), ('dude3'), ('dude4'), ('dude5);";
That's because you don't mention the column names - see http://www.w3schools.com/php/php_mysql_insert.asp Also you forgot to select the database - mysql_select_db("my_db"); 那是因为您没有提到列名-请参见http://www.w3schools.com/php/php_mysql_insert.asp另外您还忘记了选择数据库-mysql_select_db(“ my_db”);
So your query would have to be something like "INSERT INTO mydata (column1, column2, column3, column4, column5) VALUES ('dude1', 'dude2', 'dude3', 'dude4', 'dude5')"; 因此,您的查询必须类似于“ INSERT INTO mydata(column1,column2,column3,column4,column5)VALUES('dude1','dude2','dude3','dude4','dude5')”。
Edit: Of course Corey is right. 编辑:当然,科里是对的。 It's just a better practice I think - I always do it :)
我认为这是更好的做法-我总是这样做:)
You are connecting to a remote host, are you sure you have the rights to do so? 您正在连接到远程主机,您确定您有权这样做吗? Where is this code executed?
该代码在哪里执行?
Outputting the result of mysql_error()
would be useful! 输出
mysql_error()
的结果将很有用!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.