0

我创建了一个基本的管理界面来将项目上传到一个小型的“网上商店”数据库中。该页面不会有购物车系统,因为页面上不会发生任何交易。整个页面仅用于演示。我不得不提到我已经复制了一些代码,一切都是为了学校项目。

mysql 数据库有一个包含列的表:id、product_name、price、details、category、subcategory、date_added"。以下表单和 php 代码管理在数据库中插入新值

我希望“子类别”字段根据所选类别仅显示几个选项。例如。如果我选择 category=value1,子类别只会让我在“valuex”和“valuey”之间进行选择,因此管理员不能混淆。

如何填充子类别下拉列表?

这是PHP部分

$product_name = mysql_real_escape_string($_POST['product_name']);
$price = mysql_real_escape_string($_POST['price']);
$category = mysql_real_escape_string($_POST['category']);
$subcategory = mysql_real_escape_string($_POST['subcategory']);
$details = mysql_real_escape_string($_POST['details']);
// See if that product name is an identical match to another product in the system
$sql = mysql_query("SELECT id FROM products WHERE product_name='$product_name' LIMIT 1");
$productMatch = mysql_num_rows($sql); // count the output amount
if ($productMatch > 0) {
    echo 'Sorry, rename your product, <a href="inventory_list.php">click here</a>';
    exit();
}
// Adds to database database
$sql = mysql_query("INSERT INTO products (product_name, price, details, category, subcategory, date_added) 
    VALUES('$product_name','$price','$details','$category','$subcategory',now())") or die (mysql_error());
 $pid = mysql_insert_id();
// Place image in the folder 
$newname = "$pid.jpg";
move_uploaded_file( $_FILES['fileField']['tmp_name'], "../inventory_images/$newname");
header("location: inventory_list.php"); 
exit();

这就是 HTML 表单

<div align="right"><a href="inventory_list.php#inventoryForm">+ Add New Inventory Item</a></div>

 <a name="inventoryForm" id="inventoryForm"></a>
<h3>
Add New Inventory Item Form
</h3>
<form action="inventory_list.php" enctype="multipart/form-data" name="myForm" id="myform" method="post">
  <tr>
    <td>Product Name</td>
    <td><label>
      <input name="product_name" type="text" id="product_name" size="64" />
    </label></td>
  </tr>
  <tr>
    <td align="left">Product Price</td>
    <td><label>
      €
          <input name="price" type="text" id="price" size="12" />
    </label></td>
  </tr>
  <tr>
    <td align="left">Category</td>
    <td><label>
      <select name="category" id="category">
      <option value="value1">value1</option>
      <option value="value2">value2</option>
      <option value="value3">value3</option>
      </select>
    </label></td>
  </tr>
  <tr>
    <td align="left">Subcategory</td>
    <td><select name="subcategory" id="subcategory">
    <option value=""></option>
      <option value="valuex">valuex</option>
      <option value="valuey">valuey</option>
      <option value="valuez">valuez</option>
      </select></td>
  </tr>
  <tr>
    <td align="left">Product Details</td>
    <td><label>
      <textarea name="details" id="details" cols="64" rows="5"></textarea>
    </label></td>
  </tr>
  <tr>
    <td align="left">Product Image</td>
    <td><label>
      <input type="file" name="fileField" id="fileField" />
    </label></td>
  </tr>      
  <tr>
    <td>&nbsp;</td>
    <td><label>
      <input type="submit" name="button" id="button" value="Add Item Now" />
    </label></td>
  </tr>
</table>
</form>

谢谢大家!

4

1 回答 1

0

尝试使用此代码,这将获取类别和子类别并根据您选择的选项显示选项。请告诉我这是可行的。

 **<select name="category" id="category"><?php 
        $category = mysql_query("SELECT category FROM products");
              while ($sql = mysql_fetch_array($category)) { ?>
                    <option value="value1"><?php echo $sql['category']; ?></option>
                 <?php } ?> </select>  $category = mysql_fetch_array($category); 
    $categoryname = $category['category']; 
    $sqls = mysql_query("SELECT subcategory FROM products where category
            ='$categoryname'"); ?> <select name="subcategory" id="subcategory">
                <?php    $subcategory = mysql_fetch_array($sqls);
                while (mysql_fetch_array($subcategory)) {        ?>
                    <option value="value1"><?php echo $subcategory['subcategory']; ?></option>
                <?php } ?> </select>**
于 2013-04-24T08:05:28.353 回答