Putting records into a list box.

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Keanman
Forum Newbie
Posts: 18
Joined: Fri Jun 13, 2003 3:19 pm
Contact:

Putting records into a list box.

Post by Keanman »

Ok guys and gals,

I'm having a bit of trouble trying to get some records from a database and into a list box. After this is done I also want the location of a speceific record displayed when the page loads. Anybody have any idea why this isn't working?

Code: Select all

<? 
include("details.inc"); 

//post values into the variables from the search.php form 
$asset = stripslashes(addslashes($_POST['asset'])); 
$name = stripslashes(addslashes($_POST['name'])); 
$location = stripslashes(addslashes($_POST['location'])); 
$po = stripslashes(addslashes($_POST['po'])); 
$warranty = stripslashes(addslashes($_POST['warranty'])); 
$serial = stripslashes(addslashes($_POST['serial'])); 

//get id num into the $id variable, else post 
if(isset($_GET['id'])){ 
$id=$_GET['id']; 
}else{ 
$id = $_POST['id']; 
} 

$query="SELECT * FROM assets WHERE id=".$id; 
$result=mysql_query($query); 
$rs = mysql_fetch_array($result); 

mysql_query($query) or die (mysql_error()); 

$searching=$_GET['searching']; 
$search=$_GET['search']; 

//if the delete button is clicked, delete the current record and display to the user that it was deleted 
if(isset($_POST['btnDelete'])) { 
$query="DELETE FROM assets WHERE id=".$id; 
mysql_query($query) or die (mysql_error()); 
header("Location: search.php?searching=".$searching."&search=".$search); 
} 

//if the update button is clicked, update the current record and display to the user that it was updated 
if(isset($_POST['btnUpdate'])) { 
$query = "UPDATE assets SET serialNum = '".$serial."', assetNum = '".$asset."', itemName = '".$name."',location = '".$location."', po= '".$po."',warranty = '".$warranty."' WHERE id = ".$id; 

mysql_query($query) or die (mysql_error()); 
header("Location: search.php?searching=".$searching."&search=".$search); 
} 

include("header.inc"); 
include("menu.inc"); 
?> 

<TR ALIGN="CENTER"> 
   <TD><CENTER><H2>Update Database</H2></CENTER><P></TD> 
</TR> 

<form method="post" action="<?php echo $PHP_SELF;?>"> 

<TR ALIGN="CENTER"> 
   <TD> 
      <TABLE BORDER=1 WIDTH=40% ALIGN="CENTER"> 
      <TR ALIGN=CENTER> 
        <TD><B>Item Name</B></TD> 
        <TD><Input type="text" name="name" size="20" value="<? echo $rs['itemName'] ?>"></TD> 
      </TR> 
      <TR ALIGN=CENTER> 
         <TD><B>Serial Number</B></TD> 
         <TD><Input type="text" name="serial" size="20" value="<? echo $rs['serialNum'] ?>"></TD> 
      </TR> 
      <TR ALIGN=CENTER> 
         <TD><B>Asset Number</B></TD> 
         <TD><Input type="text" name="asset" size="20" value="<? echo $rs['assetNum'] ?>"></TD> 
      </TR> 
      <TR ALIGN=CENTER> 
         <TD><B>Location</B></TD><TD> 

      <? 
      print ("<select>"); 
      while ($row = mysql_fetch_array($result)) { 
      print ("<option> {$row['location']} </option>\n"); 
      } 
      print ("</select>"); 
      ?> 

         </TR> 
      <TR ALIGN=CENTER> 
         <TD><B>P.O. #</B></TD> 
         <TD><Input type="text" name="po" size="20" value="<? echo $rs['po'] ?>"></TD> 
      </TR> 
      <TR ALIGN=CENTER> 
         <TD><B>Warranty</B></TD> 
         <TD><Input type="text" name="warranty" size="20" value="<? echo $rs['warranty'] ?>"></TD> 
      </TR> 
      </TABLE> 
      <BR><CENTER> 
      <input type="Submit" name="btnUpdate" Value="Update Record"> 
       <input type="Submit" name="btnDelete" Value="Delete Record"> 
      </FORM></CENTER> 
   </TD> 
</TR> 

<? 
include("footer.inc"); 
?>
tylerdurden
Forum Commoner
Posts: 66
Joined: Mon Jul 28, 2003 11:52 am
Location: Austria

Post by tylerdurden »

Try a

Code: Select all

echo "<pre>";
print_r($variable);
echo "</pre>
once in a while to check what your variables contain. Your handling of the mysql_query result seems a bit odd. You use

Code: Select all

$rs = mysql_fetch_array($result);
and later

Code: Select all

while ($row = mysql_fetch_array($result)) {
That way the first result row is ignored in the while loop.
Post Reply