Загрузка нескольких изображений в один вход для одного продукта или поста php oop

Пожалуйста, помогите мне в этом проекте... загрузка нескольких изображений в один вход для одного поста или поста php oop. когда я хочу вставить изображение со значением post_id, но я не понимаю, как я работаю с этим кодом..... Пожалуйста, пожалуйста, помогите мне, пожалуйста.... и как удалить все изображения с одним удаленным сообщением, и как обновить пост и способ отображения всех изображений пост по почте означает, когда я нажимаю один пост и открываю со всеми изображениями ранее опубликованные изображения в одном посте....

database.php

<?php
Class Database{
    public $host   = DB_HOST;
    public $user   = DB_USER;
    public $pass   = DB_PASS;
    public $dbname = DB_NAME;

    public $link;
    public $error;

    public function __construct(){
        $this->connectDB();
    }

    private function connectDB(){
    $this->link = new mysqli($this->host, $this->user, $this->pass, $this->dbname);
    if(!$this->link){
        $this->error ="Connection fail".$this->link->connect_error;
        return false;
    }
 }

    // Select or Read data

public function select($query){
    $result = $this->link->query($query) or die($this->link->error.__LINE__);
    if($result->num_rows > 0){
        return $result;
    } else {
        return false;
    }
}

// Insert data
public function insert($query){
$insert_row = $this->link->query($query) or die($this->link->error.__LINE__);
    if($insert_row){
        return $insert_row;
    } else {
        return false;
    }
    }

// Update data
    public function update($query){
$update_row = $this->link->query($query) or die($this->link->error.__LINE__);
    if($update_row){
        return $update_row;
    } else {
        return false;
    }
  }

  // Delete data
   public function delete($query){
    $delete_row = $this->link->query($query) or die($this->link->error.__LINE__);
    if($delete_row){
        return $delete_row;
    } else {
        return false;
    }
  }

  // Inserted Last ID
   public function lid($query){
    $inslid = $this->link->query($query) or die($this->link->error.__LINE__);
    if($inslid){
        return $inslid;
    } else {
        return false;
    }
  }
}
?>

post_add.php

<?php 
    require_once('inc/top.php'); 
    require_once('inc/header.php');
    require_once('inc/sidebar.php');
?>

<li class="active">Add New Post</li>
    </ul><!-- /.breadcrumb -->

    <div class="nav-search" id="nav-search">
        <form class="form-search">
            <span class="input-icon">
                <input type="text" placeholder="Search ..." class="nav-search-input" id="nav-search-input" autocomplete="off" />
                <i class="ace-icon fa fa-search nav-search-icon"></i>
            </span>
        </form>
    </div><!-- /.nav-search -->
</div>

<div class="page-content">
<?php require_once('inc/athm.php'); ?>

    <div class="page-header">
        <h1>
            <a href="index.php">Dashboard</a>
            <small>
                <i class="ace-icon fa fa-angle-double-right"></i>
                Add New Post
            </small>
        </h1>
    </div><!-- /.page-header -->
    <div class="row">
    <div class="col-xs-12">
        <!-- PAGE CONTENT BEGINS -->
