Page 1 of 1

PHP Update Form with drop down menu

Posted: Wed Jul 01, 2009 7:25 am
by pauldr
Thank you all for your help with coding.

I have a page that allows users to update a database. I am using drop down menus which are being populated by the database. What I am trying to figure out is how to allow a user to change that information. I tried using an array, but all it shows in the drop down menu is 'Array'. My goal is to display the current data in the database, and then allow a user to change it based upon data being pulled from another table.

My code is posted below:

Code: Select all

<html>
 
<!-- Header -->
<? include("../include/header.php"); ?>
 
<!-- Menu -->
<? include("../include/menu.php"); ?>
 
<div id="main">
<!-- Begin Content -->
 
<?php 
require('../include/connect.php');
 
$id_update = $_GET['id'];
 
if (! is_numeric($id_update))
{
    die('Error: ID is not numeric.'); 
}
 
$result = mysql_query('SELECT 
                        item.id,
                        item.category_id,
                        category.name,
                        item.short_desc,
                        item.part_no,
                        item.vendor,
                        item.unit_price,
                        item.long_desc,
                        item.max_qty,
                        item.qty,
                        item.type_id,
                        type.type
                       FROM
                        item, 
                        category,
                        type
                       WHERE 
                        item.id = ' . $id_update . ' 
                            AND 
                                category.id = item.category_id
                            AND
                                type.id = item.type_id');
 
while($row = mysql_fetch_array($result))
    {
 
/*
echo <pre>;
print_r($row);
echo </pre>;
*/
        
    $id = $row['id'];
    $cat_id = $row['category_id'];
    $cat_name = $row['name']; 
    $short_desc = $row['short_desc']; 
    $part_no = $row['part_no']; 
    $vendor = $row['vendor']; 
    $unit_price = $row['unit_price']; 
    $long_desc = $row['long_desc']; 
    $max_qty = $row['max_qty']; 
    $qty = $row['qty'];
    $type = $row['type'];
    $type_id = $row['type_id'];
            
    }
    
$result_c = mysql_query('SELECT
                            id,
                            name
                        FROM
                            category');
 
while($row_c = mysql_fetch_array($result_c))
    {
        $id_c = array($cat_id, $row_c['id']);
        $name_c = array($cat_name, $row_c['name']);
    }
    
$result_t = mysql_query('SELECT
                            id,
                            type
                        FROM
                            type');
 
while($row_t = mysql_fetch_array($result_t))
    {
        $row_t['id'];
        $row_t['type'];
    }
    
$id_t = array();
$type_t = array();
 
?>
<form id="site_form" action='update.php' method='post'>
    <input type="hidden" name="id" value="<?=$id_update?>" />
    <table border='0'>
        <tr>
            <!-- <th>Category</th> -->
            <th>Category</th>
            <th>Short Description</th>
            <th>Part No</th>
            <th>Vendor</th>
            <th>Unit Price</th>
            <th>Description</th>
            <th>Max Qty</th>
            <th>Qty</th>
            <th></th>
            <th>Update</th>
        </tr>
        <tr valign='top'>
            <td>
                <select name='category'>
                <option value = <?php echo "$id_c"?>> <?php echo "$name_c"?> </option>                  
                </select>
            </td>
            <td>
                <select name='category_c'>
                <option value = <?php echo "$cat_id"?>> <?php echo "$cat_name"?> </option>                  
                </select>
            </td>
            <td><input type='text' name='short_desc' value='<?php echo "$short_desc" ?>'></td>
            <td><input type='text' name='part_no' value='<?php echo "$part_no" ?>'></td>
            <td><input type='text' name='vendor' value='<?php echo "$vendor" ?>'></td>
            <td><input type='text' size='8' name='unit_price' value='<?php echo "$unit_price" ?>'></td>
            <td><textarea rows='3' cols='20' name='long_desc'><?php echo "$long_desc" ?></textarea></td>
            <td><input type='text' size='1' name='max_qty' value='<?php echo "$max_qty" ?>'></td>
            <td><input type='text' size='1' name='qty' value='<?php echo "$qty" ?>'></td>
            <td>
                <select name='category'>
                    <?php 
                    
                    echo "<option value =" . $type_id . ">" . $type . "</option>";
                    ?>                      
                </select>
            </td>   
            <td> <a href='item.php'>No</a> </td>
            <td> <a href="javascript&#058;document.forms['site_form'].submit();">Yes</a> </td>
        </tr>
    </table>
</form>
<!-- End Content -->
    
<!-- Footer -->
<? include("../include/footer.php"); ?>
Thank you!

Re: PHP Update Form with drop down menu

Posted: Thu Jul 02, 2009 4:52 am
by pauldr
McInfo, thank you so much for the clear explanations! I have it working now!

Thank you,
Paul