如何使用phpmyadmin在数据库中插入图像?

时间:2014-12-06 07:59:52

标签: php mysql database phpmyadmin

我使用xampp localhost开发网站。我想使用phpmyadmin添加图像,但我无法添加它...图像可以在phpmyadmin中显示但不能出现在我的网站上..我在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);
?>


在文件夹中添加图片的代码是什么?

//我应该在哪里插入此代码“>     $ 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'];

4 个答案:

答案 0 :(得分:1)

您不应该在表格中保存图像。

您应该创建一个文件夹来保存上传的图片。在上传过程中,您复制文件夹中的图片(使用哈希获取唯一的文件名),然后将文件名存储在表格中。

如果您想使用图片,只需在文件路径打开图片。

答案 1 :(得分:1)

正如其他人所说,你应该避免在mysql中存储图像,但是如果你仍然想要这样做那么它很简单:

<?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);
?>

根据数据的大小,您的脚本可能需要一段时间才能执行,更不用说其他人发布的警告了。

要保存到文件夹

我相信您发布的代码可能是将图片转换为其他格式。这允许您存储到文件夹:

$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"

答案 2 :(得分:0)

您可以保存数据库中的图像路径

您可以将图像复制到文件夹

查看图像时。 你可以调用图片的路径

答案 3 :(得分:0)

请勿将图像保存在数据库中。我知道你可以使用blob,但它会给你的数据库带来很大的压力。就像我上面的2个答案一样,最好将图像保存到文件夹中,将路径保存为TEXT到数据库中。然后使用

调用图像