<?php
    if ($_SERVER['REQUEST_METHOD'] == 'POST'){
        $title = mysqli_real_escape_string($db->link, $_POST['title']);
        $cat = mysqli_real_escape_string($db->link, $_POST['cat']);
        $body = mysqli_real_escape_string($db->link, $_POST['body']);
        $tags = mysqli_real_escape_string($db->link, $_POST['tags']);
        $author = mysqli_real_escape_string($db->link, $_POST['author']);
        $userid = mysqli_real_escape_string($db->link, $_POST['userid']);

    if ($title == "" || $cat == "" || $body == "" || $tags == "" || $author == "") {
            echo "<span class='error'> Field must not be empty ! </span>";
        }else{
            $query = "INSERT INTO tbl_post(cat, title, body, author, tags, userid) VALUES('$cat','$title','$body','$author','$tags','$userid')";
            $post_ins_id = $db->lid($query);

            $inserted_rows = $db->insert($query);           
                if ($inserted_rows) {
                         echo "<span class='success'>Data Inserted Successfully. </span>";
                    }else {
                         echo "<span class='error'>Data Not Inserted !</span>";
                    }
                }




        if (isset($_FILES['image']['name'])) {

            for($i=0; $i< count($_FILES["image"]["name"]); $i++){

                $permited  = array('jpg', 'jpeg', 'png', 'gif');

                $file_name = $_FILES['image']['name'][$i];
                $file_size = $_FILES['image']['size'][$i];
                $file_temp = $_FILES['image']['tmp_name'][$i];

                $div = explode('.', $file_name);
                $file_ext = strtolower(end($div));
                $unique_image = substr(md5(time()), 0, 10).'.'.$file_ext;
                $uploaded_image = "../images/".$unique_image;

                if ($file_size >1048567) {
                     echo "<span class='error'>Image Size should be less then 1MB! </span>";
                } elseif (in_array($file_ext, $permited) === false) {
                     echo "<span class='error'>You can upload only:-" .implode(', ', $permited)."</span>";
                } else{
                move_uploaded_file($file_temp, $uploaded_image);
                $query = "INSERT INTO tbl_images(post_id, cat, image) VALUES('$post_ins_id, $cat','$uploaded_image')";
                $inserted_rows = $db->insert($query);
                if ($inserted_rows) {
                         echo "<span class='success'>Data Inserted Successfully. </span>";
                    }else {
                         echo "<span class='error'>Data Not Inserted !</span>";
                    }
                }
            }
        }
    }
?>
<form action="" method="post" class="form-horizontal" role="form" enctype="multipart/form-data">
    <div class="form-group">
        <label class="col-sm-3 control-label no-padding-right" for="form-field-1-1"> Post Title </label>
        <div class="col-sm-9">
            <input type="text" name="title" id="form-field-1-1" placeholder="Please Enter Your Post Title" class="form-control" />
        </div>
    </div>
    <div class="form-group">
        <label class="col-sm-3 control-label no-padding-right" for="form-field-1-1"> Post Category </label>
        <div class="col-sm-9">
            <select id="select" name="cat" class="form-control">
                <option>Select Category</option>
                <?php
                    $query = "select * from tbl_category";
                    $category = $db->select($query);
                    if ($category) {
                        while ($result = $category->fetch_assoc()) {
                ?>
                <option value="<?php echo $result['id']; ?>"><?php echo $result['name']; ?></option>
                <?php }  } ?>
            </select>
        </div>
    </div>
    <div class="form-group">
        <label class="col-sm-3 control-label no-padding-right" for="form-field-1-1"> Upload Image </label>
        <div class="col-sm-9">
            <input type="file" name="image[]" class="form-control" multiple="" />
        </div>
    </div>

    <div class="form-group">
        <label class="col-sm-3 control-label no-padding-right" for="form-field-1-1"> Post Content </label>
        <div class="col-sm-9">
            <textarea name="body" id="body" cols="30" rows="10" class="form-control"></textarea>
        </div>
    </div>
    <div class="form-group">
        <label class="col-sm-3 control-label no-padding-right" for="form-field-1-1"> Post Tags </label>
        <div class="col-sm-9">
            <input type="text" name="tags" id="form-field-1-1" placeholder="Please Enter Your Post Tags" class="form-control" />
        </div>
    </div>
    <div class="form-group">
        <label class="col-sm-3 control-label no-padding-right" for="form-field-1-1"> Post Author </label>
        <div class="col-sm-9">
            <input type="text" name="author" id="form-field-1-1" value="<?php echo Session::get('username'); ?>" class="form-control" readonly=""/>
            <input type="hidden" name="userid" id="form-field-1-1" value="<?php echo Session::get('userId'); ?>" class="form-control" />
        </div>
    </div>
    <div class="clearfix"></div>
    <div class="clearfix form-actions">
        <div class="col-md-offset-3 col-md-9">
            <button class="btn btn-info" type="submit" name="submit"><i class="ace-icon fa fa-check bigger-110"></i>Add New Post</button>
            &nbsp; &nbsp; &nbsp;
            <button class="btn" type="reset"><i class="ace-icon fa fa-undo bigger-110"></i>Reset</button>
        </div>
    </div>
</form>
<?php require_once('inc/footer.php'); ?>

0 ответов

Другие вопросы по тегам