将图像和表单上传到数据库

时间:2014-11-22 07:12:55

标签: php database upload submit

所以,我一直想知道这个剧本,但仍然无法做到正确。由于某种原因,它不会保存到我的数据库。任何想法为什么它不起作用?非常感谢任何帮助。谢谢!这是我的剧本。

 <?php 

 include_once ("database.php"); ?>

 <?php


 if (isset($_POST['anisave'])) {
    $id = $_POST['id'];
    $title = $_POST['title'];
    $genre = $_POST['genre'];
    $description = $_POST['description'];
    $start = $_POST['start'];
    $stop = $_POST['stop'];
    $image_file = $_FILES['image']['name'];
    $type = $_FILES['image']['type'];
    $size = $_FILES['image']['size'];

    if (empty($image_file) || empty($id)) {
        echo "Sorry, form is not complete yet!";
        header('Location: add.php');
    }
    else{
        $query_id = mysql_query("SELECT * FROM anidata WHERE id = '$id'");
        $check = mysql_num_rows($query_id);

        if ($check > 0) {
            echo "Sorry, Anime ID not available";
            header('Location: add.php');
        }
        else{
            if ($type != "image/gif" && $type != "image/jpg" && $type != "image/jpeg" && $type != "image/png") {
                echo "Invalid image file, please use JPEG,JPG,PNG or GIF to upload the image."
                header('Location: add.php');
            }
            if ($size > 10000) {
                echo "Affordable file is under 10mB."
                header('Location: add.php');
            }
            else{
                $upload_directory = 'upload/';
                $temp = $upload_directory.$image_file;

                if (move_uploaded_file($_FILES['image']['tmp_name'] , $temp)) {
                    $sql = "INSERT INTO anidata VALUES ('$id', '$title', '$temp', '$genre', '$description','$start', '$stop')";
                    $query = mysql_query($sql)

                    if ($query) {
                        header('Location: view.php');
                    }
                    else{
                        echo mysql_query();
                    }
                }
                else{
                    echo "<p> Upload Failed, error code = " . $_FILES['location']['error']. "</p>";
                }
            }
        }
    }
 }
 else{
    unset($_POST['anisave']);
 }


  ?>

0 个答案:

没有答案