gpt4 book ai didi

mysql - 在线相册-上传的图像未显示在mysql数据库中

转载 作者:行者123 更新时间:2023-11-29 22:46:18 24 4
gpt4 key购买 nike

我正在使用mysql和php创建在线相册。我使用phpmyadmin来管理我的数据库。到目前为止,我有以下代码:

    <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
$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');
mysqli_query($mysqli, "INSERT INTO photos (caption, image_url, date_taken, imageID)
VALUES($caption, $random_name.jpeg, NOW(), $albumID)");
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”文件夹中。但是,当我检查phpmyadmin时,“照片”表中没有添加照片。
我应该补充一点,“照片”和“相册”表的架构为:
照片(标题,image_url,date_taken,imageID,userID)
相册(标题,创建的日期,修改的日期,专辑ID)

我的代码在做什么错,这样上传的图片就不会显示在数据库中?

谢谢!

最佳答案

SQL语法错误。更好的报价。
而且不要忘了大幅削减:)

mysqli_query($mysqli, "INSERT INTO photos (caption, image_url, date_taken, imageID)
VALUES('" . addslashes($caption) . "', '" . $random_name . ".jpeg', NOW(), " . $albumID . ")");

关于mysql - 在线相册-上传的图像未显示在mysql数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29104279/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com