我自己创建了一个CMS,我添加了一个上传页面。
我要做的是在我的ADD.PHP表单上添加2种输入类型。第一种是使用上传按钮添加图像。
第二个是使用URL添加图像。
这两个都应该保存到mysql中我的MOBI数据库中的同一个promo_image中。
如果输入两个字段,我希望URL版本成为优先级。
我目前为此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']);
$image = $_POST['image'];
$imageupload = $_POST['image'];
$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, if(trigger){empty(["image"])} else {"imageupload"});
$query->bindValue(4, $link);
$query->bindValue(5, $category);
$query->bindValue(6, $brand);
$query->execute();
header('location: index.php');
}
}
?>
<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">
<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');
}
?>
目前看来我得到的错误是:
Parse error: syntax error, unexpected T_IF in admin/add.php on line 23
我只是不确定如何做到这一点。请有人帮忙。谢谢。
答案 0 :(得分:1)
这使用PHP短if / else语法:
$query->bindValue(3, !empty($image) ? $image : $imageupload);
答案 1 :(得分:0)
你的$ image和$ imageupload是相同的值,所以if语句是多余的?尝试以下方法。
更新文件上传(您从表单标签中删除了enctype。还要确保targetpath目录存在且设置为chmod 777
<?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['Filedata']))
{
$filename = $_FILES['Filedata']['name'];
$targetpath = "../put/path/here" . $filename; //target directory relative to script location
$copy = copy($_FILES['Filedata']['tmp_name'], $targetpath);
if (!$copy)
$error = "Image was not uploaded successfully";
}
}
$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');
}
}
?>
<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');
}
?>