I have a searchresult.php file where I pull all the menus names out of my database and print them with a button close to them where the button has a input type hidden where I associated the name of the menu through the variable $nameofmenu . Something like
Menu1 --> Button (See Menu)
Menu2 --> Button (See Menu)
Menu3 --> Button (See Menu)
<?PHP
while($myrow = pg_fetch_assoc($result)) {
$nameofmenu = $myrow[name];
echo $nameofmenu; //It prints correctly the name of the menu
echo '<form name"formname" method="post" action="resultsmenu.php">';
echo "<input type='hidden' name='menuname' value=' $nameofmenu ' />";
echo "<input type='submit' name='submit' value='See Menu' />";
}
?>
Then I have my resultsmenu.php file that will open when I click in any of the buttons.
<?php
$nameofmenu = $_POST['menuname'];
$db = pg_connect('host=localhost dbname=test user=myuser password=mypass');
$query = "SELECT * FROM menu where name='$nameofmenu'";
$result = pg_query($query);
if (!$result) {
echo "Problem with query " . $query . "<br/>";
echo pg_last_error();
exit();
}
$myrow = pg_fetch_assoc($result);
$description = $myrow[description];
?>
I have 2 Issues:
First issue: in the searchresult.php, even it prints out correctly all the different menu names, whenever I click any of the buttons, all the buttons return my the same menu (The last one, in the example: Menu 3 in the $nameofmenu = $_POST['menuname']; of the resultsmenu.php file.
Second issue: this issue happend in the resultsmenu.php file. Even for Menu 3, it seams that the recognized the value in the variable. Meaning I don't get back any result or value from the query: $query = "SELECT * FROM menu where name='$nameofmenu'";
However, if I set up the value Manually like $nameofmenu = "Menu3"; then It works.
I even tried this code to see if there is any different in the value passed from $_POST['menuname'] and the value typed in manually and it seams to be the same value because it prints "Both variables are the same"
$nameofmenu2 = "Menu3";
$nameofmenu = $_POST['menuname']; //where the value in menuname is Menu3
if ($nameofmenu2 = $nameofmenu){echo "Both variables are the same";}
Thank you so much
You overwrite the same input field (=name) over and over again, so only the "last" overwrite is returned/available. To avoid this, put the data as POST-ARRAYs like this (also switch your quotes):
echo '<input type="hidden" name="menuname[]" value="'.$nameofmenu.'" />';
After submitting, you now will see that $_POST['menuname'] is an array! e.g.
var_dump($_POST['menuname']);
As mentioned by David, your are assigning nameofmenu2 to nameofmenu. But furthermore, when you echoed:
echo "<input type='hidden' name='menuname' value=' $nameofmenu ' />";
you included whitespace on either side of $nameofmenu. This could cause problems when you go to make your query. Try using:
var_dump($_POST['menuname']);
to check your expected post data.
Related
The context is: e-commerce, the problem stands in adding items to the cart.
I created a while loop to iterate through each item my query returns and each item has a button that redirects to a "info on the item" page. My problem is that since there's a loop, the values to submit (e.g. the ID of the item) is overloaded and every button submits the values of the last item.
I can pass all the IDs of all the plants but i have no idea how to, in the "item detail" page, to show the correct item among the array.
lista-piante.php: (summarized)
<?php session_start();
// connect to the database
$connessione = new mysqli('localhost', 'root', 'root', 'mio');
//query
$user_check_query = "SELECT Pianta.NOME as nome, PIANTA.ID as pid, Item.PREZZO as prezzo, Item.ID as id
FROM Item, Pianta WHERE Pianta.ID = Item.PIANTA";
$result = mysqli_query($connessione, $user_check_query);
if($result->num_rows > 0) {
echo"<h3>Lista delle nostre piante</h3>";
echo"<ul class=\"plant-flex\">";
// loop through records
while($row = $result->fetch_array(MYSQLI_ASSOC)){
echo"<form method='get' action='../html/details-pianta.php'>";
echo"<li>";
echo"<div class='plant-preview'>";
echo"<div class='plant-preview-description'>";
//dichiarazione variabili (per leggibilità)
$nome= $row['nome'];
$pid = $row['pid'];
// PRINT NAME
echo"<div class='plant-preview-description-name'>";
echo "<p class='bold'>" . $nome . "</p>";
echo "<input type='hidden' name='name' value='$nome' />";
echo"</div>";
echo "<input type='hidden' name='pid[]' value='$pid' />";
echo"<div>";
echo"<button type=\"submit\" class=\"btn\" name=\"details_plant\">Dettagli" . $item . "</button>";
echo"</div>";
echo"</li>";
echo"<form/>";
}
$result->free();
}
echo "<ul/>";
$connessione->close();
details-pianta.php: (summarized, it will contain the style of the page)
<?php session_start();?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="it" lang="it">
<head> <!-- meta tag and other stuff --> </head>
<body>
<div>
<?PHP include('../php/dettagli-pianta.php'); ?>
</div>
</div>
</body>
</html>
dettagli-pianta.php: (summarized, it should contain the info of each item)
<?php
session_start();
// connect to the database
$connessione = new mysqli('localhost', 'root', 'root', 'mio');
$pid = mysqli_real_escape_string($connessione, $_GET['pid']);
//but pID is either the last item's id, or an array with all items' ids, so i can't chose the only one i want
//the URL shows always more pIDs (from the lista-piante.php get form)
$user_check_query = "SELECT * FROM Pianta, item WHERE Pianta.ID = '$pid' ";
$result = mysqli_query($connessione, $user_check_query);
if($result->num_rows > 0) {
echo"<h3>Dettagli della pianta</h3>";
echo"<ul>";
//loop thought query records
while($row = $result->fetch_array(MYSQLI_ASSOC)){
echo"<form method='post' action='../php/add-carrello.php'>";
echo"<li>";
echo"<div>";
//dichiarazione variabili (per leggibilità)
$nome =$row['NOME'];
//$genere= $row['GENERE'];
//$specie= $row['SPECIE'];
//etc etc
// PRINT NAME
echo"<div>";
echo "<p class='bold'>" . $nome . "</p>";
echo"</div>";
//echo"<div>";
//echo "<p class='bold'>" . $specie. "</p>";
//echo"</div>";
//echo "<input type='hidden' name='pid' value='$pid' />";
echo"<div>";
echo"<button type=\"submit\" class=\"btn\" name=\"add-carrello\"> Aggiungi al carrello</button>";
echo"</div>";
echo"</li>";
}
$result->free();
}
echo "<ul/>";
$connessione->close();
There is an error with form closing, but... I have a feeling you are complicating it for yourself by using all those forms/buttons.
I would clean up the code and get rid of the form/button scheme altogether, and use simple a href links with ?id=$pid
Something like this (shortened, if needed post a comment and I can expand):
while($row = $result->fetch_array(MYSQLI_ASSOC)){
echo "<li>";
echo "<div class='plant-preview'>";
echo "<div class='plant-preview-description'>";
//dichiarazione variabili (per leggibilità)
$nome = $row['nome'];
$pid = $row['pid'];
// PRINT NAME
echo "<div class='plant-preview-description-name'>";
echo "<p class='bold'>" . $nome . "</p>";
echo "</div>";
echo "<div>";
echo "<a href ='../html/details-pianta.php?pid=$pid'>Dettagli " . $nome . "</a>";
echo "</div>";
echo "</li>";
}
You can use CSS later to make your a-href link look like a button, add image, or play with it any way you like.
Then in the product details page (dettagli-pianta.php) use your product id pretty much same as you already do:
$pid = $_GET['pid']
...
$user_check_query = "SELECT * FROM Pianta WHERE Pianta.ID = '$pid' ";
Here is one Stack Overflow that talks about using link instead form:
How send parameter to a url without using form in php?
Edit (mistakes found, more suggestions, in case you still want to keep forms/buttons):
you did not close ?> in your examples, but ok, probably just copy/paste here
you have $item variable that isn't defined, and not pulled from SELECT query, so I can only assume that is supposed to be $nome (or you deleted something to make it shorter in question and forgot about it)
you also probably wanted a space like this (note space after Dettagli)
echo "<button type='submit' class='btn' name='details_plant'>Dettagli " . $nome . "</button>";
not sure why you keep files in folders "../html/" and "../php/" when both are with extensions .php, could be confusing for others later working on your code, or ... maybe it's just me
you have <?php session_start();?> then under it you include a file that also has <?php session_start();?> ... no need for one of them. Since "details-pianta.php" is in folder "html" I guess you can remove it from there, as you don't need session just to include another PHP file
in form you submit name='pid[]' when you should just use name='pid' (no brackets), like this:
echo "<input type='hidden' name='pid' value='$pid' />";
in "dettagli-pianta.php" you have a select like this: $user_check_query = "SELECT * FROM Pianta, item WHERE Pianta.ID = '$pid' "; where you name two tables but don't join them, and I can only assume it should be $user_check_query = "SELECT * FROM Pianta WHERE Pianta.ID = '$pid' ";
be careful of uppercase/lowercase in database and column names, try to standardize
in one line you echo with mixed quotes something like echo "<bla name='bla'>" and in next you escape quotes like echo "<bla name=\"bla\">" ... make it easy on yourself and - standardize. Unless absolutely necessary, first way (mixing single and double quotes) is preferable, and only when you have something really complicated to echo like mix of HTML+JavaScript, then resort to escaping quotes
And finally, but actually a direct answer to your issue - you closed your form in the wrong way (yeah, had to look real hard to see it):
echo"<form/>";
You need to have (space is optional, reads better):
echo "</form>";
Non the less, as others too have already commented, using forms just to get buttons is an overkill. Try using simple links (<a href> elements) and style them with CSS to your liking, making them look like buttons...
And last, but... I believe it to be important... Try not to mix Italian and English in code (including database structure). It is obvious you are just learning but havin database called "mio" with table "pianti" and column "prezzo" ... then use something like "item". Also, names of files like "details-pianta", then you have another file "detaggli_pianta", and in button name you use "details_plant", it is huge mashup of two languages. you seem to be fluent English speaker judging from your question, so - why not use English everywhere in code? Name your database "my_first_web_store", name your table "plants", name your column "price", name your PHP files "plant_details" (or details_plant, whatever), use comments in English in your code (nobody but you and other developers see that). You will be thankful later when your plant-selling company expands worldwide, and when you will have multi-language webshop, and maybe 3 more developers working from India and US, all working on same project... :)
Have fun learning!
This may have been asked before but i have not been able to find it.
I have created a dropdown list in a form to show a selection from a database.
I am then sending that information via $_POST to another page.
But i am only getting the one result (eg plantID).
$sql = "SELECT DISTINCT * FROM PLANTS";
$result = mysqli_query($mysqli,$sql)or die(mysqli_error());
//********************* Botannical name drop down box
echo "<form name='selection' id='selection' action='profile.php' method='post'>";
echo "<select name='flower'>";
while($row = mysqli_fetch_array($result)) {
$plantid = $row['FlowerID'];
$plantname = $row['Botannical_Name'];
$plantcommon = $row['Common_Name'];
$plantheight = $row['Height'];
$plantav = $row['AV'];
$plantcolours = $row['Colours'];
$plantflowering = $row['Flower_Time'];
$plantspecial = $row['Special_Conditions'];
$plantfrost = $row['Frost_Hardy'];
$plantaspect = $row['Aspect'];
$plantspeed = $row['Growth_Speed'];
echo "<option value=".$plantid.">".$plantname." -> AKA -> ".$plantcommon."</option>";
}
echo "</select>";
echo "<br />";
//********************* End of form
echo "<input type='submit' name='submit' value='Submit'/>";
echo "</form>";
Is there any way i can send all the data via post or
can i somehow get the required PlantID from the database and show all the information for that record in a table on the second page.
Hope this makes sense to someone out there :D
Although technically you can, it is not advised nor used anywhere.
Just send an id like you do right now and then select all the data with another query based on this id.
First of all this is my first question on here, and altohugh I have searched the site none of the answers I've seen resolve my current problem.
I am a PHP novice and am currently working on an end project for a course. The object is to make a rudimentary blog where users can post, delete and edit their news, admins can edit or delete everything etc. I am mostly doing fine, but am having a bit of trouble with the editing feature.
The following code displays all blog posts, their authors and dates of posting. If the currently logged in person is the author of a post or a admin, they have the option of deleting or editing each individual post. A small form appears that contains the title and post text. When the user types something else in clicking on the edit button should change the values in the database to the new values the user specified. The problem is that whenever i click on the edit button in the current setup, nothing happens. If i move the if statement outside of the other if statement, the posts do update, but become blank in the database.
Running print_r($_POST) after the fact shows that the array it builds has correct names and updated values, but still they aren't updated in the database. Here is the code, the pertinent part starts at the last if statement( I know, it isn't injection proof, will get to that as soon as it works):
$query = "SELECT id, title, body, pub_date, user_id FROM posts ORDER BY id desc";
$query_fetch = mysql_query($query);
while ($blog_post = mysql_fetch_assoc($query_fetch)) {
$author_id = $blog_post["user_id"];
$post_id = $blog_post["id"];
$post_id2 = $blog_post["id"] . 2;
$title = $blog_post['title'];
$body = $blog_post['body'];
$query = "SELECT username FROM users WHERE id = '$author_id'";
$query_run = mysql_query($query);
$author = mysql_fetch_assoc($query_run);
echo "<h2>" . censor($blog_post["title"]) . "</h2>" . "<br> <p> Autor: " . $author["username"] . "</p><br><p>Objavljeno: " . $blog_post["pub_date"];
if ($_SESSION['admin'] == 1 or $_SESSION['username'] == $author["username"]) {
echo "<form action='' method='POST'><input type='submit' name= '$post_id' value= 'Obriši objavu'></form>";
echo "<form action='' method='POST'><input type='submit' name= '$post_id2' value= 'Uredi objavu'></form>";
}
echo "<p>" . censor($blog_post["body"]) . "</p>";
if (isset($_POST["$post_id"])) {
$del_post = "DELETE FROM posts WHERE id = '$post_id'";
mysql_query($del_post);
}
if (isset($_POST["$post_id2"])) {
echo "<form action='' method= 'POST'>New title<input type='text' value = '$title' name='title'>New text<textarea name='body' id='' cols='30' rows='10'>$body</textarea><input type='submit' name='edit' value='edit'></form>";
if (isset($_POST['edit'])) {
$edit_title = $_POST['title'];
$edit_body = $_POST['body'];
$query = "UPDATE posts SET title= '$edit_title', body= '$edit_body' WHERE id= '$post_id'";
mysql_query($query);
}
}
}
Any help would be appreciated.
This last piece of code
if (isset($_POST["$post_id2"])) {
echo "<form action='' method= 'POST'>New title<input type='text' value = '$title' name='title'>New text<textarea name='body' id='' cols='30' rows='10'>$body</textarea><input type='submit' name='edit' value='edit'></form>";
if (isset($_POST['edit'])) {
$edit_title = $_POST['title'];
$edit_body = $_POST['body'];
$query = "UPDATE posts SET title= '$edit_title', body= '$edit_body' WHERE id= '$post_id'";
mysql_query($query);
}
}
gets activated when post_id2 is sent, but generates a form where post_id2 is not contained anymore. So when you submit that form, the IF is not entered.
You can modify it like this:
if (isset($_POST["$post_id2"])) {
echo "<form action='' method= 'POST'>New title<input type='text' value = '$title' name='title'>New text<textarea name='body' id='' cols='30' rows='10'>$body</textarea><input type='submit' name='edit' value='edit'></form>";
}
if (isset($_POST['edit'])) {
$edit_title = $_POST['title'];
$edit_body = $_POST['body'];
$query = "UPDATE posts SET title= '$edit_title', body= '$edit_body' WHERE id= '$post_id'";
mysql_query($query);
}
In general I think you would find it easier to use forms differently, specifically by using some sort of action tag:
input type="hidden" name="command" value="edit"
input type="hidden" name="post" value="{$post_id}"
This way you could run one single query immediately, without the need for browsing all the posts in a cycle.
One other useful possibility is to split your code between different PHP files, and keeping common code in one include:
<?php // this is delete.php
include "common.php";
$post_id = my_get_var('post_id');
my_sql_command("DELETE FROM posts WHERE...");
used from
<form action="delete.php" method="post" ...>
As you can see this allows for different ways of retrieving post_id (centrally defined in a single function my_get_var in common.php) and the central definition of SQL functions. How this function interfaces to MySQL can then be updated, specifically passing from mysql_ functions (which are deprecated, and soon will no longer be available) to e.g. PDO.
It also allows you to test a single command independently, by directly entering delete.php in the browser (you need for my_get_var to accept both POST and GET variables to do this).
Details
You want to inspect and/or modify a collection of posts. You then require initially at least the following operations: list, edit, and delete.
Only the first works against all posts.
So you could have a list.php file running the SELECT. Also, it is only in this SELECT that you need information about the user, so your query could become:
$query = "SELECT posts.id, title, body, pub_date, user_id, username FROM posts JOIN users ON (posts.user_id = users.id) ORDER BY posts.id desc";
In the display cycle we would display this information:
$query_fetch = mysql_query($query);
// This file will receive requests to edit or delete
// We can use a single form.
echo '<form action="manage.php">';
while ($post = mysql_fetch_assoc($query_fetch)) {
echo "<h2>" . censor($post["title"]) . "</h2>" . "<br> <p> Autor: " . $post["username"] . "</p><br><p>Objavljeno: " . $post["pub_date"];
if ((1 == $_SESSION['admin']) or ($_SESSION['username'] == $post["username"]) {
echo "<input type=\"submit\" name=\"Obriši objavu\" value=\"{$post['id']}\" />";
echo "<input type=\"submit\" name=\"Uredi objavu\" value=\"{$post['id']}\" />";
}
echo "<p>" . censor($blog_post["body"]) . "</p>";
}
echo "</form>";
This way you need only one form, and it will submit one field with a name describing the action to be taken, and the post on which to do it.
The file manage.php will then receive this information -- and can also be used to update it:
foreach(array(
"delete" => "Obriši objavu", // from list.php
"edit" => "Uredi objavu", // " "
"update" => "update" // from this file itself (see below)
)
as $test_todo => $var) {
if (array_key_exists($var, $_POST)) {
$id = $_POST[$var];
$todo = $test_todo;
}
}
if (isset($id)) {
switch($todo) {
case "delete":
mysql_query("DELETE FROM posts WHERE id = '{$id}'");
break;
case "edit":
// Get this post.
$query = "SELECT posts.id, title, body, pub_date, user_id, username FROM posts JOIN users ON (posts.user_id = users.id) WHERE posts.id = '{$id}';";
echo '<form action="manage.php" method= "POST">';
// This is how we tell this file what to do, and to what.
echo "<input type=\"hidden\" name=\"update\" value=\"{$id}\">";
// run query, fetch the one record, display info...
echo "</form>";
break;
case "update":
// Build the update query from $_POST.
mysql_query("UPDATE posts SET ...");
}
At first check that your query is correct. Then try to hard-code your query. Also test your query in phpMyAdmin Also you can try to remove the '' from your number variables on every query.
Please, can you give us your error?
There is a possibility also that your database has already been updated. So double check it.
This is how I usually debug. echo the query. Run it in PHPmyadmin, and see the error.
so, in your case.
echo "UPDATE posts SET title= '$edit_title', body= '$edit_body' WHERE id= '$post_id'";
echo that and you will have the query that the script will be trying to run.
Try running it in phpmyadmin and check what the error is.
example.php ~ when i check the checkbox in example.php webpage.. then i click the print button, the value will be pass on example1.php
if($items) {
foreach($items as $i)
{
print"<input name='checkbox[]' type='checkbox' id='checkbox[]' value='$i->logi_id'>";
}
}
echo "<a href='example1.php?checkbox=" . $checkbox . "'><input type='button' name='print' id='delete' value='Print'></a>"
?>
example2.php
<?php
$keyword = $_REQUEST['keyword'];
$keycateg = $_REQUEST['keycateg'];
$print = $_REQUEST['print'];
$checkbox = $_REQUEST['checkbox'];
$count = count($_REQUEST['checkbox']);
if($print){
for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];
$sql = "select * FROM sampleDB WHERE logi_id='$del_id'";
$result = mysql_query($sql);
}
}
?>
Simple words ---
checkbox=<?php echo $checkbox; ?>
but dont rely on user inputs,...sanitize the data
If i understand your question correctly you want to submit the form using a link, prefered way would be to style a submit button as a link, but you could ofc use javascript to submit the form using a link. you can find a more indepth explanation here
http://www.javascript-coder.com/javascript-form/javascript-form-submit.phtml
I guess his question is how he can evaluate the current value of the checkbox and use it in a a href "GET" encoded style.
I would recommend not using a href, but using another form with post method "get" (that encodes the url for you).
Else your are stuck with javascript I believe.
$checkbox_string = htmlentities(http_build_query(array('checkbox'=>$checkbox)));
echo "<a href='example1.php?$checkbox_string'>...</a>"
I'm currently using php to populate a form with selections from a database. The user chooses options in a select style form and submits this, which updates a summary of the selections below the form before a second submit button is used to complete the interaction.
My issue is that every time a user uses the first submit, the selections that were there previously do not stick. They have to go through the whole form again.
Is there anyway to keep these selections present without resorting to php if statements? There are a ton of options so it would be a pain to use php for each one. Also, form is being submitted via POST.
Sample from form:
<?php
// GRAB DATA
$result = mysql_query("SELECT * FROM special2 WHERE cat = 'COLOR' ORDER BY cat")
or die(mysql_error());
echo "<div id='color'><select id='color' name='product_color'>";
while($row = mysql_fetch_array( $result )) {
$name= $row["name"];
$cat= $row["cat"];
$price= $row["price"];
echo "<option value='";echo $name;echo"'>";echo $name;echo" ($$price)</option>";}
echo "</select>";
echo "<input type='hidden' name='amount_color' value='";echo $price;echo"'></div>";
?>
I tried using this js snippet to repopulate the selections, but it does not seem to work properly...
<script type="text/javascript">document.getElementById('color').value = "<?php echo $_GET['proudct_cpu'];?>";</script>
This does not seem to work. Any suggestions other than php if statements?
Thanks!
edit: This is basically the form set up I'm using, though I've shortened it significantly because the actual implementation is quite long.
// Make a MySQL Connection
<?php mysql_connect("localhost", "kp_dbl", "mastermaster") or die(mysql_error());
mysql_select_db("kp_db") or die(mysql_error());
?>
<br />
<form action="build22.php" method="post">
<input type="hidden" name="data" value="1" />
<br />
<br />
<?php
// GRAB DATA
$result = mysql_query("SELECT * FROM special2 WHERE cat = 'color' ORDER BY cat")
or die(mysql_error());
echo "<div id='color'><select id='color' name='product_color'>";
while($row = mysql_fetch_array( $result )) {
$name= $row["name"];
$cat= $row["cat"];
$price= $row["price"];
echo "<option value='";echo $name;echo"'>";echo $name;echo" ($$price)</option>";}
echo "</select>";
echo "<input type='hidden' name='amount_color' value='";echo $price;echo"'></div>";
?>
<input type="submit" value="Update Configuration">
</form>
The selections from the form above get echoed after submission to provide the user with an update as such:
<div id="config" style="background-color:#FFF; font-size:12px; line-height:22px;">
<h1>Current Configuration:</h1>
<?php echo "<strong>Color:</strong>    ";echo $_POST['product_color']; ?>
</div>
I assume you're storing the user's selections in a separate table. If that's the case, you'll need to add some logic to determine if you should display the form values or what's already been stored.
<?php
// form was not submitted and a config id was passed to the page
if (true === empty($_POST) && true === isset($_GET['config_id']))
{
// make sure to properly sanitize the user-input!
$rs = mysql_query("select * from saved_configuration where config_id={$_GET['config_id']}"); // make sure to properly sanitize the user-input!
$_POST = mysql_fetch_array($rs,MYSQL_ASSOC); // assuming a single row for simplicity. Storing in _POST for easy display later
}
?>
<div id="config" style="background-color:#FFF; font-size:12px; line-height:22px;">
<h1>Current Configuration:</h1>
<?php echo "<strong>Color:</strong>    ";echo $_POST['product_color']; ?>
</div>
So after storing the user's selections in the database, you can redirect them to the page with the new config_id in the URL to load the saved values. If you're not storing the selected values in a table, you can do something similar with cookies/sessions.
echo the variables into the value tag of the form elements. If you post all your code I'm sure I can help you.
UPDATE
ah, so they are dropdown lists that you need to remember what was selected? Apologies, I read your post in a rush yesterday and thought it was a form with text inputs.
I just did a similar thing myself but without trying your code let me see if I can help.
Basically what you need to do is set one value in the dropdown to selected="selected"
When I had to do this I had my dropdown values in an array like so:
$options = array( "stack", "overflow", "some", "random", "words");
// then you will take your GET variable:
$key = array_search($_GET['variablename'], $options);
// so this is saying find the index in the array of the value I just told you
// then you can set the value of the dropdown to this index of the array:
$selectedoption = $options[$key];
This is where it might be confusing as my code is different so if you want to use it you will probably need to restructure a bit
I have a doSelect function to which I pass the following parameters:
// what we are passing is: name of select, size, the array of values to use and the
// value we want to use as the default selected value
doSelect("select_name", 1, $options, $selectedoption, "");
// these are the two functions I have:
// this one just processes each value in the array as a select option which is either
// the selected value or just a 'normal' select value
FUNCTION doOptions($options, $selected)
{
foreach ($options as $option)
{
if ($option == $selected)
echo ("<option title=\"$title\" id=\"$value\" selected>$option</option>\n");
else
echo ("<option title=\"$title\" id=\"$value\">$option</option>\n");
}
}
// this is the function that controls everything - it takes your parameters and calls
// the above function
FUNCTION doSelect($name, $size, $options, $selected, $extra)
{
echo("<select class=\"\" id=\"$name\" name=\"$name\" size=\"$size\" $extra>\n");
doOptions($options, $selected);
echo("</select>\n");
}
I know that's a lot of new code that's been threw at you but if you can get your select values from the db into the array then everything else should fall nicely into place.
The only thing I would add, is at the start where we call doSelect, I would put that in an if statement because you don't want to set something as selected which hasn't been set:
if (isset($_GET['variable']))
{
$key = array_search($_GET['variablename'], $options);
$selectedoption = $options[$key];
doSelect("select_name", 1, $options, $selectedoption, "");
}
else
{
doSelect("select_name", 1, $options, "", "");
}
I hope that helps!