Php 图像会上载到文件夹,但不会上载到SQL数据库
我是这里的初学者,我想上传几个图像到SQL数据库和一个文件夹,我目前正在本地主机上测试,它正常上传到我的文件夹,但不上传到SQL数据库,有什么建议吗 HTMLPhp 图像会上载到文件夹,但不会上载到SQL数据库,php,mysql,Php,Mysql,我是这里的初学者,我想上传几个图像到SQL数据库和一个文件夹,我目前正在本地主机上测试,它正常上传到我的文件夹,但不上传到SQL数据库,有什么建议吗 HTML <!DOCTYPE html> <?php $conn = mysql_connect("localhost","root","")or die("could not connect"); mysql_selectdb("decorydata", $conn); ?> <html lang="en">
<!DOCTYPE html>
<?php
$conn = mysql_connect("localhost","root","")or die("could not connect");
mysql_selectdb("decorydata", $conn);
?>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Import data from Decory DB</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="title" content="Hemant Vishwakarma">
<meta name="description" content="Import Excel File To MySql Database Using php">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body>
<br><br>
<div class="container">
<div class="row">
<div class="col-md-12 text-center"><h1> All Decory images </h1> </div>
<br>
<div class="col-md-3 hidden-phone"></div>
<div class="col-md-6" id="form-login">
<form class="well" action="import-img.php" method="post" enctype="multipart/form-data">
<fieldset>
<legend>Import image file</legend>
<div class="control-group">
<div class="control-label">
<label>image File:</label>
</div>
<div class="controls form-group">
<input type="file" name="files[]" id="file" class="input-large form-control" multiple>
</div>
</div>
<div class="control-group">
<div class="controls">
<button type="submit" id="submit" name="submit" class="btn btn-success btn-flat btn-lg pull-right button-loading" data-loading-text="Loading...">Upload images</button>
</div>
</div>
</fieldset>
</form>
</div>
<div class="col-md-3 hidden-phone"></div>
</div>
<table class="table table-bordered">
<thead>
<tr>
<th>ID</th>
<th>name</th>
<th>type</th>
</tr>
</thead>
<?php
$SQLSELECT = "SELECT * FROM images";
$result_set = mysql_query($SQLSELECT, $conn);
while($row = mysql_fetch_array($result_set))
{
?>
<tr>
<td><?php echo $row['ID']; ?></td>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['type']; ?></td>
</tr>
<?php
}
?>
</table>
</div>
</body>
</html>
从Decory数据库导入数据
所有诱饵图像
导入图像文件
图像文件:
上传图像
身份证件
名称
类型
PHP:
<?php
$conn = mysql_connect("localhost", "root", "") or die("could not connect");
mysql_selectdb("decorydata", $conn);
$rd = rand();
if (isset($_FILES['files'])) {
$errors = array();
foreach ($_FILES['files']['tmp_name'] as $key => $tmp_name) {
$file_name = $key . $rd . $_FILES['files']['name'][$key];
$file_size = $_FILES['files']['size'][$key];
$file_tmp = $_FILES['files']['tmp_name'][$key];
$file_type = $_FILES['files']['type'][$key];
if ($file_size > 2097152) {
$errors[] = 'File size must be less than 2 MB';
}
$query = "INSERT INTO images (name,type) VALUES($file_name','$file_type',); ";
$desired_dir = "uploadphotos";
if (empty($errors) == true) {
if (is_dir($desired_dir) == false) {
mkdir("$desired_dir", 0700); // Create directory if it does not exist
}
if (is_dir("$desired_dir/" . $file_name) == false) {
$src = imagecreatefromjpeg($tmp_name);
list($width, $height) = getimagesize($tmp_name);
$newwidth = ($width / $height) * 150;
$newheight = 150;
$tmp = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
$rd = rand();
$filename = "thumbphotos/" . $file_name;
imagejpeg($tmp, $filename, 100);
imagedestroy($src);
move_uploaded_file($file_tmp, "$desired_dir/" . $file_name);
} else { // rename the file if another one exist
$new_dir = "$desired_dir/" . $file_name . time();
rename($file_tmp, $new_dir);
}
mysql_query($query);
} else {
print_r($errors);
}
}
if (empty($error)) {
echo " <div class='alert alert-success'>Your Photos Is Successfully Uploded.<a href='imagesupload.php'> Add new Photos</a></div>";
}
}
?>
它在$query
字符串中的输入错误。一个,
和代码>太多
请使用:
mysql_query($query) or die('Error: '.mysql_error());
这将向您显示错误
请查看Mysqli而不是mysql。
您想将图像上传到数据库中有很大的原因吗?它效率不高,而且在处理过程中要付出代价。您必须解构图像才能将其插入数据库。然后,当您需要将图像拉出以显示时,您必须重建图像。此外,即使表中只有几行,数据库中的图像也会使数据库的大小急剧增长。
最佳做法是将图像文件上载到您已在使用的文件夹中。然后只需获取图像文件的名称及其扩展名,并将其存储在数据库中。当您需要显示图像时,只需使用存储图像的静态路径,并从数据库中动态提取所需图像的名称。这是一种键入值($file_name',
和,)代码>警告:如果您只是在学习PHP,请不要使用该界面。它是如此可怕和危险,以至于在PHP7中被删除。替换类和指南类说明了最佳实践。你的用户数据没有,也有,并且可以被利用。嗯,thx alot:),很抱歉,由于工作时间太长,我很累,我把这个typothx给你提了个建议,但是如果你仔细看php文件,这正是我正在做的,我正在将图像上传到“uploadedphotos”文件夹中,只是将它们的名称和类型上传到数据库中,因为一旦我需要显示它们,我只需从文件夹中按它们的名称调用它们,我就完成了:)