在你想到任何东西之前,请理解我是PHP的新手,说实话,我完全依赖于堆栈溢出来构建我的简单和第一个PHP web应用程序。
我正试图从查询字符串中获取数据并将其存储在我的数据库中。我希望有人写一小段代码来检查字符串是否存在,如果它存在,将其添加到我的数据库。我正在使用wordpress,所以我认为我们可以使用$_GET和$wpdb,但我不知道语法。带querystring的url类似于:http://www.mydomain.tld/?publishid=1235ABC
根据给定的地址http://www.mydomain.tld/?publishid=1235ABC,它将像下面这样::
<?php
$input = isset($_GET['publishid']) ? $_GET['publishid'] : '';
if(!empty($input)) {
$query = "INSERT INTO table (input_field) VALUES ('$input')";
mysql_query($query);
}
?>
将检查GET变量中是否存在已发布的内容。其余的取决于您正在使用的数据库,以及如果该数据库不存在,您想要做什么。
if(!isset($_GET['publishid']))
{
//Do whatever you want if the key does not exists
}
else
{
//Insert into the database.
}
喜欢(bariz和Nobita)的答案,也喜欢这个小小的修改
<?php
if(!empty($_GET['publishid'])) {
$input = $_GET['publishid'];
$query = "INSERT INTO table (input_field) VALUES ('$input')";
mysql_query($query);
}
?>