将post image url上传到mysql数据库,并将多个url放在一行

时间:2016-03-08 16:11:52

标签: php 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 而且我想在一行中有多个图像网址,这可能吗?

感谢。

修改

查询的额外信息: 我的数据库名称是&#34; portalen&#34;行名是&#34; leerlingen&#34;。

2 个答案:

答案 0 :(得分:1)

首先,看看你的phpMySql screenshoot,我建议你看看字段长度文档。你真的不需要varchar(1000)来保存电子邮件地址,也不需要varchar(9999)来存储文件名。

对于您想要做的事情,您只需要在MySQL中查询要添加$ RandomAccountNumber的内容。 &#34; png格式&#34;到img_url字段。

警告:您的脚本假定该文件是PNG。我建议您查看文档或一些示例,了解如何确定文件mime类型以将正确的扩展名添加到已保存的文件中。这里有一个很好的例子:http://php.net/manual/en/features.file-upload.php

答案 1 :(得分: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 @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@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 CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

只需将该代码放入.sql文件并导入即可。

谢谢大家。