Jump to content

[SOLVED] Table (form) not showing after while loop


hwcasey12

Recommended Posts

I am building a form for work. I have built a table. On one of the inputs, I wanted to build a drop down list and the options to come from the database. Below is the code I am using. My drop down options pull in perfectly, but the rest of my form does not show up. It just ends after the drop down. If I take it out, the whole form will show fine. (it is more than just the rest of my table not showing up, the footer include doesn't show up either).

 

<form method="post" action="index.php">

 

<table border="0" cellspacing="1" cellpadding="1">

<tr>

<td colspan="2"><h2>Enter New Equipment:</h2></td>

</tr>

 

<tr><td colspan="2"> </td></tr>

 

<tr>

<td><h3>Equipment Category:</h3></td>

<td><select name="item">

 

<?php

$con = mysql_connect("localhost", "***", "***") or die('Sorry, could not connect to database server');

mysql_select_db("equipment", $con) or die('Sorry, could not connect to database');

$query = "SELECT item FROM eqmt ORDER BY item DESC";

$result = mysql_query($query) or die('Sorry, could not get equipment categories at this time');

while($row = mysql_fetch_array($result, MYSQL_ASSOC) or die('No records retrieved'))

{

echo "<option>".$row["item"]."</option>\n";

}

?>

 

</select>

</td>

</tr>

 

<tr>

<td><h3>Model:</h3></td>

<td>

<input type="text" name="model" size="35">

</td>

</tr>

 

<tr>

<td><h3>Serial Number:</h3></td>

<td>

<input type="text" name="serial" size="35">

</td>

</tr>

 

<tr>

<td><h3>Purchase Date:</h3></td>

<td>

<input type="Text" name="purchdate" size="35">

</td>

</tr>

 

<tr>

<td><h3>Amount:</h3></td>

<td>

<input type="Text" name="amount" size="35">

</td>

</tr>

 

<tr>

<td><h3>Location:</h3></td>

<td><select name="location">

 

<?php

$con = mysql_connect("localhost", "hwcasey12", "wesley12") or die('Sorry, could not connect to database server');

mysql_select_db("equipment", $con) or die('Sorry, could not connect to database');

$query = "SELECT location FROM location ORDER BY id DESC";

$result = mysql_query($query) or die('Sorry, could not get locations at this time');

while($row = mysql_fetch_array($result, MYSQL_ASSOC) or die('No records retrieved'))

{

echo "<option>".$row['location']."</option>\n";

}

?>

</select>

</td>

</tr>

<tr><td><input type="hidden name" ="content" value="addeqmt">

<input type="submit" value="Add Equipment"></td></tr>

</table>

</form>

Okay

Let break up this line

while($row = mysql_fetch_array($result, MYSQL_ASSOC))

while loops until while the condition is true, and ends the loop when its false

 

now

$row = mysql_fetch_array($result, MYSQL_ASSOC)

$row gets an array from the mysql database unless theirs no more records then it gets a false..

that ends the loop

 

Now with this line

$row = mysql_fetch_array($result, MYSQL_ASSOC) or die('No records retrieved')

if mysql_fetch_array= false it calls

die('No records retrieved')

(remember row is getting its value from mysql_fetch_array so it works both ways)

this is the same as exit.. thus ends the whole script not just the loop

 

make sense ?

Your very welcome

 

Happy PHPing

 

as a tip

when you use mysql_fetch_array($result, MYSQL_ASSOC)

instead of using

or die('No records retrieved')

use

or die(mysql_error())

as it give more info

of course don't try this in a while loop ;)

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.