ChompGator Posted June 1, 2008 Share Posted June 1, 2008 Hello, What would be a PHP script to pre-populate a drop down box with the data in a 'UID' column i have in my db? Quote Link to comment https://forums.phpfreaks.com/topic/108295-help-with-display/ Share on other sites More sharing options...
.josh Posted June 1, 2008 Share Posted June 1, 2008 We don't write scripts for people. We help them work out bugs in their scripts. I will point you in the right direction and say that you need to: -Query your db for the info -Make your form tag and your dropdown tag and then right after that, make a loop that echoes your option tags, with the value being the current iteration of your query's result array. Then after the loop, close out your form elements and tags etc.. and you're good to go. Now that you have a plan, get to it. If you have issues with making your code work, post it and ask away. Quote Link to comment https://forums.phpfreaks.com/topic/108295-help-with-display/#findComment-555210 Share on other sites More sharing options...
ChompGator Posted June 1, 2008 Author Share Posted June 1, 2008 <?php $host="**"; // Host name $username="**"; // Mysql username $password="**"; // Mysql password $db_name="***"; // Database name $tbl_name="**"; // Table name mysql_connect($host, $username, $password)or die("cannot connect to server "); mysql_select_db("$db_name")or die("cannot select DB"); $result = @mysql_query("select * users"); if (mysql_num_rows($result) > 0) { print "<select name=\"select_name\">"; while ($row = mysql_fetch_array($result)) { print "<option value=\"" . $row['table_field1'] . "\">" . $row['table_field2'] . "</option>\n"; } print "</select>"; } mysql_free_result($result); ?> Thats what I got so far Quote Link to comment https://forums.phpfreaks.com/topic/108295-help-with-display/#findComment-555213 Share on other sites More sharing options...
haku Posted June 1, 2008 Share Posted June 1, 2008 Are the columns in your database called 'table_feed1' and 'table_feed2'? If so, then this looks ok. What exactly is the problem you are having? Quote Link to comment https://forums.phpfreaks.com/topic/108295-help-with-display/#findComment-555220 Share on other sites More sharing options...
ChompGator Posted June 2, 2008 Author Share Posted June 2, 2008 well the field names are uid and first so I changed those - but now, its giving me this error Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in D:\hosting\member\aiim\site2\login\exec\new.php on line 12 Warning: mysql_free_result(): supplied argument is not a valid MySQL result resource in D:\hosting\member\aiim\site2\login\exec\new.php on line 19 Quote Link to comment https://forums.phpfreaks.com/topic/108295-help-with-display/#findComment-555224 Share on other sites More sharing options...
.josh Posted June 2, 2008 Share Posted June 2, 2008 $result = @mysql_query("select * from test"); but if your column names are "uid" and "first" then $row['table_field1'] and $row['table_field2'] do not exist, because table_field1 and table_field2 are not column names. Quote Link to comment https://forums.phpfreaks.com/topic/108295-help-with-display/#findComment-555227 Share on other sites More sharing options...
haku Posted June 2, 2008 Share Posted June 2, 2008 This means that either: 1) Your database connection wasn't made 2) There was an error in your mysql syntax, resulting in no result from your query. It's almost definitely point two, as you missed the word 'from'. On that note, you really shouldn't use the '@' symbol, pretty much ever. It masks errors, which means that you miss problems like this. Change your query to read like this: $result = mysql_query("select * from users") or die(mysql_error()); although I personally would use: $result = mysql_query("select uid from users") or die(mysql_error()); As selecting '*' is inefficient, and in some cases can present a security issue. Quote Link to comment https://forums.phpfreaks.com/topic/108295-help-with-display/#findComment-555228 Share on other sites More sharing options...
ChompGator Posted June 2, 2008 Author Share Posted June 2, 2008 Hey, awesome the drop down shows now, but it doesn't show anything in the drop down... What kind of security issue's does it create using the *? Quote Link to comment https://forums.phpfreaks.com/topic/108295-help-with-display/#findComment-555231 Share on other sites More sharing options...
.josh Posted June 2, 2008 Share Posted June 2, 2008 Hey, awesome the drop down shows now, but it doesn't show anything in the drop down... What kind of security issue's does it create using the *? did you change your $row['...'] 's to your actual column names? * is inefficient because you are quering for more information than you need. It selects every piece of data from every row and column of your table. That takes more processing time and memory use. It's a waste. It's potentially dangerous because if someone manages to hack your scripts to dump all your variables in memory, well, for instance, your table could have a name and password column and then mr. hacker now has everybody's name and password. Or credit card info, or whatever else your table might contain. Quote Link to comment https://forums.phpfreaks.com/topic/108295-help-with-display/#findComment-555248 Share on other sites More sharing options...
ChompGator Posted June 2, 2008 Author Share Posted June 2, 2008 Hey, awesome the drop down shows now, but it doesn't show anything in the drop down... What kind of security issue's does it create using the *? did you change your $row['...'] 's to your actual column names? * is inefficient because you are quering for more information than you need. It selects every piece of data from every row and column of your table. That takes more processing time and memory use. It's a waste. It's potentially dangerous because if someone manages to hack your scripts to dump all your variables in memory, well, for instance, your table could have a name and password column and then mr. hacker now has everybody's name and password. Or credit card info, or whatever else your table might contain. Wow, I did not know that at all...Thank you for the tip! I changed my field names yep, this is how it looks now $result = mysql_query("select uid from users") or die(mysql_error()); if (mysql_num_rows($result) > 0) { print "<select name=\"select_name\">"; while ($row = mysql_fetch_array($result)) { print "<option value=\"" . $row['uid'] . "\">" . $row['first'] . "</option>\n"; } print "</select>"; } mysql_free_result($result); ?> Quote Link to comment https://forums.phpfreaks.com/topic/108295-help-with-display/#findComment-555260 Share on other sites More sharing options...
.josh Posted June 2, 2008 Share Posted June 2, 2008 so..is it working? Quote Link to comment https://forums.phpfreaks.com/topic/108295-help-with-display/#findComment-555275 Share on other sites More sharing options...
haku Posted June 2, 2008 Share Posted June 2, 2008 If you look at this code: print "<option value=\"" . $row['uid'] . "\">" . $row['first'] . "</option>\n"; You are calling $row['uid'] and $row['first'], but you have only extracted 'uid' from the database. Since you haven't extracted 'first', all of the names will be empty in the drop down list, although the values will be there. So you need to change your database query to this: "select uid, first from users" Quote Link to comment https://forums.phpfreaks.com/topic/108295-help-with-display/#findComment-555300 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.