doulin2555 2013-11-06 22:24
浏览 32
已采纳

在页面加载和表单上显示数据库结果使用通配符提交[关闭]

I am having issue trying to display a database result on page load and form submit. when the page loads it keeps giving me blank page but when I submit the form, I get results.

<?php
    $alpha = mysql_real_escape_string($_POST['alpha']);

    $p ="^[".$alpha."]";

    if (!isset($p)&&empty($p)){$p = "^[a-z]";}

    $select = "SELECT u.pname,u.categoryid,p.amountpaid,p.teller,p.handler,
    p.scollection,p.project,p.levies,p.status,q.quarter,q.year,
    p.invoice,q.ordinary_cf,q.special_cf,q.cate_id
    FROM users u
    INNER JOIN tbl_paymentalert p
    ON u.categoryid = p.catid

    INNER JOIN tbl_quartersummary q
    ON p.catid= q.cate_id
    WHERE q.quarter = :quarter AND q.year = :year AND pname REGEXP :p
    ORDER BY u.pname  limit :eu,:limit
";

$q=$conn->prepare($select);
$q->bindValue(':quarter', $quarter, PDO::PARAM_STR);
$q->bindValue(':year', $year, PDO::PARAM_STR);
$q->bindValue(':p', $p, PDO::PARAM_STR);
$q->bindValue(':eu', $eu, PDO::PARAM_INT); 
$q->bindValue(':limit', $limit, PDO::PARAM_INT);
$q->execute();
    ?>
  • 写回答

1条回答 默认 最新

  • doutian3269 2013-11-07 04:22
    关注

    Okay... so... you're getting the value from $_POST.

    // if you're using mysqli_ or pdo and using a prepared query or data binding
    // mysql_real_escape_string is not necessary... 
    $alpha = mysql_real_escape_string($_POST['alpha']);
    
    // even if $alpha was empty, $p will always be set and will
    // never be empty because of the following statement.  
    // if alpha is empty, $p will be "^[]"
    $p ="^[".$alpha."]";
    
    
    // this code will never happen... so your default won't be set.     
    if (!isset($p) && empty($p) )
    {
        $p = "^[a-z]";
    }
    

    do something like...

    //        if `$_POST` is set,        use that value,   if not use 'a-z'
    $alpha = (isset($_POST['alpha'])) ? $_POST['alpha'] : 'a-z'; 
    
    $p = "^[".$alpha."]";
    

    and then

    $q->execute(array($quarter,$year,$p,$e,$limit));
    

    Make sure those variables are set or you have handled what happens if they are not set. It might cause you to get no results back without any explanation.

    (last edit, I swear, unless OP asks for clarification or something)
    good job for trying out the pdo, normally people are scared of it and ignore the warnings. seriously, good job!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ESP8266连接阿里云出现问题
  • ¥15 被蓝屏搞吐了,有偿求帮解答,Ai回复直接拉黑
  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并