Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: filter data using checkbox php and mysqli

  1. #1
    Join Date
    May 2012
    Posts
    217
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default filter data using checkbox php and mysqli

    Hi

    I need some help, I am trying to filter data using checkboxes, php and mysqli but can't seem to get it working

    I followed a guide I found on Google but don;t seem to work, below is the code I have

    index.php coding

    Code:
    <p>
    <form id="search_form">
        <div class="well">
        <h4 class="text-info">Search by Size</h4>
        <input value="Microsoft Office Pro Plus 2010" class="sort_rang" name="software_title[]"  type="checkbox"> Microsoft Office Pro Plus 2010
        <input value="Microsoft Office Pro Plus 2013" class="sort_rang" name="software_title[]"  type="checkbox"> Microsoft Office Pro Plus 2013
        </div>
        </form>
    </p>
    
    <div class="ajax_result">
    <?php if(isset($all_row) && is_object($all_row) && count($all_row)): $i=1;?>
    <?php foreach ($all_row as $key => $software_title) { ?>       
    <div class="col-sm-3 col-md-3">
    <div class="well">
    <h2 class="text-info"><?php echo $software_title['software_title']; ?></h2>
    <p><span class="label label-info">Size : 
    <?php echo $software_title['software_title']; ?></span></p>
    </div>
    </div>        
    <?php } ?>
    <?php endif; ?>
    
    <?php
    // connect to the database
    include('connect-db.php');
    
        $db=new mysqli('localhost','','','');
        $all_row=$db->query("SELECT * FROM ");
    
    // get the records from the database
    if ($result = $mysqli->query("SELECT id, software_title, customers_email, DATE_FORMAT(date_purchased, '%d/%m/%Y') AS date_purchased, sent FROM  ORDER BY id"))
    {
    // display records if there are records to display
    if ($result->num_rows > 0)
    {
    // display records in a table
    echo "<table class='records' id='software'>";
    
    // set table headers
    echo "<tr>
    <th>ID</th>
    <th>Software Title</th>
    <th>Customers Email</th>
    <th>Date Purchased</th>
    <th>Sent</th>
    <th colspan='2'>Actions</th>
    </tr>";
    
    while ($row = $result->fetch_object())
    {
    // set up a row for each record
    echo "<tr>";
    echo "<td><a href='view-specific-product-keys-sold.php?id=" . $row->id . "'>".$row->id . "</a></td>";
    echo "<td>" . $row->software_title . "</td>";
    echo "<td>" . $row->customers_email . "</td>";
    echo "<td>" . $row->date_purchased . "</td>";
    echo "<td>" . $row->sent . "</td>";
    echo "<td><a href='add-update-keys-sold.php?id=" . $row->id . "'>Edit</a></td>";
    echo "<td><a href='delete.php?id=" . $row->id . "'>Delete</a></td>";
    echo "</tr>";
    }
    
    echo "</table>";
    }
    // if there are no records in the database, display an alert message
    else
    {
    echo "No results to display!";
    }
    }
    // show an error if there is an issue with the database query
    else
    {
    echo "Error: " . $mysqli->error;
    }
    
    // close database connection
    $mysqli->close();
    
    ?>
    
    <a href="add-update-keys-sold.php">Add New Product Key Sold</a>
    
    <script type="text/javascript">
    $(document).on('change','.sort_rang',function(){
       var url = "search-filter-results.php";
       $.ajax({
         type: "POST",
         url: url,
         data: $("#search_form").serialize(),
         success: function(data)
         {                  
            $('.ajax_result').html(data);
         }               
       });
      return false;
    });
    </script>
    search-filter-results.php coding

    Code:
    <?php
        $db=new mysqli('localhost','','','');
        $sql="SELECT * FROM ";
        extract($_POST);
        if(isset($software_title)) 
            $sql.=" WHERE software_title IN (".implode(',', $software_title).")";
        $all_row=$db->query($sql);
    ?>
    <?php if(isset($all_row) && is_object($all_row) && count($all_row)): $i=1;?>
        <?php foreach ($all_row as $key => $software_title) { ?>       
        <div class="col-sm-3 col-md-3">
            <div class="well">
                <h2 class="text-info"><?php echo $software_title['software_title']; ?></h2>
                <p><span class="label label-info">Size :
                 <?php echo $software_title['software_title']; ?></span></p>          
            </div>
        </div>
       <?php } ?>
    <?php endif; ?>
    I check the checkboxes on the index.php but nothing happens

    Thank you in advance

  2. #2
    Join Date
    Jan 2015
    Posts
    78
    Thanks
    0
    Thanked 19 Times in 19 Posts

    Default

    I have two questions -

    1) What debugging have you done to find the problem yourself? What part of the code is working and exactly at what point is the code not working and what result occurs at that point rather than the expected result?

    2) What does your database statement error handling, assuming you have any in place, report about the queries you are running, particularity the one in search-filter-result.php file?

  3. #3
    Join Date
    May 2012
    Posts
    217
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    Hi

    Thank you for the reply, I have put error coding in but both php files do not display any errors

  4. #4
    Join Date
    Jan 2015
    Posts
    78
    Thanks
    0
    Thanked 19 Times in 19 Posts

    Default

    The IN() part of the sql query in search-filter-result.php is incorrect for string values and would be returning an sql error.

  5. #5
    Join Date
    May 2012
    Posts
    217
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    Quote Originally Posted by DyDr View Post
    The IN() part of the sql query in search-filter-result.php is incorrect for string values and would be returning an sql error.
    Oh right, weird as is not producing any sql errors

    what would I need to change the IN(0 part to as is text and not a INT value

  6. #6
    Join Date
    May 2012
    Posts
    217
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    I changed the IN line to the following in my search-filter-results.php but still does not work

    Code:
    $sql .= " WHERE " . implode(' AND ', $software_title);

  7. #7
    Join Date
    May 2012
    Posts
    217
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    Been playing round with it some more and changed the coding now to the following

    Code:
    $sql .= " WHERE software_title LIKE '%software_title%' ";
    it works but it matches the text if is software title is in the words but need to be for what checkboxes I am selecting, hope that makes sense

  8. #8
    Join Date
    Jan 2015
    Posts
    78
    Thanks
    0
    Thanked 19 Times in 19 Posts

    Default

    Quote Originally Posted by DyDr View Post
    The IN() part of the sql query in search-filter-result.php is incorrect for string values and would be returning an sql error.
    Each string value is surrounded by single-quotes, so that it's treated as a literal string, rather than a msyql keyword or identifier.

  9. #9
    Join Date
    May 2012
    Posts
    217
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    Quote Originally Posted by DyDr View Post
    Each string value is surrounded by single-quotes, so that it's treated as a literal string, rather than a msyql keyword or identifier.
    That's what I got now or am I still wrong

    Code:
    $sql .= " WHERE software_title LIKE '%software_title%' ";

  10. #10
    Join Date
    Jan 2015
    Posts
    78
    Thanks
    0
    Thanked 19 Times in 19 Posts

    Default

    That query may have single-quotes where they are needed, but that's not the query format that does what you want.

Similar Threads

  1. Mysqli vs Mysql and CMS
    By Victro in forum MySQL and other databases
    Replies: 2
    Last Post: 06-16-2010, 01:14 PM
  2. Storing Checkbox Data
    By resilient in forum MySQL and other databases
    Replies: 1
    Last Post: 02-15-2009, 05:35 PM
  3. Checkbox Data converted to plain text
    By pranceatron in forum JavaScript
    Replies: 0
    Last Post: 04-14-2008, 05:03 PM
  4. Replies: 1
    Last Post: 10-27-2007, 01:04 PM
  5. Checkbox array to delete mysql data
    By jnscollier in forum PHP
    Replies: 5
    Last Post: 05-26-2007, 02:09 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •