php 如何使用 phpmyadmin 的“插入”选项卡将图像插入数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28951283/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to insert image to database using phpmyadmin 'insert' tab
提问by mar
I have a mysql database I created through phpmyadmin. Basically, I would like to know how to insert an image (or to be specific, insert an image relative path) to my database through the phpmyadmin 'insert' tab. Any suggestions on fields and data types would be great!
我有一个通过 phpmyadmin 创建的 mysql 数据库。基本上,我想知道如何通过 phpmyadmin 的“插入”选项卡将图像(或者更具体地说,插入图像的相对路径)插入到我的数据库中。关于字段和数据类型的任何建议都会很棒!
回答by Alejo_Blue
I always save the path where the file is being uploaded, Take a look at the following example to see if it fit your needs:
我总是保存文件上传的路径,看看下面的例子,看看它是否符合你的需要:
Page 1 : Here your are going to have your form to upload the images files
第 1 页 : 在这里你将有你的表格来上传图像文件
′
<html>
<head>
<title>Pagina 1 form</title>
</head>
<body>
<div>This is our upload control</div>
<form name="form" action="page2.php" method="POST" enctype="multipart/form-data">
<input type="file" name="foto">
<p>
<button>Subir archivo</button>
</form>
</body>
′
′
Page 2: This is the one which is going to receive the file and save it to the table you want
第 2 页:这是将要接收文件并将其保存到您想要的表的那个
′
<?php
$file_get = $_FILES['foto']['name'];
$temp = $_FILES['foto']['tmp_name'];
$file_to_saved = "dcuments/".$file_get; //Documents folder, should exist in your host in there you're going to save the file just uploaded
move_uploaded_file($temp, $file_to_saved);
echo $file_to_saved;
$insert_img = mysql_query("INSERT INTO my_table (field_image) values ('".$file_to_saved."')");
if ($insert_img) {
# code...
echo "Img inserted successfully";
}
else{
echo "There is something wrong with this code. Eff!";
}
?>
′
′
Hope this helps! XD
希望这可以帮助!XD
回答by Leandro Papasidero
Any suggestions on fields and data types would be great!
关于字段和数据类型的任何建议都会很棒!
If you haveto store the images into the database (no recommended), the column type depends on the image size that you are going to insert.
如果您必须将图像存储到数据库中(不推荐),列类型取决于您要插入的图像大小。
TINYBLOBA binary large object column with a maximum length of 255 (2^8 - 1) characters.
TINYBLOB二进制大对象列,最大长度为 255 (2^8 - 1) 个字符。
BLOBA binary large object column with a maximum length of 65535 (2^16 - 1) characters.
BLOB一个二进制大对象列,最大长度为 65535 (2^16 - 1) 个字符。
MEDIUMBLOBA binary large object column with a maximum length of 16777215 (2^24 - 1) characters.
MEDIUMBLOB一个二进制大对象列,最大长度为 16777215 (2^24 - 1) 个字符。
TINYBLOBA binary large object column with a maximum length of 255 (2^8 - 1) characters.
TINYBLOB二进制大对象列,最大长度为 255 (2^8 - 1) 个字符。
BLOBA binary large object column with a maximum length of 65535 (2^16 - 1) characters.
BLOB一个二进制大对象列,最大长度为 65535 (2^16 - 1) 个字符。
MEDIUMBLOBA binary large object column with a maximum length of 16777215 (2^24 - 1) characters.
MEDIUMBLOB一个二进制大对象列,最大长度为 16777215 (2^24 - 1) 个字符。
LONGBLOBA binary large object column with a maximum length of 4294967295 (2^32 - 1) characters.
LONGBLOB一个二进制大对象列,最大长度为 4294967295 (2^32 - 1) 个字符。
Create DDL Statement:
创建 DDL 语句:
CREATE TABLE table_name (
Image LONGBLOB,
);
If you don't haveto store the image into the database, alejo-blueanswer will work for you
如果您不必将图像存储到数据库中,alejo-blue答案将为您工作