Jump to content

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

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.