PHP将文件上传到服务器,获取url,将所有内容添加到数据库中

时间:2014-10-21 11:32:01

标签: php mysql database video upload

我有插入代码:

<?php

include '../connect/con.php';

$id = mysqli_real_escape_string($con, $_POST['id']);
$vidLink = mysqli_real_escape_string($con, $_POST['vidLink']);
$vidTreilerLink = mysqli_real_escape_string($con, $_POST['vidTreilerLink']);


$sql="INSERT INTO videolinks (id, vidlink, vidTreilerLink) VALUES ('$id', '$vidLink', '$vidTreilerLink')";

if (!mysqli_query($con,$sql)) {
  die('Error: ' . mysqli_error($con));
}echo "Video links are added";

mysqli_close($con);
?>

和表单以向数据库添加信息:

<html>
<body>

<form action="../insert/insertVidLinks.php" method="post">
<table style="width:500px, margin-left:50px">  
    <table style="width:500px, margin-left:50px">  
    <tr><td>Video ID: </td>     
<td><select id="id" name="id">

            <?php

            $mysqlserver="localhost";
            $mysqlusername="admim";
            $mysqlpassword="admin";
            $link=mysql_connect(localhost, $mysqlusername, $mysqlpassword) or die ("Error connecting to mysql server: ".mysql_error());

            $dbname = 'databaseName';
            mysql_select_db($dbname, $link) or die ("Error selecting specified database on mysql server: ".mysql_error());

            $cdquery="SELECT * FROM newsvid";
            $cdresult=mysql_query($cdquery) or die ("Query to get data from firsttable failed: ".mysql_error());

            while ($cdrow=mysql_fetch_array($cdresult)) {
            $cdTitle=$cdrow["id"];
                echo "<option>
                    $cdTitle
                </option>"; } ?>

 </select>
</td></tr>
  <tr><td>VideoLink: </td><td><input type="text" name="vidLink"></td></tr>
  <tr><td>VideoTLink:</td><td><input type="text" name="vidTreilerLink"></td></tr>

  <tr align="right"><td></td><td style="padding-top:10px"><input type="submit"></td></tr> 
</table>    
</form>


</body>
</html>

问题是:我想...当我选择ID然后而不是videoLink和videoTLink我需要选择要上传的文件

所以我选择ID例如ID6

然后我必须输入VideoLink,但我需要选择要上传的文件

然后需要输入VideoTLink但是我需要再次选择要上传的文件

最后我需要UPLOAD文件夹中的两个文件以及数据库中的ID,videoLink(URL)和videoTLink(URL)。

我有上传文件脚本的示例:

<?php
$target_dir = "../../uploads/";
$target_dir = $target_dir . basename( $_FILES["uploadFile"]["name"]);
$uploadOk=1;

// Check if file already exists
if (file_exists($target_dir . $_FILES["uploadFile"]["name"])) {
    echo "Sorry, file already exists.";
    $uploadOk = 0;
}

// Check file size
if ($uploadFile_size > 128000000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}

// Check if $uploadOk is set to 0 by an error
if ($ok==0) {
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else { 
    if (move_uploaded_file($_FILES["uploadFile"]["tmp_name"], $target_dir)) {
        echo "The file ". basename( $_FILES["uploadFile"]["name"]). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}
?>

数据库应如下所示:

--------------------------------------------------------------------
--ID6--http://website/videolink.mp4--http://website/videoTLink.mp4--
--ID7--http://website/videolink.mp4--http://website/videoTLink.mp4--
--ID8--http://website/videolink.mp4--http://website/videoTLink.mp4--
--ID9--http://website/videolink.mp4--http://website/videoTLink.mp4--
--------------------------------------------------------------------

0 个答案:

没有答案