Jump to content

Help with displaying database info in dropdown menu's


Recommended Posts

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

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

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());
?>

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

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.

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.