如何使用URL中的参数将数据插入到mySql数据库中?

问题描述:

我是新来的PHP和数据库,这是我的PHP代码:如何使用URL中的参数将数据插入到mySql数据库中?

<?php 

$email = $_POST['Email']; 
$tc = $_POST['TotalCash']; 
$tr = $_POST['TotalReferalls']; 

// Connects to your Database 
mysql_connect("205.178.146.92", "user", "pass") or die(mysql_error()); 
mysql_select_db("db") or die(mysql_error()); 
mysql_query("INSERT INTO UserInformation(Email, TotalCash, TotalReferalls) VALUES('$email', '$tc', '$tr')"); 
Print "Your table has been populated"; 
?> 

这个工程时,我改变$email和所有的变量设定值,如$email = '[email protected]'$tc = 3$tr = 4,但是当我尝试通过调用URL来设置它(mysite.com/myphp.php?$email='[email protected]'& $ tc = 4 & $ tr = 2)它不起作用,请显示一种工作方式来设置来自url的参数。

在此先感谢!

如果您需要来自URL的参数,请将$_POST切换为$_GET

此外,您需要将mysql_real_escape_string()与外部字符串一起使用,否则您有SQL注入漏洞。

更好,因为你是PHP和数据库的新手,从一开始就学习它是最好的方法。掉落mysql_*()并使用PDO

+0

非常感谢! – Paluter 2011-06-15 02:32:39

对于URl,您必须将$ _POST更改为$ _GET。

+0

也谢谢你!!!! – Paluter 2011-06-15 02:33:41