hwcasey12 Posted January 28, 2009 Share Posted January 28, 2009 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> Link to comment https://forums.phpfreaks.com/topic/142722-solved-table-form-not-showing-after-while-loop/ Share on other sites More sharing options...
MadTechie Posted January 28, 2009 Share Posted January 28, 2009 change while($row = mysql_fetch_array($result, MYSQL_ASSOC) or die('No records retrieved')) to while($row = mysql_fetch_array($result, MYSQL_ASSOC) ) Link to comment https://forums.phpfreaks.com/topic/142722-solved-table-form-not-showing-after-while-loop/#findComment-748126 Share on other sites More sharing options...
hwcasey12 Posted January 28, 2009 Author Share Posted January 28, 2009 That worked! May I ask why? I like to understand errors. Basically, I shouldn't run "or die" when using the array to pull in data for a form? Link to comment https://forums.phpfreaks.com/topic/142722-solved-table-form-not-showing-after-while-loop/#findComment-748130 Share on other sites More sharing options...
MadTechie Posted January 28, 2009 Share Posted January 28, 2009 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 ? Link to comment https://forums.phpfreaks.com/topic/142722-solved-table-form-not-showing-after-while-loop/#findComment-748134 Share on other sites More sharing options...
hwcasey12 Posted January 28, 2009 Author Share Posted January 28, 2009 It does! I just started learning PHP in this month and I LOVE IT! Thanks for the help! Link to comment https://forums.phpfreaks.com/topic/142722-solved-table-form-not-showing-after-while-loop/#findComment-748138 Share on other sites More sharing options...
MadTechie Posted January 28, 2009 Share Posted January 28, 2009 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 Link to comment https://forums.phpfreaks.com/topic/142722-solved-table-form-not-showing-after-while-loop/#findComment-748147 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.