简体   繁体   English

为什么我的php文件不会将数据插入到我的mysql数据库中?

[英]Why won't my php file insert data into my mysql database?

I have been having trouble getting my PHP file to insert data into my database. 我在获取PHP文件以将数据插入数据库中时遇到了麻烦。 I have gone so far as making one simple script to put data into my database and it still isn't working. 我所做的只是编写一个简单的脚本即可将数据放入数据库,但仍然无法正常工作。 I'm hoping that someone will see something that will make me feel like a dork for having missed it. 我希望有人能看到让我觉得自己很想念的东西。 The code below is what is in my simple script that isn't inserting into my database. 下面的代码是我的简单脚本中未插入数据库的内容。

<? php
$con=mysql_connect("localhost","user","password","database"

mysql_query($con,"INSERT INTO table_name (username, pass) VALUES ('bob', 'tom')");

mysql_close($con);

?>

I hope that you guys see the problem because I'm drawing a complete blank. 我希望你们能看到这个问题,因为我正在画一个完整的空白。

You're missing mysql_select_db("yourdatabase", $con); 您缺少mysql_select_db("yourdatabase", $con);

Edit: forget that, you have it in your connection, but like someone commented, you have a syntax error. 编辑:忘记了,连接中已经包含了它,但是就像有人在评论一样,您有语法错误。

You can also use mysql_error() to see what's going wrong. 您也可以使用mysql_error()查看出了什么问题。

Ps the use of the old mysql extension is highly discouraged. 强烈建议不要使用旧的mysql扩展。

IN the code that you posted, mysql_connect is missing the closing paren. 在您发布的代码中,mysql_connect缺少结束括号。 Did you check and see if the SQL returns any errors by itself? 您是否检查并查看SQL是否本身返回任何错误?

I just ran the query on my server, and it works-it inserts the row. 我只是在服务器上运行查询,并且该查询可以工作,它会插入行。 So, there's nothing wrong with your query. 因此,您的查询没有任何问题。 Are you sure you have the table set up properly? 您确定表格设置正确吗? What kind of fields are username and password in your table? 表中的用户名和密码属于哪种字段? Are you using the correct table name? 您使用的表格名称正确吗? Are you sure that you're checking in the same table that you're writing to? 您确定要签入与写入相同的表吗?

<? php
$con=mysql_connect("localhost","user","password","database"

Correct syntax and procedure is to connect,select the database.. But make sure that you have closed off your functions : 正确的语法和过程是连接,请选择数据库。。但是请确保已关闭功能:

$con = mysql_connect("host","user","password");
$Database = mysql_select_db("Database",$con);

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

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