将图片+图片信息从php表单上传到mysql数据库 [英] uploading pictures +picture information from php form to mysql database
问题描述
我具有用于将图片上传到我的网站并将信息保存到mysql数据库的表单的以下代码:
I have this code for a form that uploads pictures to my website and saves the information to a mysql database:
<form method='post'>
Album Name: <input type="text" name="title" />
<input type="submit" name="submit" value="create" />
</form>
<h4>Add Photo</h4>
<form enctype="multipart/form-data" method="post">
<?php
require_once 'config.php';
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if(isset($_POST['upload'])){
$caption = $_POST['caption'];
$albumID = $_POST['album'];
$file = $_FILES ['file']['name'];
$file_type = $_FILES ['file']['type'];
$file_size = $_FILES ['file']['size'];
$file_tmp = $_FILES ['file']['tmp_name'];
$random_name = rand();
if(empty($file)){
echo "Please enter a file <br>";
} else{
move_uploaded_file($file_tmp, 'uploads/'.$random_name.'.jpg');
$ret = mysqli_prepare($mysqli, "INSERT INTO photos (caption, image_url, date_taken)
VALUES(?, ?, NOW())");
$filename = "uploads/" + $random_name + ".jpeg";
mysqli_stmt_bind_param($ret, "ss", $caption, $filename);
mysqli_stmt_execute($ret);
echo "Photo successfully uploaded!<br>";
}
}
?>
Caption: <br>
<input type="text" name="caption">
<br><br>
Select Album: <br>
<select name="album">
<?php
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
$result = $mysqli->query("SELECT * FROM albums");
while ($row = $result->fetch_assoc()) {
$albumID = $row['albumID'];
$title = $row['title'];
echo "<option value='$albumID'>$title</option>";
}
?>
</select>
<br><br>
Select Photo: <br>
<input type="file" name="file">
<br><br>
<input type="submit" name="upload" value="Upload">
</form>
这成功将图片上传到我的"uploads"文件夹以及mysql数据库中,但是,我想输入图片URL"uploads/(生成的随机名称).jpg" 我无法使用当前代码执行此操作,照片表的"image_url"列中记录的信息只是生成的随机数.开头没有"uploads/",结尾没有".jpg".
This successfully uploads the picture to my 'uploads' folder as well as my mysql database, however, I would like to put in image URL "uploads/(random name generated).jpg" I have failed to do this with my current code, the information recorded in the 'image_url' column of my photos table is just the random number generated. without the "uploads/" in the beginning and ".jpg" in the end.
我应该提到我的photos表的架构是: 标题,image_url,date_taken,imageID
I should mention that the schema for my photos table is: caption, image_url, date_taken, imageID
任何帮助将不胜感激!! 预先谢谢你
Any help will be very much appreciated!! thank you in advance
推荐答案
在此行中,您正在使用+
(加号)进行连接:
You are using +
(plus) signs to concatenate with, in this line:
$filename = "uploads/" + $random_name + ".jpeg";
PHP使用点/句号进行连接,而不是使用JS/C语言语法的加号:
PHP uses dots/periods to concatenate with, rather than plus signs which is JS/C language syntax:
$filename = "uploads/" . $random_name . ".jpeg";
错误检查将表明语法错误.
Error checking would have signaled the syntax error.
这篇关于将图片+图片信息从php表单上传到mysql数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!