1

I have to code to insert data with image. when I insert information, the info will be saved into table except image and image name will be empty I do not know what the problem is?

The type of image is longblob and image name is varchar

addEmployee.php

<?php
session_start();
require 'common.php';
$connection = new mysqli($localhost , $dusername , $dpassword,$database);

if ($connection->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

//echo "Connected successfully";
echo "<pre>";
//$EmpID = $_POST['EmpID'];
$SSN = $_POST['SSN'];
$Fname = $_POST['Fname'];
$Lname = $_POST['Lname'];
$PhoneNumber = $_POST['PhoneNumber'];
$Salary = $_POST['Salary'];
$Email = $_POST['Email'];
$JobTitle = $_POST['JobTitle'];
$Password = $_POST['Password'];
$image= addslashes(file_get_contents($_FILES['image']['tmp_name']));
$name = addslashes($_FILES['image']['imageName']);

$query = "INSERT INTO `employee`( `SSN`, `Fname`, `Lname`, `PhoneNumber`, `Salary`, `Password`, `JobTitle`,`Email` , `image` , `name`)";
$query.=                 "VALUES('$SSN','$Fname','$Lname','$PhoneNumber','$Salary','$Password','$JobTitle' ,'$Email' ,'$image' ,'$name')";

$result = mysqli_query($connection, $query);

if (!$result)
{
    echo "1";
    die("Query failed".  mysqli_errno($connection));   
}
else {
    header( "location: admin.html" );
}
?>
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Mohammed
  • 51
  • 1
  • 8

1 Answers1

0

Even if storing images in db is a debatable practice, I woul try to show you some issues. 1. The $_FILES doesn't contain the 'imageName' key, just name. See http://php.net/manual/ro/features.file-upload.post-method.php 2. Make sure that the image field type is large enough to store the entire image content (dump a strlen of the $image). Use blob. Also, are you certain the post sends you the image? 3. Remove the echo "pre"... it scrambles the headers directive you will use to redirect.

What is the sql errorno? Also, be aware of security issues. Images can have injected in them chunks of executable codes. The safest way (in your case) is to create a clone of it (imagecreate).

Adrian
  • 85
  • 6
  • Thank you for your comments. I do not get any error number. The all information will be saved in database except the image and the image name – Mohammed Nov 16 '16 at 08:16