Demont Posted November 19, 2008 Share Posted November 19, 2008 I have a script I wrote to display information from the database into drop down menus, but NONE of the information from the database is being displayed... addroom.php <?php // Connects to your Database include("connect.php"); include("userinfo.php"); $result = mysql_query("SELECT * FROM rooms WHERE room='$roomin'"); while($row = mysql_fetch_array($result)){ $roomnumber=$row['room']; $roomname=$row['roomname']; $roomdesc=$row['roomdesc']; $north=$row['north']; $south=$row['south']; $east=$row['east']; $west=$row['west']; $up=$row['up']; $down=$row['down']; $mnorth=$row['mnorth']; $msouth=$row['msouth']; $meast=$row['meast']; $mwest=$row['mwest']; $mup=$row['mup']; $mdown=$row['mdown']; } //This code runs if the form has been submitted if (isset($_POST['submit'])) { //This makes sure they did not leave any fields blank if (!$_POST['roomname'] | !$_POST['roomdesc'] ) { die('You did not complete all of the required fields'); } // now we insert it into the database $insert = "INSERT INTO rooms (roomname,roomdesc,north,south,east,west,up,down,mnorth,msouth,meast,mwest,mup,mdown) VALUES ('".$_POST['roomname']."','".$_POST['desc']."','".$_POST['north']."','".$_POST['south']."','".$_POST['east']."','".$_POST['west']."','".$_POST['up']."', '".$_POST['down']."','".$_POST['mnorth']."','".$_POST['msouth']."','".$_POST['meast']."','".$_POST['mwest']."','".$_POST['mup']."', '".$_POST['mdown']."')"; $add_member = mysql_query($insert); ?> <title>The Roman Empire</title> <body bgcolor="grey"> <table width="800" border="1" cellpadding="0" align="center" bgColor="CC0000"> <!-- Row 1 --> <tr> <td width="800" align="center" valign="middle"> <font size="5"><font color="gold">The Roman Empire</font></font> <br> <a href="http://www.tregame.com" border="0"><img src="/img/spqr.jpg"></a> <br> </td> </tr> <tr> <td width="800" align="center" valign="middle"> <center><h1>Added/Created</h1> <p>Room Added/Created, go <a href="addroom.php">Back</a>.</p></center> </td> </tr> <?php } else { ?> <body bgcolor="grey"> <table width="800" border="1" cellpadding="0" align="center" bgColor="CC0000"> <!-- Row 1 --> <tr> <td width="800" align="center" valign="middle"> <font size="5"><font color="gold">The Roman Empire</font></font> <br> <a href="http://www.tregame.com" border="0"><img src="/img/spqr.jpg"></a> <br> You are editing <? Print $roomname; ?> </td> </tr> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <table width="800" border="1" cellpadding="0" align="center" bgColor="CC0000"> <tr> <td width="400" align="center" valign="middle"> Room Name: </td> <td width="400" align="center" valign="middle"> <INPUT TYPE="text" NAME="roomname" SIZE="30" value="<? Print $roomname; ?>"> </td> </tr> <tr> <td width="400" align="center" valign="middle"> Room Description: </td> <td width="400" align="center" valign="middle"> <TEXTAREA NAME="roomdesc" ROWS=6 COLS=40 value="<? Print $roomdesc; ?>"> </TEXTAREA> </td> </tr> <tr> <td width="400" align="center" valign="middle"> Connect North To(Click checkbox too): </td> <td width="400" align="center" valign="middle"> <SELECT NAME="north" SIZE="1"> <option selected><? Print $roomnumber; ?> </SELECT> <INPUT TYPE="checkbox" NAME="mnorth" VALUE="1"> </td> </tr> <tr> <td width="400" align="center" valign="middle"> Connect South To(Click checkbox too if able to move south): </td> <td width="400" align="center" valign="middle"> <SELECT NAME="south" SIZE="1"> <option selected><? Print $roomnumber; ?> </SELECT> <INPUT TYPE="checkbox" NAME="msouth" VALUE="1"> </td> </tr> <tr> <td width="400" align="center" valign="middle"> Connect East To(Click checkbox too if able to move east): </td> <td width="400" align="center" valign="middle"> <SELECT NAME="east" SIZE="1"> <option selected><? Print $roomnumber; ?> </SELECT> <INPUT TYPE="checkbox" NAME="meast" VALUE="1"> </td> </tr> <tr> <td width="400" align="center" valign="middle"> Connect West To(Click checkbox too if able to move west): </td> <td width="400" align="center" valign="middle"> <SELECT NAME="west" SIZE="1"> <option selected><? Print $roomnumber; ?> </SELECT> <INPUT TYPE="checkbox" NAME="mwest" VALUE="1"> </td> </tr> <tr> <td width="400" align="center" valign="middle"> Connect Up To(Click checkbox too if able to move up): </td> <td width="400" align="center" valign="middle"> <SELECT NAME="up" SIZE="1"> <option selected><? Print $roomnumber; ?> </SELECT> <INPUT TYPE="checkbox" NAME="mup" VALUE="1"> </td> </tr> <tr> <td width="400" align="center" valign="middle"> Connect Down To(Click checkbox too if able to move down): </td> <td width="400" align="center" valign="middle"> <SELECT NAME="down" SIZE="1"> <option selected><? Print $roomnumber; ?> </SELECT> <INPUT TYPE="checkbox" NAME="mdown" VALUE="1"> </td> </tr> <tr><th colspan=2><input type="submit" name="submit" value="Add Room"></th></tr> </table> </form> <table width="800" border="1" cellpadding="0" align="center" bgColor="CC0000"> <!-- Row 1 --> <tr> <td width="800" align="center" valign="middle"> <center><b><p>If you want the room to be able to connect to another, you have to click the check box.</b></p></center> </td> </tr> </table> <?php } ?> Quote Link to comment https://forums.phpfreaks.com/topic/133269-help-with-displaying-database-info-in-dropdown-menus/ Share on other sites More sharing options...
gevans Posted November 19, 2008 Share Posted November 19, 2008 To start with you should try checking that you're getting info from your query, and if not, why; $result = mysql_query("SELECT * FROM rooms WHERE room='$roomin'"); if(!$result) die('Invalid query: ' . mysql_error()); Also try and avoid using short tags '<?' keep to the full ones <?php Quote Link to comment https://forums.phpfreaks.com/topic/133269-help-with-displaying-database-info-in-dropdown-menus/#findComment-693128 Share on other sites More sharing options...
Demont Posted November 19, 2008 Author Share Posted November 19, 2008 Invalid query: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) Quote Link to comment https://forums.phpfreaks.com/topic/133269-help-with-displaying-database-info-in-dropdown-menus/#findComment-693135 Share on other sites More sharing options...
trq Posted November 19, 2008 Share Posted November 19, 2008 Use 127.0.0.1 instead of localhost as the host argument to mysql_connect. It would appear your mysql install is misconfigured. Quote Link to comment https://forums.phpfreaks.com/topic/133269-help-with-displaying-database-info-in-dropdown-menus/#findComment-693137 Share on other sites More sharing options...
MatthewJ Posted November 19, 2008 Share Posted November 19, 2008 also, you are using mysql_fetch_array, but are then referencing the data with $row[columnname'] You would need to use mysql_fetch_assoc() or reference the data with the array key instead $row[0] Quote Link to comment https://forums.phpfreaks.com/topic/133269-help-with-displaying-database-info-in-dropdown-menus/#findComment-693194 Share on other sites More sharing options...
redarrow Posted November 19, 2008 Share Posted November 19, 2008 un protected database........ Real programmers dont use $_POST[''] in there mysql querys you need to add mysql_real_escape_string()<< on all varables your database problam ((lession to what member thorpe told you))...... <?php // now we insert it into the database $insert = "INSERT INTO rooms (roomname,roomdesc,north,south,east,west,up,down,mnorth,msouth,meast,mwest,mup,mdown) VALUES ('".$_POST['roomname']."','".$_POST['desc']."','".$_POST['north']."','".$_POST['south']."','".$_POST['east']."','".$_POST['west']."','".$_POST['up']."', '".$_POST['down']."','".$_POST['mnorth']."','".$_POST['msouth']."','".$_POST['meast']."','".$_POST['mwest']."','".$_POST['mup']."', '".$_POST['mdown']."')"; $add_member = mysql_query($insert)or die(mysql_error()); ?> Quote Link to comment https://forums.phpfreaks.com/topic/133269-help-with-displaying-database-info-in-dropdown-menus/#findComment-693205 Share on other sites More sharing options...
Demont Posted November 19, 2008 Author Share Posted November 19, 2008 Figured it out. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/133269-help-with-displaying-database-info-in-dropdown-menus/#findComment-693571 Share on other sites More sharing options...
Andy-H Posted November 19, 2008 Share Posted November 19, 2008 also, you are using mysql_fetch_array, but are then referencing the data with $row[columnname'] You would need to use mysql_fetch_assoc() or reference the data with the array key instead $row[0] Actually mysql_fetch_array fetches data and assigns it associative AND indexed keys, maybe your thinking of mysql_fetch_row() ? Quote Link to comment https://forums.phpfreaks.com/topic/133269-help-with-displaying-database-info-in-dropdown-menus/#findComment-693574 Share on other sites More sharing options...
MatthewJ Posted November 19, 2008 Share Posted November 19, 2008 I was... my bad, mysql_fetch_assoc would be slightly faster though I do believe as mysql_fetch_array returns both index types correct? Quote Link to comment https://forums.phpfreaks.com/topic/133269-help-with-displaying-database-info-in-dropdown-menus/#findComment-693583 Share on other sites More sharing options...
MatthewJ Posted November 19, 2008 Share Posted November 19, 2008 Real programmers dont use $_POST[''] in there mysql querys Real programmers sometimes also develop in closed systems... where for development purposes, there is no need to add the security. Good practice sure... but when my boss wants to see the code results before lunch, I crack out the code and secure it before I launch. One should never assume. A better line may have been something like "I know you're going to escape your data before making this application live, that said..." instead of trying to make it sound like the guy doesn't know what he is doing. Quote Link to comment https://forums.phpfreaks.com/topic/133269-help-with-displaying-database-info-in-dropdown-menus/#findComment-693591 Share on other sites More sharing options...
Andy-H Posted November 19, 2008 Share Posted November 19, 2008 I was... my bad, mysql_fetch_assoc would be slightly faster though I do believe as mysql_fetch_array returns both index types correct? I would believe so, however I thaught using count() inside a for loop instead of assigning it to a variable would be slower, yet some guy did a microtime test and proved me wrong lol Quote Link to comment https://forums.phpfreaks.com/topic/133269-help-with-displaying-database-info-in-dropdown-menus/#findComment-693605 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.