0

How to upload image to database and move into image folder this is my function for other rows:

<?php
function add_post($title, $contents, $category) {
    $title      = mysql_real_escape_string($title);
    $contents   = mysql_real_escape_string($contents);
    $category   = (int) $category;
    //var_dump($category);

    mysql_query("INSERT INTO `posts` SET

            `cat_id`        = '{$category}',
            `title`         = '{$title}',
            `image`         = '{$image}',
            `contents`      = '{$contents}',
            `date_posted`   = NOW()");
}

und this is post page:

<?php 
    include_once('../resources/init.php'); 


    if ( isset($_POST['title'], $_POST['contents'], $_POST['category']) ) {
            //var_dump($_POST);
        $errors = array();

        $title      = trim($_POST['title']);
        $contents   = trim($_POST['contents']);

        if ( empty($title)) {
            $errors[] = 'You need to supply a title';
        } else if ( strlen($title) > 255 ){
            $errors[] = 'The title can not be longer than 255  characters'; 
        }
        if ( empty($contents) ) {
            $errors[] = 'You need to supply some text';
        }
        if ( ! category_exists('id', $_POST['category']) ){
            $errors[] = 'That category does not exist'; 
        }

        if ( empty($errors) ) {
            add_post($title, $contents, $_POST['category']);

            $id = mysql_insert_id();

            header('location: add_post.php?id=' . $id);
            die();
        }
    }
?>
2
Contributors
3
Replies
22
Views
3 Years
Discussion Span
Last Post by gogs85
0

$uploadDir = 'backend/'; //path for where u have to upload
$filename = $_FILES['photo']['name'];
$tmpname = $_FILES['photo']['tmp_name'];

$filePath = $uploadDir . $filename;
$result1 = move_uploaded_file($tmpname, $filePath);

insert into review (id,filename, filepath) values('','$filename','$filePath')");

<input type="file" name="photo">

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.