Hi all,
I have 2 tables. tbl_category & tbl_products
Im trying to retrieve the category value from tbl-category into a dropdown list using select and then insert the value selected into tbl_products column category_name on form submission.
I have the dropdown working and I am retrieving the categories correctly however when posting the form nothing gets inserted into tbl_products and no error messages are displayed on screen.
HTML FORM ACTION
<pre><form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post" enctype='multipart/form-data'>
<p>
<label for="name">Product Category:</label>
<?php include 'global.php';
echo '<select id="category_name" name="category_name">';
$sql = mysql_query("SELECT category_name FROM tbl_category");
while ($row = mysql_fetch_array($sql)){
echo "<option value='".$row['category_name']."'>".$row['category_name']."</option>";
}
echo '</select>';
?>
<input name="submit" type="submit" class="btn-action" value="Add Product">
</form>
PHP CODE
<?php
include 'global.php';
$code = mysqli_real_escape_string($link, $_REQUEST['code']);
$name = mysqli_real_escape_string($link, $_REQUEST['name']);
$price = mysqli_real_escape_string($link, $_REQUEST['price']);
if(isset($_POST['submit'])) {
$category_name = $_POST['category_name'];
$target_dir = "product/";
$target_file = $target_dir . basename($_FILES["imageUpload"]["name"]);
$uploadOk = 1;
$extensions_arr = array("jpg","jpeg","png","gif");
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION,$extensions_arr);
if (move_uploaded_file($_FILES["imageUpload"]["tmp_name"], $target_file)) {
} else {
echo "Issue with image";echo "</br>";
}
$image=basename($_FILES["imageUpload"]["name"]);
$sql = "insert into tbl_product(code,category_name,name,price,image)
values('".$code."','".$name."','".$category_name."','".$price."', '".$target_file."')";
if(mysqli_query($link, $sql)){
echo "Product added successfully";
}
else{
echo "" . mysqli_error($link);
}
}
mysqli_close($link); ?>
What I have tried:
I have tried following numerous you tube videos and tutorials to get this to work however without success.