通过php向mysql发布查询字符串,而不是将链接放在地址栏中
我正在尝试用php创建一个链接,并提交该链接,以便它将其中包含的数据发布到我的mysql数据库中 如果我将此链接粘贴到我的地址栏中,然后按enter键,我将能够将记录添加到DB中,无论来自哪个网站或浏览器:通过php向mysql发布查询字符串,而不是将链接放在地址栏中,php,mysql,Php,Mysql,我正在尝试用php创建一个链接,并提交该链接,以便它将其中包含的数据发布到我的mysql数据库中 如果我将此链接粘贴到我的地址栏中,然后按enter键,我将能够将记录添加到DB中,无论来自哪个网站或浏览器: http://mysite.com/data.php?first=bob&last=smith data.php如下所示: <? include("dbinfo.inc.php"); $first = $_GET['first']; $last = $_GET['last
http://mysite.com/data.php?first=bob&last=smith
data.php如下所示:
<?
include("dbinfo.inc.php");
$first = $_GET['first'];
$last = $_GET['last'];
$query = "INSERT INTO db1 (first,last) VALUES ('$first','$last')";
mysql_query($query);
mysql_close();
?>
您的文件写入错误。这些变量与查询不匹配,您无法对其进行转义。我还使用了mysqli
,因为mysql
被折旧了。请记住,这不是一个好办法,但无论如何我会回答这个问题
include("dbinfo.inc.php");
$first= mysqli_real_escape_string($_GET['first']);
$last = mysqli_real_escape_string($_GET['last']);
$query = "INSERT INTO db1 (first,last) VALUES ('$first','$last')";
mysqli_query($connection, $query);
mysqli_close();
这就是解决方案:
<?
$url = 'http://mysite.com/data.php?first=bob&last=smith';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
//echo $result;
curl_close($ch);
?>
在mysqli中,首先传递连接资源。如果使用mysqli对象,则不需要传递它。
include("dbinfo.inc.php");
$first= mysqli_real_escape_string($_GET['first']);
$last = mysqli_real_escape_string($_GET['last']);
$query = "INSERT INTO db1 (first,last) VALUES ('$first','$last')";
mysqli_query($connection, $query);
mysqli_close();
<?
$url = 'http://mysite.com/data.php?first=bob&last=smith';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
//echo $result;
curl_close($ch);
?>