Hướng dẫn insert image php mysql - chèn hình ảnh php mysql

Với mọi trang web đều phải có chức năng upload image lên đưa Database với mục đích thêm hình ảnh vào cơ sở dữ liệu MySQL. Bài viết này sẽ hướng dẫn bạn cách upload ảnh bằng PHP và MySQL với những đoạn code dễ hiểu đọc xong bạn hoàn toàn có thể làm được.upload ảnh bằng PHP và MySQL với những đoạn code dễ hiểu đọc xong bạn hoàn toàn có thể làm được.

  • Readmore : Code hiển thị nội dung bài viết trong PHP

Với bài tập này bạn sẽ biết cách lưu đường dẫn ảnh vào Database và hiển thị bức ảnh lên trên website. Nào hãy bắt đầu thôi!

Bước 1: Bạn sẽ khởi tạo 4 file như trong hình Bạn sẽ khởi tạo 4 file như trong hình

  • photo: dùng để lưu hình ảnh trên máy tính
  • connect.php; kết nối tới Database
  • style; thêm CSS để trang trí form
  • upload.php; file upload ảnh
  • xuly.php; xử lý ảnh sau khi upload

Tạo 1 cơ sở dữ liệu Database tên là ‘upload_image‘ => sau đó tạo bảng Table tên là images bằng paste đoạn MySQL vào Databaseupload_image‘ => sau đó tạo bảng Table tên là images bằng paste đoạn MySQL vào Database

CREATE TABLE IF NOT EXISTS `images` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`image` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

Bước 2: Tiếp theo bạn sẽ tạo một file upload.php Tiếp theo bạn sẽ tạo một file upload.php

 
 
 
Image Upload 
 
 
 

Bước 3: Tiếp theo sẽ tạo một file là xuly.php Tiếp theo sẽ tạo một file là xuly.php

 2097152) {
$errors[]='Kích thước file không được lớn hơn 2MB';
}
$image = $_FILES['image']['name'];
$target = "photo/".basename($image);
$sql = "INSERT INTO images (image) VALUES ('$image')";
mysqli_query($conn, $sql);
if (move_uploaded_file($_FILES['image']['tmp_name'], $target)) {
echo '';
}else{
echo '';
}
}
$result = mysqli_query($conn, "SELECT * FROM images");
?>



Image Upload



"; echo "

If you are attempting to obscure the location of an image and serve it via the PHP script, your image tag will look something like this:


and you can modify your script to open and output the file:

$image = $row['Pics'];
header("Content-Type: image/jpg");
readfile( $image ); 

answered Nov 2, 2011 at 15:09Nov 2, 2011 at 15:09

George CumminsGeorge CumminsGeorge Cummins

27.9k8 gold badges67 silver badges90 bronze badges8 gold badges67 silver badges90 bronze badges

If

 
 
 
Image Upload 
 
 
 
7 contains the path to the image, then you'll need to translate that into an image reference to direct the browser to that path. Something like this:


Note that "path" can mean something entirely different here. If it's a server-side fully qualified path, you'll want to translate it into an application-relative path usable by the browser. Something that begins with

 
 
 
Image Upload 
 
 
 
8 won't be useful to the browser.

answered Nov 2, 2011 at 15:11Nov 2, 2011 at 15:11

DavidDavidDavid

194k34 gold badges191 silver badges268 bronze badges34 gold badges191 silver badges268 bronze badges

You need to wrap it in an img tag.

 
 
 
Image Upload 
 
 
 
0

To display several images

 
 
 
Image Upload 
 
 
 
1

answered Nov 2, 2011 at 15:09Nov 2, 2011 at 15:09

George CumminsGeorge Cumminsrmorero

27.9k8 gold badges67 silver badges90 bronze badges4 silver badges10 bronze badges

5

If

 
 
 
Image Upload 
 
 
 
7 contains the path to the image, then you'll need to translate that into an image reference to direct the browser to that path. Something like this:upload ảnh bằng PHP và MySQL với những đoạn code dễ hiểu đọc xong bạn hoàn toàn có thể làm được.

  • Note that "path" can mean something entirely different here. If it's a server-side fully qualified path, you'll want to translate it into an application-relative path usable by the browser. Something that begins with
     
     
     
    Image Upload 
     
     
     
    
    8 won't be useful to the browser.

answered Nov 2, 2011 at 15:11

DavidDavid Bạn sẽ khởi tạo 4 file như trong hình

  • 194k34 gold badges191 silver badges268 bronze badges
  • You need to wrap it in an img tag.
  • To display several images
  • upload.php; file upload ảnh
  • xuly.php; xử lý ảnh sau khi upload

Tạo 1 cơ sở dữ liệu Database tên là ‘upload_image‘ => sau đó tạo bảng Table tên là images bằng paste đoạn MySQL vào Databaseupload_image‘ => sau đó tạo bảng Table tên là images bằng paste đoạn MySQL vào Database

CREATE TABLE IF NOT EXISTS `images` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`image` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

Bước 2: Tiếp theo bạn sẽ tạo một file upload.php Tiếp theo bạn sẽ tạo một file upload.php

 
 
 
Image Upload 
 
 
 

Bước 3: Tiếp theo sẽ tạo một file là xuly.php Tiếp theo sẽ tạo một file là xuly.php

 2097152) {
$errors[]='Kích thước file không được lớn hơn 2MB';
}
$image = $_FILES['image']['name'];
$target = "photo/".basename($image);
$sql = "INSERT INTO images (image) VALUES ('$image')";
mysqli_query($conn, $sql);
if (move_uploaded_file($_FILES['image']['tmp_name'], $target)) {
echo '';
}else{
echo '';
}
}
$result = mysqli_query($conn, "SELECT * FROM images");
?>



Image Upload



"; echo ""; echo "
"; } ?>

Bước 4: Tạo file connect.php để kết nối database Tạo file connect.php để kết nối database

Bước 5: Thêm file style.cssstyle.css

#content{
margin: 20px auto;
border: 1px solid #cbcbcb;
overflow: auto;
padding: 20px;
}
form{
margin: 20px auto;
}
form div{
margin-top: 5px;
}
#img_div{
padding: 5px;
border: 1px solid #cbcbcb;
float: left
}
#img_div:after{
content: "";
display: block;
clear: both;
}
#img_div img{
float: left;
margin: 5px;
width: 400px;
height: auto;
}

Sau khi tạo xong 4 file: thư mục photo trong máy tính, upload.php, xuly.php và connect.php, style.css bạn thử chạy vào xem kết quả như thế nào.

Kết luận: Trên đây là code upload hình ảnh lên Database bằng PHP và MySQL, với bài hướng dẫn với các bước đơn giản trên hi vọng sẽ giúp bạn học thêm một chút kiến thức về lập trình PHP.

Đọc thêm: Cách lấy dữ liệu trong MySQL