FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


I could use some help with dropdown lists in php





Poetsunited
So I encountered the following problem... I was making a site with 2 drop down lists one listing all my users and another listing all the possible items, but the items all have a value, so I wanted to have my script get the value from database... but for some reason it doesn't... any ideas? Sad

<?php
require "log.php";
include 'connect.php';

echo '<form action="manageusers.php" method="POST">';
echo '<select name="user">';
echo '<option value=""> ----> </option>';
$result = mysql_query("SELECT * FROM `APS`");
while($row = mysql_fetch_array($result))
{
echo '<option value="' . $row[login] . '">' . $row[login] . '</option>' ;
}
echo '</select>';
echo '<select name="item">';
echo '<option value=""> ----> </option>';
$result2 = mysql_query("SELECT * FROM `items`");
while($row2 = mysql_fetch_array($result2))
{
echo '<option value="' . $row2[itemname] . '">' . $row2[itemname] . ' - ' . $row2[itemvalue] . '</option>' ;
}
echo '</select>';
echo '<input type="hidden" name="value" value="' . $row2[itemvalue] . '">';
echo '<input type="submit" value="award">';
echo '</form>';

$name = $_POST[user];
$item = $_POST[item];
$value = $_POST[value];


if ($value == "")
echo "Please select something in the form above!";
else
{
mysql_query("INSERT INTO history (`username`,`reason`,`value`,`admin`) VALUES ('$name','$item','$value',' ')");
echo "Added to database!";
}



?>
jmraker
It looks like you're using the constant login in $row[login] where $row['login'] would be more valid.

You should try adding a var_dump($row) or print_r($row) inside the first loop see if $row[login] is valid
Same with $row2 in the 2nd loop and view the page source

If you don't see any options and no debug output, you probably have the SQL wrong or no data in the tables.
Poetsunited
The problem is with the value... for some reason it won't get the value from the posted $row2[itemname]... and I have no idea how to make it get the value needed, i somehow need to get it to get the value as i select from the drop down list...
I thought that code would do it but it doesn't...

any ideas?
kacsababa
$row2['itemname'] not just [itemname]. Type the keys as strings not constants. And thats for all arrays.
jmraker
What I would do is add a var_dump($row2); inside the loop like below, the exit() is so you don't see 100's of dumps, and the dump is at the end of the page instead of somewhere in the middle

Code:
while($row2 = mysql_fetch_array($result2))
{
var_dump($row2);
exit();
echo '<option value="' . $row2[itemname] . '">' . $row2[itemname] . ' - ' . $row2[itemvalue] . '</option>' ;
}


Then load the page, everything inside of $row2 should be on the screen (but only the 1st record), if you want to see it better view the source of the page. the var_dump should contain the field name and/or a field number that's what you want, if you don't see the right fields being used, you might be using the wrong table, test if the query in phpmyadmin works
CosmicDisturbance
Put ' in the brackets, that might fix the problem.
Related topics
help me upgrade php to 5.
How To : Improve Your PHP Programming
Filter File Types in PHP File Upload
a game online
phpBB - Installation Tutorial
myBBoard - Installation Tutorial
php dropdown lists with if statements or switch
need help enabling GD php extension
[ HELP] Problems with PHP and Perl
Need help fixing my php file
a bit of help with my php.ini, please?
Please can anyone help with a php script error/problem?
Help With Simple PHP Code Snipit
Help coding a php back end for an xml website! Radio646.com
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.