0

Im creating myself a CMS which I have added an upload page.

what I am trying to do is add 2 input types on to my ADD.PHP form. The first is to add an image using the upload button.

The second one is to add an image using a URL.

Both of these should be saved to the same promo_image within my MOBI database in mysql.

What I would like is for the URL version to be priority for if both the fields are entered.

The code I currently have for this ADD.PHP page is:

<?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>&larr; 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');
}

?>

As it currently stands I am getting the error of:

Parse error: syntax error, unexpected T_IF in admin/add.php on line 23

Im just not so sure on how to do it. please can someone help. thank you.

4

2 回答 2

1

这使用 PHP 简短的 if/else 语法:

$query->bindValue(3, !empty($image) ? $image : $imageupload);
于 2013-08-28T10:43:29.677 回答
0

您的 $image 和 $imageupload 是相同的值,所以 if 语句是多余的?尝试以下。

更新文件上传(您在表单标签中缺少 enctype。还要确保目标路径目录存在并设置为 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>&larr; 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');
}
?>
于 2013-08-28T10:49:51.547 回答