I have the following code in my addproduct.php which connects to doaddproduct.php after clicking a submit button:
<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>
So, the dropdown menu (addproduct.php) gives options from the database (table: category, column: categoryname). For example the categoryname for categoryID '1' is food, the categoryname for categoryid '2' is drink and so on.
Now, I want to insert the data from selected dropdown option to my database (table: product, column: categoryID) by changing the selected categoryname into categoryID. For example, I select 'drink' from dropdown menu and when I click the submit button, the program inserts the data into table product:
|ProductID | CategoryID | ProductName |
| 123231 | 2 | Coca-cola |
Here's my following code in my 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");
}
}
?>
What should I do?