我为自己的网站创建了一个CMS,在我的管理页面中,我有一个添加页面,可以将新内容添加到我的网站LOCATED HERE,并添加了一些表单字段。
其中2个是:
图片网址(文字框)&上传图片(选择文件按钮)
当我填写所有文件并使用IMAGE URL选择图像并点击添加文章时,它工作正常,我的表单保存到我的数据库,然后显示在我的网站上。
当我填写所有文件并使用UPLOAD IMAGE选择图像并点击添加文章时,它会将图像添加到我的cpanel中的所选文件夹中,但不会添加到数据库。
我的问题是:如何才能将其添加到数据库中?并将新图像位置保存到数据库中的图像字段?
在将上传文件按钮添加到我的页面时,我已按照本教程进行操作。
请不要向我展示如何执行此操作的链接,因为我已经阅读了这些内容,但是当我将其添加到我的代码中时,我仍然喋喋不休。
我的add.php代码就是这个。
<?php
session_start();
include_once('../include/connection.php');
if (isset($_SESSION['logged_in'])){
if (isset($_POST['title'], $_POST['content'])) {
$title = $_POST['title'];
$content = nl2br($_POST['content']);
if (!empty($_POST['image']))
{
$image = $_POST['image'];
}
else
{
$image = $_POST['imageupload'];
if (isset($_FILES['imageupload']))
{
$errors = array();
$allowed_ext = array('jpg', 'jpeg', 'png', 'gif');
$file_name = $_FILES['imageupload'] ['name'];
$file_ext = strtolower (end (explode ('.', $file_name)));
$file_size = $_FILES['imageupload'] ['size'];
$file_tmp = $_FILES['imageupload'] ['tmp_name'];
if (in_array ($file_ext, $allowed_ext) === false) {
$errors[] = 'File extension not allowed';
}
if ($file_size > 2097152) {
$errors[] = 'File size must be under 2mb';
}
if (empty($errors)) {
if (move_uploaded_file($file_tmp, 'images/'.$file_name)) {
echo 'File uploaded';
$image = $file_name;
}
}else{
foreach ($errors as $error)
echo $error, '<br />';
}
}
}
$link = $_POST['link'];
$category = $_POST['category'];
$brand = $_POST['brand'];
if (empty($title) or empty($content)) {
$error = 'All Fields Are Required!';
}else{
$query = $pdo->prepare('INSERT INTO mobi (promo_title, promo_content, promo_image, promo_link, promo_cat, promo_name) VALUES(?, ?, ?, ?, ?, ?)');
$query->bindValue(1, $title);
$query->bindValue(2, $content);
$query->bindValue(3, $image);
$query->bindValue(4, $link);
$query->bindValue(5, $category);
$query->bindValue(6, $brand);
$query->execute();
header('location: index.php');
}
}
?>
<?php
if (isset($_FILES['Filedata']))
{
// And if it was ok
if ($_FILES['Filedata']['error'] !== UPLOAD_ERR_OK)
exit('Upload failed. Error code: ' . $_FILES['image']['error']);
$filename = $_FILES['Filedata']['name'];
$targetpath = "../img/news/" . $filename; //target directory relative to script location
$copy = copy($_FILES['Filedata']['tmp_name'], $targetpath);
}
?>
<html>
<head>
<title>Add Article</title>
<link rel="stylesheet" href="../other.css" />
</head>
<body>
<div class="container">
<a href="index.php" id="logo"><b>← Back</b></a>
<br />
<div align="center">
<h4>Add Article</h4>
<?php if (isset($error)) { ?>
<small style="color:#aa0000;"><?php echo $error; ?></small><br /><br />
<?php } ?>
<form action="add.php" method="post" autocomplete="off" enctype="multipart/form-data">
<input type="text" name="title" placeholder="Title" /><br /><br />
<textarea rows="15" cols="50" placeholder="Content" name="content"></textarea><br /><br />
<input name="imageupload" type="file" id="image" placeholder="Imageupload" />
<input type="text" name="image" placeholder="Image" /><br /><br />
<input type="link" name="link" placeholder="Link" /><br /><br />
<input type="category" name="category" placeholder="Category" /><br /><br />
<input type="category" name="brand" placeholder="Brand" /><br /><br />
<input type="submit" value="Add Article" />
</form>
</div>
</div>
</body>
</html>
<?php
}else{
header('location: index.php');
}
?>
请帮忙。
答案 0 :(得分:1)
if (move_uploaded_file($file_tmp, 'images/'.$file_name)) {
echo 'File uploaded';
$image = '/images/'.$filename;//try updating the line like this