我在這裏是一個初學者,我想上傳幾個圖像到SQL數據庫和一個文件夾,我目前正在我的本地主機上測試,它通常上傳到我的文件夾但沒有上傳到SQL數據庫,有什麼建議?圖片上傳到文件夾,但沒有上傳到SQL數據庫
的HTML
<!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>
的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>";
}
}
?>
我缺少什麼?
這是一個錯誤'VALUES($ file_name','和'',);'<<<並檢查錯誤會告訴你。 –
**警告**:如果您只是學習PHP,請不要使用['mysql_query'](http://php.net/manual/en/function.mysql-query.php)界面。這是非常可怕和危險的,它在PHP 7中被刪除了。[PDO的替代品並不難學](http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps -pdo-for-database-access /)以及[PHP The Right Way](http://www.phptherightway.com/)等指南介紹了最佳實踐。你的用戶數據是**不是** [正確轉義](http://bobby-tables.com/php.html),並有[SQL注入漏洞](http://bobby-tables.com/),並且可以被利用。 – tadman
以及thx很多:),對不起,我很長時間工作很累,我滑倒這個錯誤 –