2013-08-30 33 views
0

爲我的網站和我的管理頁面創建CMS我有一個添加頁面,將新內容添加到我的網站LOCATED HERE並添加了一些表單域。上傳圖像並添加鏈接到mysql

這些值分別爲:

圖片URL(文本框)&上傳圖片(選擇文件按鈕)

當我填寫所有的Fileds並且使用圖像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>&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'); 
} 

?> 

請幫忙。

回答

1
if (move_uploaded_file($file_tmp, 'images/'.$file_name)) { 
         echo 'File uploaded';       
      $image = '/images/'.$filename;//try updating the line like this