English 中文(简体)
用 PHP 存储视频时未定义索引错误
原标题:Undefined index error when storing video with PHP

I have a problem with my php code. I am trying to write data into mysql using a form. the form contains a video upload as well that stores the video locally and stores an url of the video into the database. The problem is when i m testing it it comes up with multiple errors of undefined index for videoname , videoupload , videodescription . the funny thing is that if i m not choosing a file to upload and still input something in the other fields it writes the info in the database and doesn t come up with errors. So it s something related to the video. Does anybody have an idea of what it could be? Thanks! Code for the form:

<form action="videoUpload.php" id="videoUp" method= POST  enctype="multipart/form-data">
        <p>Name:<textarea name="videoname" value="" class="name" ></textarea></p>
        <p>Upload video:<input type="hidden" name="MAX_FILE_SIZE" value="10485760">   <input type="file" name="videoupload"> </p>
        <p>Video Description:<textarea name="videodescription" value="" class="step" ></textarea></p>
        <p><input type="submit" name="videosubmit" value="Submit Video" class="submit" /></p>
    </form>

和php:

 <?php  
    session_start();

    //This is the directory where images will be saved
    $target = "assets/video/";
    $target = $target . basename( $_FILES[ videoupload ][ name ]);

    $name = $_POST[ videoname ];
    $description = $_POST[ videodescription ];


    $connect = mysql_connect("localhost","root","") or die("Couldn t connect");
    mysql_select_db("fyp") or die("Couldn t find db");

    $queryreg = mysql_query("INSERT INTO videos(VideoName,VideoLocation,VideoDescription) VALUES( $name , $target , $description )");

    //Writes the photo to the server
    if(move_uploaded_file($_FILES[ videoupload ][ tmp_name ], $target))
    {

        //Tells you if its all ok
        echo "The file  has been uploaded, and your information has been added to the directory";
    }
    else {

    //Gives and error if its not
    echo "Sorry, there was a problem uploading your file.";
}
?>
问题回答

首先,请检查 ", http://www.php.net/ manual/en/ features. file-upload.errors.php" rel=“ nofollow”>error code 上传文件时查看以下的 < a > :

$_FILES[ videoupload ][ error ]

然后写出错误和成功情景的代码 。

有关未定义索引的错误只告诉您, 您试图访问的 POST 值没有定义 。 使用您最喜爱的浏览器的开发工具 或 < a href=" http:// www. fiddr2. com/ fiddr2/" rel="nofollow" > Fiddler 来分析实际的 HTTP 请求并确保其通过 。

在表格中插入该行的事实没有什么好笑的:对实际值没有验证。无论是否声明变量,查询都会被执行。未指定的变量不会在 PHP 中造成错误。

一般而言,您的代码在许多地方缺乏验证:

  • No input validation
  • No validation of the return value of mysql_query()
  • Open to SQL injections




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签