mysql查询在选择单选按钮时没有从数据库获取值

时间:2017-07-13 20:02:34

标签: php mysql database radio-button

我已经搜索了很长一段时间但结果却没有成功。我试图选择单选按钮,因此在同一页面上显示其结果,但我的查询不知何故没有得到相同的记录。它说MySQL" QUERY EMPTY"。请告诉我在哪里做错了。这是我的代码。非常感谢帮助。

PHP

<?php 
    include 'blocks/headerInc.php' ;
    $errmsg   = $module_id = $query = $date_from = $date_to = $sql1 = "";
    //Search section start here
    /*$sqlQuery = "SELECT * FROM tbl_user WHERE type =3 ";
    if (isset($_REQUEST['submit'])) {
        if (!empty($_REQUEST['date_from'])) {
            $date_from = date("Y-m-d", strtotime($_REQUEST['date_from']));
        }
        if (!empty($_REQUEST['date_to'])) {
            $date_to = date("Y-m-d", strtotime($_REQUEST['date_to']));
        }
        if (!empty($date_to) && empty($date_from)) {
            $errmsg = "Please select valid date range.";
        }
        if (!empty($date_to) && (strtotime($date_from) > strtotime($date_to))) {
            $errmsg = "Please select valid date range.";
        }
        if ($errmsg == '') {
            if (!empty($date_to) && (strtotime($date_from) <= strtotime($date_to))) {
                $sqlQuery .= " AND created_on BETWEEN '$date_from' AND '$date_to'";
            }
            $sqlQuery .= "  order by id DESC";
        }
        $date_from = date("m/d/Y", strtotime($date_from));
        $date_to   = date("m/d/Y", strtotime($date_to));
        $date_from = $date_from != '01/01/1970' ? $date_from : '';
        $date_to   = $date_to != '01/01/1970' ? $date_to : '';*/
        if (isset($_POST['users']) && $_POST['users'] == 'approved') {
            $sql1 = mysql_query("SELECT * FROM tbl_user WHERE type =3 and status = 1");
            //$result = ($sql1);
            while ($row = $sql1->fetch_assoc()) {
                $users[] = $row;
            }
        } elseif (isset($_POST['users']) && $_POST['users'] == 'unapproved') {
            $sql1 = mysql_query("SELECT * FROM tbl_user WHERE type =3 and status = 0");
            //$result = mysql_query($sql1);
            while ($row = $sql1->fetch_assoc()) {
                $users[] = $row;
            }
        } elseif (isset($_POST['users']) && $_POST['users'] == 'all') {
            $sql1 = mysql_query("SELECT * FROM tbl_user WHERE type =3");
            //$result = mysql_query($sql1);
            while ($row = $sql1->fetch_assoc()) {
                $users[] = $row;
            }
        }
    //}
?>

HTML:

<div class="container pagecontainer">
    <!-- Static navbar -->
    <div class="row row-offcanvas row-offcanvas-right">
        <!--/.col-xs-12.col-sm-9-->
        <div class="col-sm-3 col-md-3 sidebar" id="sidebar">
            <div id="left_panel" class="clearfix left">
                <?php include 'blocks/leftnavInc.php' ; ?>
            </div>
        </div>
        <div class="col-xs-12 col-sm-9 page-right">
            <div class="panel panel-primary">
                <div class="panel-heading">Search Registered Candidate</div>
                <div class="panel-body">
                    <div class="column col-sm-offset-0">
                        <?php if($errmsg!="") echo "<div class='error'>".ucwords($errmsg)."</div>"; ?>
                        <form class="form-horizontal" method="get" action="">
                            <div class="form-group">
                                <div class="col-md-6">
                                    <div class="col-md-4">
                                        <label for="username" class="control-label">Date From:</label>
                                    </div>
                                    <div class="col-md-8">
                                        <div class="input-group date">
                                            <input class="form-control datepicker" data-val="true" data-val-date="The field Dob must be a date." data-val-required="The Dob field is required." id="Dob" name="date_from" placeholder="Date From" type="text" value="<?php echo $date_from ; ?>" >
                                        </div>
                                    </div>
                                </div>
                                <div class="col-md-6">
                                    <div class="col-md-4">
                                        <label for="username" class="control-label">Date To:</label>
                                    </div>
                                    <div class="col-md-8">
                                        <div class="input-group date">
                                            <input class="form-control datepicker" data-val="true" data-val-date="The field Dob must be a date." data-val-required="The Dob field is required." id="Dob" name="date_to" placeholder="Date To" type="text" value="<?php echo $date_to ; ?>" >
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="form-group">
                                <div class="col-md-6">
                                    <div class="col-md-8 text-left">
                                        <button type="submit" name="submit" value="submit" class="btn btn-success"><i class="glyphicon glyphicon-floppy-disk"></i> Search</button>
                                        <button type="reset" onClick="javascript:window.location.href='reportRegisteredUsers.php'" class="btn btn-danger"><i class="glyphicon glyphicon-ban-circle"></i> Cancel</button>
                                    </div>
                                </div>
                                <div class="col-md-6">
                                    <div class="col-md-4">
                                        <label for="username" class="control-label"> </label>                  
                                    </div>
                                    <div class="col-md-8 text-right">
                                    </div>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
            <div class="panel panel-primary">
                <div class="panel-heading">Report:Registered Candidate</div>
                <div class="panel-body">
                    <input type="radio" name="users" value="all" checked="checked"> All Candidates<br>
                    <input type="radio" name="users" value="approved"> Approved Candidates<br>
                    <input type="radio" name="users" value="unapproved"> Unapproved Candidates<br>      </form>          
                    <div class="column col-sm-offset-0">
                        <table id="example" class="table table-striped table-hover table-bordered dataTableReport dt-responsive nowrap"   cellspacing="0" width="100%">
                            <thead>
                                <tr>
                                    <th>S.No.</th>
                                    <th>Email ID</th>
                                    <th>SBI Employee ID</th>
                                    <th>Name</th>
                                    <th>Mobile No.</th>
                                    <th>Date of Birth</th>
                                    <th>Registration Date</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                    $sqr            =   $db->query($sql1);
                                    //print_r($sqr);
                                    //$i                =   1 ;
                                    //$sq           =   $db->query($sqlQuery);
                                    $i              =   1 ;
                                    if($db->affected_rows > 0)
                                    {
                                        while($row=mysql_fetch_array($sqr))
                                        {
                                         extract($row);
                                    ?>              
                                <tr>
                                    <td><?php echo $i ; ?></td>
                                    <td><?php echo $email ; ?></td>
                                    <td><?php echo $employee_id ; ?></td>
                                    <td><?php echo $first_name."&nbsp;".$middle_name."&nbsp;".$last_name ; ?></td>
                                    <td><?php echo $mobile ; ?></td>
                                    <td><?php if($dob !='1970-01-01'){echo date("d-m-Y", strtotime($dob)) ; }?></td>
                                    <td><?php echo date("d-m-Y", strtotime($created_on)) ; ?></td>
                                </tr>
                                <?php $i++;}} ?>              
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
            <div>
                <button type="reset" onClick="javascript:history.go(-1)" class="btn btn-danger"><i class="glyphicon glyphicon-ban-circle"></i> Go Back</button>
            </div>
        <!--/row-->
        </div>
        <!--/.sidebar-offcanvas-->
    </div>
