简体   繁体   English

PHP脚本不将数据写入数据库

[英]PHP script not writing data into database

I have this code from another Stack Overflow question answered here . 在这里回答另一个Stack Overflow问题的代码。

I have a mysql table called track in my database. 我的数据库中有一个名为track的mysql表。

$server = "host";
$username = "user";
$password = "pass";
$database = "database";

$connId = mysql_connect($server,$username,$password) or die("Cannot connect to     server");
$selectDb = mysql_select_db($database,$connId) or die("Cannot connect to database");


$tracking_page_name="example";
$ref=$_SERVER['HTTP_REFERER'];
$agent=$_SERVER['HTTP_USER_AGENT'];
$ip=$_SERVER['REMOTE_ADDR'];
$host_name = gethostbyaddr($_SERVER['REMOTE_ADDR']);
$strSQL = "INSERT INTO track (tm, ref, agent, ip, tracking_page_name, host_name)         VALUES(curdate(),'$ref','$agent','$ip','$tracking_page_name','$host_name')";
$test=mysql_query($strSQL);

Basically I want all the data this script collects to be put into my database. 基本上我希望这个脚本收集的所有数据都放入我的数据库中。 However, no matter what I type, even if I change the variables to text, nothing gets written into the database. 但是,无论我键入什么,即使我将变量更改为文本,也不会将任何内容写入数据库。 I can read from the database just fine using another script, but I can't get this one to add any piece of info to the db. 我可以使用另一个脚本从数据库中读取,但是我无法通过这个来向db添加任何信息。 Is there a typo I just can't seem to find? 是否有一个我似乎无法找到的拼写错误?

I just want information about anyone who visits the page this gets included in. 我只想了解访问该网页的任何人的信息。

EDIT: I was trying to fill a field called "host_name", but in my database it had a different name. 编辑:我试图填写一个名为“host_name”的字段,但在我的数据库中它有一个不同的名称。 The original script shown here would not show the error and I did not ever run a check to see if all my fields are in order. 这里显示的原始脚本不会显示错误,我也没有检查过我的所有字段是否按顺序排列。 That was the error. 那是错误。

Many possibilities: 许多可能性:

  1. If the user user has read permissions but not write permissions on the database or the table, read ops will succeed but write ops will fail. 如果用户user具有读取权限但没有对数据库或表的写权限,则读取操作将成功,但写入操作将失败。
  2. If this is a MyISAM table, the table may be locked by another process. 如果这是MyISAM表,则该表可能被另一个进程锁定。 Read operations would work but write operations would not 读操作可以工作,但写操作不会
  3. Maybe the table or a field you're trying to write to does not exist or is misspelled 也许您尝试写入的表或字段不存在或拼写错误

Change... 更改...

$test=mysql_query($strSQL);

to... 至...

if(!$test=mysql_query($strSQL)) die(mysql_error());

It will show you why the operation failed ( docs ). 它将向您显示操作失败的原因( docs )。

Also, if you're starting don't use MySQL functions. 另外,如果你开始不使用MySQL功能。 They are being removed from PHP so you're just giving yourself more work to do in the future. 他们正在从PHP中删除,因此您将来会给自己做更多的工作。 Use MySQLi or PDO_MySQL instead ( docs ). 使用MySQLiPDO_MySQL代替( docs )。

Finally, never just insert text you got from the browser in an SQL query. 最后, 不要只是在SQL查询中插入从浏览器中获得的文本。 The values of $_SERVER['HTTP_REFERER'] and $_SERVER['HTTP_USER_AGENT'] can be manipulated by your user to be anything, including a string that causes you to execute whatever query the user wants (delete your tables, exfiltrate your data...). 您的用户可以将$_SERVER['HTTP_REFERER']$_SERVER['HTTP_USER_AGENT']值操作为任何内容,包括使您执行用户想要的任何查询的字符串(删除表格,删除数据)。 ..)。 Learn about prepared statements and parameterized queries and use them instead 了解准备好的语句参数化查询,并使用它们

echo this line: 回应这一行:

$strSQL = "INSERT INTO track (tm, ref, agent, ip, tracking_page_name, host_name)         VALUES(curdate(),'$ref','$agent','$ip','$tracking_page_name','$host_name')"

and run it on a SQL console. 并在SQL控制台上运行它。 Lookout for errors and make sure the values are being passed in. 寻找错误并确保传入值。

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

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