我在addproduct.php下面的代碼連接點擊提交按鈕後doaddproduct.php:數據插入到數據庫 - PHP MYSQL
<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)從數據庫中提供選項(表:類別,列:categoryname)。例如,categoryID'1'的categoryname是food,categoryid'2'的categoryname是drink,等等。
現在,我想從選擇下拉選項中的數據插入到我的數據庫,通過改變所選擇的類別名稱爲的categoryID(表:的categoryID:產品,列)。例如,我從下拉菜單中選擇「喝酒」,當我點擊提交按鈕後,程序將數據插入到表產品:
|產品ID | CategoryID | ProductName |
| 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");
}
}
?>
我應該怎麼辦?
是什麼問題? 你的故事嗎? –
@RajeevRanjan問題的錯誤是我無法改變的選擇的下拉菜單選項(類別名)到CATEGORYID這樣我就可以將其插入到我的新表(這是表產品) – noobprogrammer
您