【发布时间】:2015-07-25 04:37:25
【问题描述】:
我编写了一个 php 脚本,该脚本将根据某些结果生成一个 pdf 文件。我的选择语句需要使用 where 条件进行过滤。基本上,我想测试我的 php 脚本,看看它是否运行良好。如何在链接本身中分配 where 条件? 这是我的 php 代码:
$query = "
SELECT
user_id
FROM TahdirUsers
WHERE
username = ':username'
";
$query_params = array(
':username' => $_POST['username']
);
try
{
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch (PDOException $ex)
{
$response["success"] = 0;
$response["message"] = "Database Error1. Please Try Again!";
die(json_encode($response));
}
$row = $stmt->fetch();
if ($row)
{
if ($_POST['password'] === $row['password'])
{
$response["success"] = 1;
$response["message"] = "Login successful!";
die(json_encode($response));
}
else
{
$response["success"] = 0;
$response["message"] = "Invalid password!";
$pdf = new PDF(PDF_PAGE_ORIENTATION,PDF_UNIT,PDF_PAGE_FORMAT,true, 'UTF-8', true);
$pdf->SetFont('aefurat', '', 12, '', true);
// The first Parameter is localhost again unless you are retrieving data from a different server.
// The second parameter is your MySQL User ID.
// The third parameter is your password for MySQL. In many cases these would be the same as your OS ID and Password.
// The fourth parameter is the Database you'd like to run the report on.
$pdf->connect('xxxxxxxxx','xxxxxxx','xxxxxxxxx','xxxxxxxx');
// This is the title of the Report generated.
$attr=array('titleFontSize'=>24,'titleText'=>'THIS IS MY PDF FILE');
// This is your query. It should be a 'SELECT' query.
// Reports are run over 'SELECT' querires generally.
$pdf->mysql_report($query,false,$attr);
$pdf->Output('htmlout.pdf', 'I');
die(json_encode($response));
}
}
else
{
$response["success"] = 0;
$response["message"] = "this username in not in our database!";
die(json_encode($response));
}
澄清我的问题。如果我的网站链接是www.testtesttest.com,如何实现URL中select语句的where条件
【问题讨论】:
-
您将您对 $_POST["username"] 的引用替换为 $_GET["username"] 并将您的 url 更改为 www.testtesttest.com/path/to/php/file?username=一些用户名
-
或者,您可以使用客户端 http 实用程序,例如 curl,它允许您指定帖子正文并使用它来针对相关 url 执行帖子
-
我把 $_POST['username'] 换成了 $_GET['username'],,, 还是不行
-
它总是告诉我用户名不在数据库中。
-
这看起来你只需要做一些基本的调试:分解问题,一步一步地解决。 “如何从 URL 获取数据?”的答案很简单——在查询字符串上写入
?username=foo,然后访问$_GET['username']。使用简单的var_dump($_GET['username'])进行测试。一旦你有了它,找出如何将该变量放入查询中;var_dump结果,确保所有错误都可见。等等。这一切都比您每次等待我们提示您的时间要容易得多。祝你好运。 :)