我正在尝试创建一个AdLister网站 - 到目前为止,用户在数据库中发布的所有内容都会在网站上正常显示,但图片除外。以下是我的代码 - 如果我遗漏了任何内容,请告诉我:
$insert_table = "INSERT INTO posts (userid, post_date, title, price, description, email, location, image) VALUES (:userid, :post_date, :title, :price, :description, :email, :location, :image)";
$stmt = $dbc->prepare($insert_table);
$stmt->bindValue(':userid', 1, PDO::PARAM_STR);
$stmt->bindValue(':post_date', $date, PDO::PARAM_STR);
$stmt->bindValue(':title', $title, PDO::PARAM_STR);
$stmt->bindValue(':price', $price, PDO::PARAM_STR);
$stmt->bindValue(':description', $description, PDO::PARAM_STR);
$stmt->bindValue(':email', $email, PDO::PARAM_STR);
$stmt->bindValue(':location', $location, PDO::PARAM_STR);
$stmt->bindValue(':image', $image, PDO::PARAM_STR);
$stmt->execute();
return $errors;
}
if (!empty($_POST)) {
if (checkValues()) {
$errors = insertPost($dbc);
} else {
$message = "Invalid format. Please try again.";
$javascript = "<script type='text/javascript'>alert('$message');</script>";
echo $javascript;
}
}
if(Input::has('title')){
if($_FILES) {
$uploads_directory = '/img';
$filename = $uploads_directory . basename($_FILES['image']['name']);
if (move_uploaded_file($_FILES['image']['tmp_name'], $filename)) {
// echo '<p>The file '. basename( $_FILES['image']['name']). ' has been uploaded.</p>';
} else {
//alert("Sorry, there was an error uploading your file.");
}
}
}
<table class="table table-hover table-bordered table-striped">
<tr class='table-hover'>
<th class="header">Photo</th>
<th class="header col-md-1">Date Posted</th>
<th class="header">Title</th>
<th class="header col-md-1">Price</th>
<th class="header col-md-6">Description</th>
<th class="header col-md-6">Image</th>
</tr>
<?php
foreach ($posts as $post):?>
<tr class='table table-hover table-bordered body'>
<td>Photo</td>
<td><?= $post['post_date'] ?></td>
<td><?= $post['title']?></td>
<td><?= $post['price']?></td>
<td><?= $post['description']?></td>
<td><?= $post['image']?></td>
<?php endforeach ?>
</tr>
</table>
答案 0 :(得分:1)
我猜你试图将图像作为发布的字符串值插入(与其他输入一样),除了从文件输入接收的数据存储为名为$ _FILES的数组
因此...
$_POST['image']
将是空的但是
$_FILES['image']['name']
将包含实际文件名。
重新排列代码以上传文件并获取名称,然后使用该值插入。
哦,别忘了包括:
enctype="multipart/form-data"
在你的html表单标签上!