简体   繁体   中英

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>

And 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.";
}
?>

First of all, check the error code for your file upload:

$_FILES['videoupload']['error']

Then write code for error and success scenarios.

The error about undefined indexes justs tell you that the POST values you are trying to access are not defined. There must be something wrong with the <form> . Use the developer tools of your favourite browser or Fiddler to analyse the actual HTTP request and ensure that the values are passed.

There is nothing funny about the fact that the row is inserted into the table: there is no validation for actual values. The query is executed regardless of the variables being declared or not. Unassigned variables are not causing errors in PHP.

In general your code lacks of validation in various places:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM