2013-08-28 79 views
0

我創建自己的CMS,我已經添加了一個上傳頁面。添加到數據庫字段時使用其中一個或另一個

我想要做的是將2個輸入類型添加到我的ADD.PHP窗體。首先是使用上傳按鈕添加圖像。

第二個是使用URL添加圖像。

這兩個都應該保存到我的MOBI數據庫中的同一個promo_image在mysql中。

我想如果這兩個字段都輸入,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>&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'); 
} 

?> 

因爲它目前爲我收到的錯誤:

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

我只是不太確定如何做到這一點。請有人幫忙。謝謝。

回答

1

它使用一個PHP短的if/else語法:

$query->bindValue(3, !empty($image) ? $image : $imageupload); 
0

你$形象,$ 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>&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'); 
} 
?> 
+0

好我的麻煩,現在壽是這樣的:這就像一個魅力但它實際上並沒有上傳圖像,可以說我的文件在我的計算機上名爲XXXXXXXX.jpg,它讀取文件但不保存到系統中,我怎樣才能將它保存到我的/ images文件夾中?謝謝。 – kevstarlive

+0

嗨。哪裏最好補充一點?我已經嘗試了幾個位置,但沒有成功。 – kevstarlive

+0

仍然沒有快樂。根本不保存圖像。 – kevstarlive

相關問題