-1
我目前正在使用我的项目,而且我很难从数据库中显示图片。当我检查图像的名字Store上的phpmyadmin,但它不是我的table.Here显示它是我的代码php-how如何在数据库中插入图像并使用php显示
存储图像数据库
在桌子上 显示图像
我目前正在使用我的项目,而且我很难从数据库中显示图片。当我检查图像的名字Store上的phpmyadmin,但它不是我的table.Here显示它是我的代码php-how如何在数据库中插入图像并使用php显示
存储图像数据库
在桌子上 显示图像
你可以试试看。 文件Image.php
<?php
// verify request id.
if (empty($_GET['id']) || !is_numeric($_GET['id'])) {
echo 'A valid image file id is required to display the image file.';
exit;
}
$imageId = $_GET['id'];
//connect to mysql database
if ($conn = mysqli_connect('localhost', 'root', 'root', 'test')) {
$content = mysqli_real_escape_string($conn, $content);
$sql = "SELECT type, content FROM images where id = {$imageId}";
if ($rs = mysqli_query($conn, $sql)) {
$imageData = mysqli_fetch_array($rs, MYSQLI_ASSOC);
mysqli_free_result($rs);
} else {
echo "Error: Could not get data from mysql database. Please try again.";
}
//close mysqli connection
mysqli_close($conn);
} else {
echo "Error: Could not connect to mysql database. Please try again.";
}
if (!empty($imageData)) {
// show the image.
header("Content-type: {$imageData['type']}");
echo $imageData['content'];
}
?>
文件:upload.php的
<?php
/**
* Upload an image to mysql database.
*/
// Check for post data.
if ($_POST && !empty($_FILES)) {
$formOk = true;
//Assign Variables
$path = $_FILES['image']['tmp_name'];
$name = $_FILES['image']['name'];
$size = $_FILES['image']['size'];
$type = $_FILES['image']['type'];
if ($_FILES['image']['error'] || !is_uploaded_file($path)) {
$formOk = false;
echo "Error: Error in uploading file. Please try again.";
}
//check file extension
if ($formOk && !in_array($type, array('image/png', 'image/x-png', 'image/jpeg', 'image/pjpeg', 'image/gif'))) {
$formOk = false;
echo "Error: Unsupported file extension. Supported extensions are JPG/PNG.";
}
// check for file size.
if ($formOk && filesize($path) > 500000) {
$formOk = false;
echo "Error: File size must be less than 500 KB.";
}
if ($formOk) {
// read file contents
$content = file_get_contents($path);
//connect to mysql database
if ($conn = mysqli_connect('localhost', 'root', 'root', 'test')) {
$content = mysqli_real_escape_string($conn, $content);
$sql = "insert into images (name, size, type, content) values ('{$name}', '{$size}', '{$type}', '{$content}')";
if (mysqli_query($conn, $sql)) {
$uploadOk = true;
$imageId = mysqli_insert_id($conn);
} else {
echo "Error: Could not save the data to mysql database. Please try again.";
}
mysqli_close($conn);
} else {
echo "Error: Could not connect to mysql database. Please try again.";
}
}
}
?>
<html>
<head>
<title>Upload image to mysql database.</title>
<style type="text/css">
img{
margin: .2em;
border: 1px solid #555;
padding: .2em;
vertical-align: top;
}
</style>
</head>
<body>
<?php if (!empty($uploadOk)): ?>
<div>
<h3>Image Uploaded:</h3>
</div>
<div>
<img src="image.php?id=<?=$imageId ?>" width="150px">
<strong>Embed</strong>: <input size="25" value='<img src="image.php?id=<?=$imageId ?>">'>
</div>
<hr>
<? endif; ?>
<form action="<?=$_SERVER['PHP_SELF']?>" method="post" enctype="multipart/form-data" >
<div>
<h3>Image Upload:</h3>
</div>
<div>
<label>Image</label>
<input type="hidden" name="MAX_FILE_SIZE" value="500000">
<input type="file" name="image" />
<input name="submit" type="submit" value="Upload">
</div>
</form>
</body>
</html>
更换线
mysqli_connect( '本地主机', '根', '根', '测试')
与
mysqli_connect('your host','your username', '你的密码','你的数据库名')
在这两个文件中再次保存。
你完成了。
不错的SQL注入孔 –
请将您的代码作为文本发布。 – MrDarkLynx
不要使用'mysql_ *'函数,它们在当前版本的PHP中被弃用和删除。而应使用'mysqli_ *'或PDO和_prepared语句_。 – MrDarkLynx
'从图像=“$ image”的车辆中选择图像'您的查询是错误的。你应该使用'select * from vehicle where image =“$ image”'或'select imagename,imagecontent from vehicle where image =“$ image”' – ruhul