【问题标题】:PHP assign where in the linkPHP分配链接中的位置
【发布时间】: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 结果,确保所有错误都可见。等等。这一切都比您每次等待我们提示您的时间要容易得多。祝你好运。 :)

标签: php mysql sql pdf pdo


【解决方案1】:

如果我上面的评论是错误的,而您的问题实际上是“如何将数据从 URL 传递到 PHP 脚本”,那么答案是您使用 the $_GET superglobal variable 以标准格式访问查询字符串 .../path/to/script.php?var1=value1&var2=value2...

因此 URL http://amjad-test.site40.net/arabictest.php?username=imsop 将执行“arabictest.php”并使用值'imsop' 填充变量$_GET['username']。然后,您可以在代码中任意位置使用该变量。

【讨论】:

  • 谢谢。您的回答帮助我修复代码。我使用了“分而治之”的规则。我将代码分成几部分。测试了每一个。把它们组装起来。现在可以使用了
猜你喜欢
  • 1970-01-01
  • 2017-04-14
  • 1970-01-01
  • 1970-01-01
  • 2022-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多