Jump to content

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

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
9 replies to this topic

#1
Thevenin

Thevenin

    Learning Programmer

  • Members
  • PipPipPip
  • 58 posts
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:
<? 


          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):

Quote

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
Furry Pancake

Furry Pancake

    Learning Programmer

  • Members
  • PipPipPip
  • 54 posts
I remember I had the same problem a while back,
try doing this:

<?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?

#3
Thevenin

Thevenin

    Learning Programmer

  • Members
  • PipPipPip
  • 58 posts
Unfortunatelly it doesn't work :crying:
I think it's impossible! :crying:

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

#4
Furry Pancake

Furry Pancake

    Learning Programmer

  • Members
  • PipPipPip
  • 54 posts
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?
h_ttp://127.0.0.1:8181/tests/?id_plants=Bushes
instead do:
h_ttp://127.0.0.1:8181/tests/plantpage.php?id_plants=Bushes

Hrmmmm. Don't give up :)

#5
Thevenin

Thevenin

    Learning Programmer

  • Members
  • PipPipPip
  • 58 posts
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 :(

#6
Feral

Feral

    Programmer

  • Members
  • PipPipPipPip
  • 162 posts
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.

#7
Thevenin

Thevenin

    Learning Programmer

  • Members
  • PipPipPip
  • 58 posts
WORKING!!!!!!!! WORKING!!!!!!!! WORKING!!!!!!!! WORKING!!!!!!!! WORKING!!!!!!!!

The problem was not the $_GET but this:
<form  id="plants" action="<?php echo $action_url ?>" method="get">

I've added the
<?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! :thumbup:

#8
Furry Pancake

Furry Pancake

    Learning Programmer

  • Members
  • PipPipPip
  • 54 posts
Glad you found the answer out :P Sorry I wasn't much help, haha but trial and error! :thumbup1:

#9
Thevenin

Thevenin

    Learning Programmer

  • Members
  • PipPipPip
  • 58 posts
You both helped me a lot, my focus was on the $_GET instead of the address.

Really thank you again! :D

#10
Furry Pancake

Furry Pancake

    Learning Programmer

  • Members
  • PipPipPip
  • 54 posts
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 :thumbup: