0
<?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);
}
?>
3
Contributors
3
Replies
28
Views
3 Years
Discussion Span
Last Post by iamthwee
2

Just dumping code isn't going to work. Please explain what should but isn't happening.

Edited by pritaeas

-1

i want to insert multiple images

<?php require_once("../includes/session.php");?>
<?php require_once("../includes/connection.php");?>
<?php require_once("../includes/function.php");?>
<?php confirm_logged_in();?>
<?php $layout_context ="admin";?>
<?php
include("../includes/layouts/header.php");
?>
<?php
find_selected_page();
?>
<div id="main">
  <div id="navigation">
   <?php
  echo navigation($current_subject,$current_page);
   ?>
  </div>
  <div id="page">
  <?php
  echo message();
  ?>
  <?php
  $errors = errors();
  ?>
  <?php
  echo form_errors($errors);
  ?>
  <h2>New  Product</h2>

  <form action="create_product.php" method="post" enctype="multipart/form-data">
  <p>
  <input type="text"   name="product_name"  value="" placeholder="Product Name:"  required="required" >
  </p>
   <p>
  <input type="text" name="product_amount" value="" placeholder="Product Amount:" required="required"  />
   <p>
   Select A File:
  <input type="file" name="files[]" multiple required="required"/>
  </p>
  <p>Visible:
  <input type="radio" name="visible" value="0" required="required"/>No
  <input type="radio" name="visible" value="1" required="required"/>Yes
   </p>
    <input type="submit" name="submit"  value="Insert Product"/>
    </form><br>
<a href="manage_content.php">Cancel</a>    
  </div>
</div>
<?php
include("../includes/layouts/footer.php");
?>
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.