</div>

<?php include 'blocks/footerInc.php'; ?>

1 个答案:

答案 0 :(得分:1)

Does this need to be strictly PHP and MySQL? Can we include some JavaScript/jQuery? While this doesn't answer your question directly, I hope it helps.

For your issue, I would detect the change event of your radio field and fire off a quick jQuery $.post to process the selection.

When your JavaScript sends a POST request to your PHP, you would run your PHP logic and return (or echo) the result. This can then be "digested" by your JavaScript. Here is a simple/rough example using jQuery and PHP. Adapt to your needs:

<script>
  // # on ready
  $(function() {

    // # current document - assumes you'll be submitting to self
    var self = document.location.href;

    // # when changing the user radios
    $('input[type=radio][name=users]').change(function() {

      // # grab the value of the radio and create a js array to post
      var postData = {'users': $(this).val()};

      // # post the postData to your PHP 
      $.post(self, postData).done(function(data, status, xhr) {

        // # assumes you're returning JSON data
        data = jQuery.parseJSON(data);

        // # add your logic here
        console.log('POST Response', data);
        // # update an element with the returned data or response:
        $('#example').before('<div>'+ data +'</div>');
     });
  });
});
</script>

Regarding your PHP code, as other users have suggested, you're using mysql_ functions that are no longer supported on modern versions of PHP. You're quickest and dirtiest adjustment, without rewriting everything, is to use mysqli_ functions (notice the additional 'i').

You're already listening for the $_POST['users'] parm, so the only addition I would recommend is to actually use that $users[] array. I could get into re-writing the PHP so it makes sense, but here is a quick adjustment to your existing code:

<?php
$users = array();

if(isset($_POST['users']) && $_POST['users'] == 'approved'){
  $query = "SELECT * FROM tbl_user WHERE type = 3 AND status = 1";

} else if(isset($_POST['users']) && $_POST['users'] == 'unapproved') {
  $query = "SELECT * FROM tbl_user WHERE type =3 and status = 0";

} else if (isset($_POST['users']) && $_POST['users'] == 'all') {
  $query = "SELECT * FROM tbl_user WHERE type =3";
}

if ($result = $mysqli->query($query)) {

  /* fetch associative array */
  while ($row = $result->fetch_assoc()) {
      $users[] = $row;
  }

  // # encode your $row array into JSON and echo for JavaScript  
  echo json_encode($users);

  // # free result set
  $result->free();
}

?>

While this isn't a complete solution, I hope it points you in the right direction.

A couple things to note - you'll need to adjust your MySQL connection code if you're to use mysqli_ functions. You'll also need to include jQuery (ideally from CDN) for that JavaScript to work (or you could rewrite it to not use a library like jQuery). This should return the results of your query to your JavaScript. The result should be available as "data" in your js. I've added a console.log so you can view the response in your inspector.

Good Luck!