我的 addproduct.php 中有以下代码,它在单击提交按钮后连接到 doaddproduct.php:
<tr>
<td>Category Name</td>
<td>:</td>
<td>
<select name="categoryname" id="">
<option value="">Select</option>
<?php
while($row=mysql_fetch_array($result)) {
?>
<option value="<?php echo $row['CategoryID']; ?>">
<?php echo $row['CategoryName']; ?></option>
<?php } ?>
</select>
</td>
</tr>
因此,下拉菜单 (addproduct.php) 提供了来自数据库的选项(表:类别,列:类别名称)。例如,categoryID '1' 的 categoryname 是 food,categoryid '2' 的 categoryname 是drink 等等。
现在,我想通过将所选类别名称更改为 categoryID 来将所选下拉选项中的数据插入我的数据库(表:产品,列:categoryID)。例如,我从下拉菜单中选择“drink”,当我单击提交按钮时,程序将数据插入到表产品中:
|产品编号 | 类别ID | 产品名称 |
| 123231 | 2 | 可口可乐 |
这是我的 doaddproduct.php 中的以下代码:
<?php
include("connect.php");
$productname = $_POST['productname'];
$categoryname = $_POST['categoryname'];
$stock = $_POST['stock'];
$price = $_POST['price'];
if($productname == NULL || $productname == ""){
header("location:../addproduct.php?err=You must fill product name");
}else if($categoryname== "none"){
header("location:../addproduct.php?err=You must choose category name");
}else if($stock == NULL || $stock == ""){
header("location:../addproduct.php?err=You must fill stock");
}else if($price == NULL || $price == ""){
header("location:../addproduct.php?err=You must fill price");
}else if($_FILES["file"]["type"] != "image/jpeg" && $_FILES["file"]["type"] != "image/png" && $_FILES["file"]["type"] != "image/jpg"){
header("location:../addproduct.php?err=Extention of your photo must be jpg/jpeg/png");
}else{
$query = $username . "-" .$fullname . "-" . $phone ."-".$email."-".$password."-".$rpassword."-".$address."-".$gender;
$qcek = "select * from product where productname = '$productname'";
$result = mysql_query($qcek);
if(mysql_num_rows($result) > 0){
header("location:../addproduct.php?err=Product name already used");
}else{
//for upload
$ext = substr($_FILES["file"]["name"], strrpos($_FILES["file"]["name"], '.'));
move_uploaded_file($_FILES["file"]["tmp_name"],"../photos/" . $username . $ext);
$pho = $username . $ext;
$password = md5($password);
//still confused here
$query = "insert into product values('', '', '$productname', '$pho', '$stock', '$price')";
mysql_query($query);
header("location:../addproduct.php?err=Success");
}
}
?>
我应该怎么办?