1

Hi I am trying the data like title,Description and image.If i give only title and description without adding image the data should be inserted into database.But if I am trying that getting error.Here is my error and code:

error: error while uploading

my code

$title=$_POST['blog_title'];
$result = str_replace(" ", "-", $title);
$description=$_POST['blog_description'];
$name=$_FILES["image"]["name"];
$type=$_FILES["image"]["type"];
$size=$_FILES["image"]["size"];
$temp=$_FILES["image"]["tmp_name"];
$error=$_FILES["image"]["error"];
if($error>0)
die("error while uploading");
else
{
if($type == "image/png" || $type == "image/jpg"|| $type == "image/jpeg" || $type == "image/svg" || $type == "image/jpe" )
{
move_uploaded_file($temp,"upload/".$name);
$sql=mysql_query("INSERT INTO blogs(image,blog_title,blog_description)values('$name','$result','$description')");
echo "upload complete";
session_start();
header("Location:blogimage.php");   
}
else
{
echo "failure";
}

Html Code

<form method="POST" action="blogs.php" enctype="multipart/form-data">
<div>
<label for="title">Title</label>
<input type="text" name="blog_title" value="">
</div>
<div>
<label for="image">IMAGE</label>
<input type="file" name="image">
</div>
<div>
<label for="blog_description">Description</label>
<textarea name="blog_description" class="text"  style="width:50%;">  </textarea>
</div>
<input type="submit" value="Submit"/>
</form>
Hardik Solanki
  • 3,153
  • 1
  • 17
  • 28
Nagu
  • 149
  • 1
  • 3
  • 10

5 Answers5

3

According to your code if you are not uploading the image, value of $error becomes 4. So your if() condition is getting executed. So remove your if condition.

if ($name = $_FILES["image"]["name"] != '') {
    if ($type == "image/png" || $type == "image/jpg" || $type == "image/jpeg" || $type == "image/svg" || $type == "image/jpe") {
        move_uploaded_file($temp, "upload/" . $name);
        $sql = mysql_query("INSERT INTO blogs(image,blog_title,blog_description)values('$name','$result','$description')");
        echo "upload complete";            
    }else{
        echo "File type not supported.";
    }
 session_start();
 header("Location:blogimage.php");
} else {
    $sql = mysql_query("INSERT INTO blogs(blog_title,blog_description)values('$result','$description')");
    echo "upload complete";
    session_start();
    header("Location:blogimage.php");
}
AAT
  • 411
  • 7
  • 16
0

You have to use like below:

...
if($type == "image/png" || $type == "image/jpg"|| $type == "image/jpeg" || $type == "image/svg" || $type == "image/jpe" )
{
    move_uploaded_file($temp,"upload/".$name);
    $sql=mysql_query("INSERT INTO blogs(image,blog_title,blog_description)values('$name','$result','$description')");
} else {
    $sql=mysql_query("INSERT INTO blogs(blog_title,blog_description)values('$result','$description')");
}
session_start();
header("Location:blogimage.php");
...
Kausha Mehta
  • 2,828
  • 2
  • 20
  • 31
0

I am using mysqli_query with your code, because mysql_* is deprecated:

Modified Code:

<?php

$link = mysqli_connect("localhost", "root", "", "yourDb");
if (!$link) {
    echo "Error: Unable to connect to MySQL." . PHP_EOL;
    echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
    echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
    exit;
}

$title=$_POST['blog_title'];
$result = str_replace(" ", "-", $title);
$description=$_POST['blog_description'];

$name = "";
$failure = "";

if(isset($_FILES["image"]["name"])){
    $name=$_FILES["image"]["name"];
    $type=$_FILES["image"]["type"];
    $size=$_FILES["image"]["size"];
    $temp=$_FILES["image"]["tmp_name"];
    $error=$_FILES["image"]["error"];
    if($error>0){
        $name = "";
    }
    else{
        if($type == "image/png" || $type == "image/jpg"|| $type == "image/jpeg" || $type == "image/svg" || $type == "image/jpe" )
        {
            move_uploaded_file($temp,"upload/".$name);        
        }
    }
}

$sql = mysqli_query($link,"INSERT INTO blogs (image,blog_title,blog_description)
               values('$name','$result','$description')");

if($sql){
    //echo "upload complete";
    session_start();
    header("Location:blogimage.php");       
    die();
}
else{
    echo 'failure';
}

?>

Explanation:

  • I am checking if if $_FILES["image"]["name"] is set than execute the file upload code.
  • further more if $error is not equal to 0 use move_uploaded_file()
  • Query will run in default either file empty or not, if empty than use $name as empty else use file name.

From PHP Manual:

mysqli::query -- mysqli_query — Performs a query on the database

Note that, its a procedural structure of mysqli_* extension, ist param of mysqli_query should be your connection identifier and second param should be your MYSQL Statement.

devpro
  • 16,184
  • 3
  • 27
  • 38
  • it is working for the image but if i give any pdf files or zip files it is accepting it should not accept any pdf or zip files erc...it should accept only images – Nagu Feb 08 '16 at 06:38
  • @user5836176: accepting the other file type is related to $type: in pdf debug what value are u getting – devpro Feb 08 '16 at 06:41
0

You have to make your fields and values dynamic :

Try this :

$_POST = array('image'=>'','blog_title'=>'yes','blog_description'=>'nothing');
foreach ($_POST as $key => $value) {
    if(!empty($value)){
        $fields .= $key.',';
        $values .= "'".$value."'".',';
    }
}
$fields = substr($fields, 0, -1);
$values = substr($values, 0, -1);
echo "INSERT INTO blogs($fields)values($values)";
Monty
  • 1,110
  • 7
  • 15
0

First of all, start session at the very top of your PHP script, like this:

<?php
    session_start();
?>

And now comes your issue. First use is_uploaded_file() function to check whether a file is uploaded or not, and then process your form accordingly.

So your code should be like this:

$title=$_POST['blog_title'];
$result = str_replace(" ", "-", $title);
$description=$_POST['blog_description'];

if(is_uploaded_file($_FILES['image']['tmp_name'])){
    $name=$_FILES["image"]["name"];
    $type=$_FILES["image"]["type"];
    $size=$_FILES["image"]["size"];
    $temp=$_FILES["image"]["tmp_name"];
    $error=$_FILES["image"]["error"];
    $ext = strtolower(pathinfo($name, PATHINFO_EXTENSION));

    if($error > 0){
        die("error while uploading");
    }else{
        $permissible_extension = array("png", "jpg", "jpeg", "svg", "jpe");
        if(in_array($ext, $permissible_extension)){
            if(move_uploaded_file($temp,"upload/".$name)){
                $sql = mysql_query("INSERT INTO blogs(image,blog_title,blog_description)values('$name','$result','$description')");
                if($sql){
                    header("Location:blogimage.php");  
                    exit();
                }else{
                    echo "Insertion failed";
                }
            }else{
                echo "File couldn't be uploaded";
            }
        }else{
            echo "Invalid format";
        }
    }

}else{
    $sql = mysql_query("INSERT INTO blogs(blog_title,blog_description)values('$result','$description')");
    if($sql){
        header("Location:blogimage.php");  
        exit();
    }else{
        echo "Insertion failed";
    }
}

Sidenote: Don't use mysql_* functions, they are deprecated as of PHP 5.5 and are removed altogether in PHP 7.0. Use mysqli or pdo instead. And this is why you shouldn't use mysql_* functions.

Community
  • 1
  • 1
Rajdeep Paul
  • 16,887
  • 3
  • 18
  • 37