1

im develop website using xampp localhost. I want to add image using phpmyadmin but i can't add it..the image can display in phpmyadmin but can't appear on my website..is it any wrong with my coding in php?

$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password=""; // Mysql password 
$db_name="MMDB"; // Database name 
$tbl_name="artist"; // Table name 

// Create connection
$conn = mysqli_connect($host, $username, $password, $db_name);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
$Name=$_POST['Name']; 
$Hometown=$_POST['Hometown']; 
$Income=$_POST['Income'];
$Image=$_POST['Image'];
//file   =$_FILES['image']['tmp_name'];
$Video=$_POST['Video']; 
$sql = "INSERT INTO $tbl_name(Name, Hometown, Income, Image, Video)
VALUES ('$Name', '$Hometown','$Income','$Image','$Video')";


if (mysqli_query($conn, $sql)) {

    include 'admin.php';
} else {
    echo "x";
}


mysqli_close($conn);
?>

whats the code for add image in folder?

// where should i insert this code "> $image = $_FILES["image"]["name"]; $uploadedfile = $_FILES['image']['tmp_name']; if ($image) { $filename = stripslashes($_FILES['image']['name']);

         /* Ensure the file is JPG/PNG */
         if (($_FILES['image']['type'] != "image/jpg") && ($_FILES['image']['type'] != "image/jpeg") && ($_FILES['image']['type'] != "image/png")) { $error_status = 2; $error_message = "File uploaded was not a JPG or PNG file."; }  
         else {
           /* Ensure the file is less than 10MB */
           $size = filesize($_FILES['image']['tmp_name']);
           if ($size > (MAX_SIZE * 1024)) { $error_status = 3; $error_message = "File uploaded needs to be less than 10MB."; }
           else {
             $extension = "jpg";
             if(($_FILES['image']['type'] == "image/jpg") || ($_FILES['image']['type'] == "image/jpeg")) { $uploadedfile = $_FILES['image']['tmp_name']; $src = imagecreatefromjpeg($uploadedfile); }
             else if (($_FILES['image']['type'] == "image/png")) { $extension = "png"; $uploadedfile = $_FILES['image']['tmp_name']; $src = imagecreatefrompng($uploadedfile); }
             $img_mime_type = $_FILES['image']['type'];
Taryn
  • 242,637
  • 56
  • 362
  • 405
Thanuja
  • 31
  • 1
  • 1
  • 3

4 Answers4

1

You shouldn't save an image in your table.

You should create a folder to save uploaded pictures. In your upload process you copy the picture in the folder (use a hash to get a unique file name) then store the file name in the table.

When you want to use the picture, simply open the picture at filepath.

K4timini
  • 711
  • 2
  • 14
  • 34
1

As others have said, you should avoid storing images in mysql, however if you still want to do it then it's pretty simple:

<?php
$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password=""; // Mysql password 
$db_name="MMDB"; // Database name 
$tbl_name="artist"; // Table name 

// Create connection
$conn = mysqli_connect($host, $username, $password, $db_name);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
//BTW: always escape your data
$Name=mysqli_real_escape_string($conn,$_POST['Name']); 
$Hometown=mysqli_real_escape_string($conn,$_POST['Hometown']);
$Income=mysqli_real_escape_string($conn,$_POST['Income']);
$Image=mysqli_real_escape_string($conn,file_get_contents($_FILES['image']['tmp_name']));
$Video=mysqli_real_escape_string($conn,$_POST['Video']); 
$sql = "INSERT INTO $tbl_name (Name, Hometown, Income, Image, Video)
VALUES ('$Name', '$Hometown','$Income','$Image','$Video')";


if (mysqli_query($conn, $sql)) {

    include 'admin.php';
} else {
    echo "x";
}


mysqli_close($conn);
?>

Depending on the size of the data, your script may take a while to execute, not to mention the warnings others have posted.

To save to a folder

I believe the code you posted is probably to convert an image to a different format. this allows you to store to a folder:

$savePath = "/your/path"
if($_FILES['image']['error'] != UPLOAD_ERR_OK) {
       $error_status = 1;
       $error_message = "There was an error uploading the file";
}
/* Ensure the file is JPG/PNG */
elseif (($_FILES['image']['type'] != "image/jpg") && ($_FILES['image']['type'] != "image/jpeg") && ($_FILES['image']['type'] != "image/png")) { 
    $error_status = 2; 
    $error_message = "File uploaded was not a JPG or PNG file.";
}  
else {
   /* Ensure the file is less than 10MB */
   $size = filesize($_FILES['image']['tmp_name']);
   if ($size > (MAX_SIZE * 1024)) {
       $error_status = 3;
       $error_message = "File uploaded needs to be less than 10MB.";
   }
   else {
      //Save the image to your specified directory
      $error_status = 0;
      $error_message = '';
      move_uploaded_file($_FILES['image']['tmp_name'], $savePath.DIRECTORY_SEPARATOR.$_FILES['image']['name']);
   }
}
//now the file should be uploaded, if error status is 0, then you can safely do this:
$Image = $savePath.DIRECTORY_SEPARATOR.$_FILES['image']['name'];
//and put that into the database "as text"
Valerie
  • 332
  • 2
  • 9
  • +1 for offering a solution to the question rather than just chastising the OP. While I agree that images shouldn't be stored in the database, if I desired/had to do so and came to SO to find out how, I'd want an answer – e_i_pi May 01 '17 at 03:55
0

you could save the path of the image in the database.

you could copy the image to the folder

while viewing the image. you could call the path of the image

candy Dev
  • 9
  • 1
0

Please don't save the image in the database. I know you can use blob but it will put very heavy stress on your database. Like the 2 answers above mine, it is better to save the image into a folder, save the path as TEXT into the database. Then call the image using

Moses Liao GZ
  • 1,556
  • 5
  • 20
  • 45