2016-03-08 52 views
1

我目前正在創建一個網站,你也可以放置圖片的博客文章,我已經有代碼上傳文件,但我也想把它在我的MYSQL行中。將帖子圖片網址上傳到mysql數據庫,並把多個網址放在一行

這裏是我的post_picture.php:

<form action="" method="POST" enctype="multipart/form-data"> 
<label for="file">Profile Pic:</label> <input type="file" name="ProfilePic" id="ProfilePic" /><br /> 

<input type="submit" name="submit" value="Submit" /> 

</form> 


<?php 
$con = mysql_connect("localhost", "a1070rik", ""); 
mysql_select_db("portals",$con); 
if(isset($_POST[submit])){ 
    $ProfilePicName = $_FILES["ProfilePic"]["name"]; 
    $ProfilePicType = $_FILES["ProfilePic"]["type"]; 
    $ProfilePicSize = $_FILES["ProfilePic"]["size"]; 
    $ProfilePicTemp = $_FILES["ProfilePic"]["tmp_name"]; 
    $ProfilePicError = $_FILES["ProfilePic"]["error"]; 

    $RandomAccountNumber = uniqid(); 
    move_uploaded_file($ProfilePicTemp, "Content/" . $RandomAccountNumber . ".png"); 
} 
?> 

這裏是我的MYSQL表結構的圖片: Picture 同時,我希望擁有多個圖像的URL在一排,這可能嗎?

謝謝。

編輯:查詢

額外信息: 我的數據庫名稱是 「portalen」 和行名稱爲 「leerlingen」。

回答

1

首先,看着你的phpMySql屏幕截圖,我建議你看一下字段長度的文檔。你真的不需要varchar(1000)來保存電子郵件地址,也不需要varchar(9999)來存儲文件名。

對於你想要做的事情,你只需要向MySQL添加$ RandomAccountNumber。 「img_url」字段中的「.png」。

警告:您的腳本假定該文件是PNG。我建議你看看文件或一些如何確定文件MIME類型的例子,以將正確的擴展名添加到保存的文件。這裏有一個很好的例子:http://php.net/manual/en/features.file-upload.php

+0

謝謝你將看到。你也可能知道如何把它放在我的MYSQL行中? –

+0

您的帖子沒有提供足夠的信息來爲您建立查詢。 – BeoWulf

+0

檢查編輯,現在好嗎? –

0

我自己找到了答案,它可能不是最安全的方式,但它現在起作用。 對於誰在這裏想它是人的代碼:

<?php 
if(isset($_POST[upload])){ 
    $fileExistsFlag = 0; 
    $fileName = uniqid() . $_FILES['Filename']['name']; 
    $link = mysqli_connect("localhost","a1070rik","","photos") or die("Error ".mysqli_error($link)); 
    /* 
    * If file is not present in the destination folder 
    */ 
    if($fileExistsFlag == 0) { 
     $target = "files/";  
     $fileTarget = $target.$fileName;  
     $tempFileName = $_FILES["Filename"]["tmp_name"]; 
     $fileDescription = $_POST['Description']; 
     $result = move_uploaded_file($tempFileName,$fileTarget); 
     /* 
     * If file was successfully uploaded in the destination folder 
     */ 
     if($result) { 
      echo "Your file <html><b><i>".$fileName."</i></b></html> has been successfully uploaded";  
      $query = "INSERT INTO images(filepath,filename,description) VALUES ('$fileTarget','$fileName','$fileDescription')"; 
      $link->query($query) or die("Error : ".mysqli_error($link));    
     } 
     else {   
      echo "Sorry !!! There was an error in uploading your file";   
     } 
     mysqli_close($link); 
    } 
    /* 
    * If file is already present in the destination folder 
    */ 
    else { 
     echo "File <html><b><i>".$fileName."</i></b></html> already exists in your folder. Please rename the file and try again."; 
     mysqli_close($link); 
    }}; 
?> 
<form method="post" action="" enctype="multipart/form-data"> 
    <p>File :</p> 
    <input type="file" name="Filename"> 
    <p>Description</p> 
    <textarea rows="10" cols="35" name="Description"></textarea> 
    <br/> 
    <input TYPE="submit" name="upload" value="Submit"/> 
</form> 

然後將代碼來創建相同的SQL表,因爲我有:

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; 
SET time_zone = "+00:00"; 


/*!40101 SET @[email protected]@CHARACTER_SET_CLIENT */; 
/*!40101 SET @[email protected]@CHARACTER_SET_RESULTS */; 
/*!40101 SET @[email protected]@COLLATION_CONNECTION */; 
/*!40101 SET NAMES utf8 */; 

-- 
-- Databank: `photos` 
-- 

-- -------------------------------------------------------- 

-- 
-- Tabelstructuur voor tabel `images` 
-- 

CREATE TABLE IF NOT EXISTS `images` (
    `filepath` varchar(999) NOT NULL, 
    `filename` varchar(1000) NOT NULL, 
    `description` varchar(999) NOT NULL 
) ENGINE=InnoDB DEFAULT CHARSET=latin1; 

/*!40101 SET [email protected]_CHARACTER_SET_CLIENT */; 
/*!40101 SET [email protected]_CHARACTER_SET_RESULTS */; 
/*!40101 SET [email protected]_COLLATION_CONNECTION */; 

只要把代碼中的.sql文件並導入它。

謝謝大家。

+0

你找到的代碼可能會解決你的問題,但可能不是很清楚,促進學習和解決其他問題。 由於很多原因,代碼非常不安全,其中一些非常基本,例如:您不檢查文件類型,並且添加rand()並不能保證不會有另一個具有相同名稱的文件。 uniqid()會爲此目的更好地工作。 – BeoWulf

+0

@BeoWulf謝謝​​你補充說:) –