Closed Thread
Results 1 to 10 of 10

Thread: Read onthefly data from PHP Form and use it in a MySQL query

  1. #1
    Thevenin is offline Learning Programmer
    Join Date
    Feb 2010
    Posts
    45
    Rep Power
    0

    Read onthefly data from PHP Form and use it in a MySQL query

    Hi all, this is my first post here and hope that someone could help me with my PHP/MySQL problem.

    I need to create a form that could grad on the fly some variables like name, surname, a checkbox and list choices and use one of them as a search string in the MySQL database table.

    I've written some code:
    Code:
    <? 

              
    function display() 
              { 

                  global 
    $wpdb
                  
    $wpdbtest = new wpdb('DB_USERNAME''DB_PASSWORD''DB_NAME''localhost'); 
                  
    $wpdbtest->show_errors();             

    ?> 
                    <form method="get"> 
                    Kind of plants:<br /> 
                    <select name="id_plants"> 
                        <option value=""></option> 
                        <option value="tree">Trees</option> 
                        <option value="shrub">Shrubs</option> 
                        <option value="bush">Bushes</option></select> 
                    <br /> 
                    <input type="submit" value="View"> 
                    </form> 

    <?php 

                      $myplants 
    $wpdbtest->get_results("SELECT * 
                                                                FROM plants_table 
                                                                WHERE id_plants = '"
    .$_GET['id_plants']."'"); 

                        foreach (
    $myplants as $myplant) { 
                          echo 
    $myplant->id_category'<br />'
                          echo 
    $myplant->id_name'<br />'
    // escape from PHP ... ?> 

                            <img src="../plants/wp-content/plugins/plants_plugin/plants_images/uploads/<? echo $myplant->id_image?>" width="250px" height="356px""> 
                            <br /><br /> 

    <? // starting up PHP again... 
                          

              } 

    ?>
    The problem is that when I press the "View" button instead to stay in the same page and have the category, name and image display, it goes to the homepage with this URL (eg.: in this case the URL is display for the Bushes selection):

    h_ttp://127.0.0.1:8181/tests/?id_plants=Bushes
    Any hint on why the .$_GET['id_plants']. doesn't work?

    Where's my mistake?
    I'm stuck on this for 2 weeks and dunno how to fix my problem.

    Really need help please

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Furry Pancake's Avatar
    Furry Pancake is offline Learning Programmer
    Join Date
    Feb 2010
    Location
    Utah
    Posts
    54
    Rep Power
    0

    Re: Read onthefly data from PHP Form and use it in a MySQL query

    I remember I had the same problem a while back,
    try doing this:

    Code:
    <?php 
    $id_plants 
    $_GET['id_plants'];
    $myplants $wpdbtest->get_results("SELECT * FROM plants_table WHERE id_plants = '".$_GET['id_plants']."'"); 

    foreach (
    $myplants as $myplant) { 
             echo 
    $myplant->id_category'<br />'
             echo 
    $myplant->id_name'<br />'
    ?>
    Grab the variable outside of the query... lmk if this works?

  4. #3
    Thevenin is offline Learning Programmer
    Join Date
    Feb 2010
    Posts
    45
    Rep Power
    0

    Re: Read onthefly data from PHP Form and use it in a MySQL query

    Unfortunatelly it doesn't work
    I think it's impossible!

    It takes me to the homepage with the same example string as above

  5. #4
    Furry Pancake's Avatar
    Furry Pancake is offline Learning Programmer
    Join Date
    Feb 2010
    Location
    Utah
    Posts
    54
    Rep Power
    0

    Re: Read onthefly data from PHP Form and use it in a MySQL query

    Hehe nothing is impossible we will find it out, I am at work. I will post up when I get onto my laptop so I can actually try out the code..

    Have you tried to connect it to the actual page rather than just the query?
    Code:
    h_ttp://127.0.0.1:8181/tests/?id_plants=Bushes 
    instead do:
    Code:
    h_ttp://127.0.0.1:8181/tests/plantpage.php?id_plants=Bushes 
    Hrmmmm. Don't give up

  6. #5
    Thevenin is offline Learning Programmer
    Join Date
    Feb 2010
    Posts
    45
    Rep Power
    0

    Re: Read onthefly data from PHP Form and use it in a MySQL query

    the big problem is that I'm working on a my own Wordpress plugin, and I think the main problem could be Wordpress.
    You all could said to me, why don't you ask on the WP Forum?
    Already done 4 times and no answers

  7. #6
    Feral is offline Programmer
    Join Date
    Jul 2008
    Posts
    163
    Rep Power
    15

    Re: Read onthefly data from PHP Form and use it in a MySQL query

    The only two things that I can see is.

    1.) you do not need to establish a new database connection. Simply use the $wpdb global to get the results of your query.

    and

    2.) your form does not have an action either use PHP_SELF or # for the action.

    Either one could cause your problem.

  8. #7
    Thevenin is offline Learning Programmer
    Join Date
    Feb 2010
    Posts
    45
    Rep Power
    0

    Re: Read onthefly data from PHP Form and use it in a MySQL query

    WORKING!!!!!!!! WORKING!!!!!!!! WORKING!!!!!!!! WORKING!!!!!!!! WORKING!!!!!!!!

    The problem was not the $_GET but this:
    Code:
    <form  id="plants" action="<?php echo $action_url ?>" method="get">
    I've added the
    Code:
    <?php echo $action_url ?>
    to my code and now everything works FINALLY!!

    The only problem is that the images doesn't work, but I could manage it.

    I really thank you all of you, you helped me so much to think and fix the problem!

    Really thank you!

  9. #8
    Furry Pancake's Avatar
    Furry Pancake is offline Learning Programmer
    Join Date
    Feb 2010
    Location
    Utah
    Posts
    54
    Rep Power
    0

    Re: Read onthefly data from PHP Form and use it in a MySQL query

    Glad you found the answer out Sorry I wasn't much help, haha but trial and error!

  10. #9
    Thevenin is offline Learning Programmer
    Join Date
    Feb 2010
    Posts
    45
    Rep Power
    0

    Re: Read onthefly data from PHP Form and use it in a MySQL query

    You both helped me a lot, my focus was on the $_GET instead of the address.

    Really thank you again!

  11. #10
    Furry Pancake's Avatar
    Furry Pancake is offline Learning Programmer
    Join Date
    Feb 2010
    Location
    Utah
    Posts
    54
    Rep Power
    0

    Re: Read onthefly data from PHP Form and use it in a MySQL query

    Seriously, I was concentrating on just that as well lol. So thank god you ended up getting it because that would have bugged till we did get it. lol

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 3
    Last Post: 08-18-2011, 09:17 AM
  2. Need help on a MySql Query
    By webcodez in forum Database & Database Programming
    Replies: 4
    Last Post: 03-06-2011, 05:33 AM
  3. How do I query mysql data via a link ($_GET?)? Glossary, term project help!?!
    By banker in forum Database & Database Programming
    Replies: 2
    Last Post: 04-20-2010, 01:51 PM
  4. MySQL Query Errors
    By Brandon W in forum Database & Database Programming
    Replies: 4
    Last Post: 02-15-2009, 02:36 AM
  5. MySQL Inner Join Query...
    By Lop in forum Database & Database Programming
    Replies: 3
    Last Post: 01-11-2007, 01:03 PM

Tags for this Thread

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