Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Jul 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Retrieve data from database using checkboxes

    Hi,
    I am hoping what i want to do is possible.

    I wanted a little project for myself so i decided to try and make a side-by-side comparison table of different products (an example would be different phones).

    I have data stored in my database and i wish to retrieve specific database entries depending on what checkboxes the user has ticked.
    For example, i have the data of 4 different phones stored in my database (all different fields such as make, model, features etc) and if a user wants to compare phone 1 and phone 3, they select the corresponding checkboxes and submits the page, then i table is displayed with the information of phones 1 and 3.

    I hope this makes sense.

    This is my code so far
    Code:
    <html>
    <form name="phonecompare" method="post" action="compare.php">
    <p>Select Phone
    <input type="checkbox" name="phone[]" value="1">Phone 1
    <input type="checkbox" name="phone[]" value="2">Phone 2
    <input type="checkbox" name="phone[]" value="3">Phone 3
    <input type="checkbox" name="phone[]" value="4">Phone 4
    </p>
    <p>
    <input type="submit" name="Submit" value="Submit">
    </p>
    </form>
    </html>
    and for compare.php my code is:
    PHP Code:
    <?php
    include("includes.php");
    doConnect();

    $sql "SELECT * FROM phone WHERE phone_id='" .$_POST['phone_id']."' (";
    foreach (
    $_POST['phone'] as $phone_id)
    $sql .= "'" $phone_id "',";
    $sql substr($sql,0,-1) . ")";
    exit(
    $sql);
    $result mysql_query($sql);
    while (
    $row mysql_fetch_array($result)) {
    print 
    $row['name'];
    }

    ?>
    All my connecting to the database is done using the includes file.
    The output im getting from this is if i select phone 1 and 3 from the form:
    Code:
    SELECT * FROM phone WHERE phone_id='' ('1','3')
    Any help would be great and if my code is completely wrong feel free to tell me, im still learning!

    Thanks

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,472
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    So what is the problem,
    do you get an error, or it doesn't work,
    or the results are not what you expect?

    I don't understand your question.

  • #3
    New to the CF scene
    Join Date
    Jul 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Its not working.
    The output i get is
    Code:
    SELECT * FROM phone WHERE phone_id='' ('1','3')
    instead of a table with different phone data in.

  • #4
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,535
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Oh, because of the exit function... its because the query you're trying to build is faulty. You're mixing equivalence with what looks like trying to use the IN functionality.

    First, you never define phone_id in your form. That's why you're getting phone_id = ''.

    Second, your comparison is wrong. Either you go with phone_id = {number} or you go with phone_id IN ({number}, {number}). You can't mix both.

    Also, as long as your DB is expecting an integer/number, you don't need to quote numbers.

  • #5
    New to the CF scene
    Join Date
    Jul 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I think i've got myself very confused and may need to start again!

  • #6
    Regular Coder
    Join Date
    Sep 2007
    Location
    Grahamstown, South Africa
    Posts
    237
    Thanks
    6
    Thanked 17 Times in 17 Posts
    Could i give you a tip for building applications? Well, Im gonna give you one any way! :-D

    When building applications, there's a good sequence to follow that will help you avoid the errors you are getting above.

    1.) Identify what the query/function params/etc should look like and do.
    ie SELECT * FROM moaning_clients WHERE moan_date = '01-02-2010' AND name = 'johnthemoaner';
    2.) Write out the function/query/script to determine that it works perfectly. (manually coding in params)
    3.) Write the code to send params. ie the form
    4.) Print_r the params sent by step 3.
    5.) Clean/sanitize the params from form. Insert the params you want.*

    *Do any manipulation you need too! ie if $_POST['val'] == 'hi' { //do this }

    It might sound long winded, but this helps you learn how each module/process communicates with each other. As you become more seasoned you can slowly leave out steps. eg (step 1).

    Soon you'll be able to leave out every step, like me, and just upload blank pages to the net. For eg. I made google from a collection of blank pages just because I was so good at my process! :-D


  •  

    Posting Permissions

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