开发者

How do I insert data into my mySql database using parameters in a URL?

开发者 https://www.devze.com 2023-03-12 20:14 出处:网络
I am n开发者_如何学Pythonew to PHP and databases, here is my PHP code: <?php $email = $_POST[\'Email\'];

I am n开发者_如何学Pythonew to PHP and databases, here is my PHP code:

<?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"; 
?>

and this works when I change $email and all of the variables to a set value, like $email = 'bob@aol.com', $tc = 3, $tr = 4, but when I try to set it by calling a url (mysite.com/myphp.php?$email='blah@aol.com'&$tc=4&$tr=2) it does not work, please show a working way to set the parameters from the url.

Thanks in advance!


If you want the parameters from the URL, switch $_POST to $_GET.

Also, you will need to use mysql_real_escape_string() with those outside strings, otherwise you have a SQL injection vulnerability.

Even better, because you are new to PHP and databases, learn it the best way from the start. Drop mysql_*() and use PDO.


For URl, you have to change $_POST to $_GET.

0

精彩评论

暂无评论...
验证码 换一张
取 消