<?php require_once("../includes/session.php");?>
<?php require_once("../includes/connection.php");?>
<?php require_once("../includes/function.php");?>
<?php confirm_logged_in();?>
<?php require_once("../includes/validation_functions.php");?>
<?php
if(isset($_POST['submit'])){
//process the form
// often there are form values in $_POST
$product_name = mysql_prep($_POST["product_name"]);
$product_amount = (int) $_POST["product_amount"];
$visible = (int) $_POST["visible"];
foreach($_FILES['files']['tmp_name'] as $key => $tmp_name ){
$file_tmp =$_FILES['files']['tmp_name'][$key];
$uploaded_dir = "images/";
$filename = $_FILES['files']['name'][$key];
$path = $uploaded_dir . $filename;
move_uploaded_file($file_tmp,"images/".$_FILES['files']['name'][$key]);
$query ="INSERT INTO products (";
$query .=" name, amount, visible";
$query .=") VALUES (";
$query .=" '{$product_name}',{$product_amount},{$visible}";
$query .=")";
$result = mysqli_query($connection,$query);
$last_id = mysqli_insert_id($connection);
$sql.= "('$last_id','$path'),";
$sql ="INSERT INTO images ( productID, file_name ) VALUES". trim($sql,',');
$results = mysqli_query($connection,$sql);
// validations
$required_fields = array("product_name", "product_amount");
validate_presences($required_fields);
$fields_with_max_lenghts = array("product_name" => 30);
validate_max_lenght($fields_with_max_lenghts);
if(!empty($errors)){
$_SESSION["errors"] = $errors;
redirect_to("new_product.php");
}
if($result && $results ){
// Sucess
$_SESSION["message"] = "Product inserted.";
redirect_to("manage_content.php");
}else{
// Failure
$_SESSION["message"] = "Product insertion Failed.";
redirect_to("new_product.php");
}
}
}else{
// THis is probably a GET request
redirect_to("new_product.php");
}
?>
<?php
if(isset($connnection)){
mysqli_close($connection);
}
?>
rjusman90 0 Junior Poster in Training
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.