PHP在提交后不发布数据

时间:2018-04-02 17:36:30

标签: php

我无法弄清楚为什么我的表单在单击提交后不想将其数据提交到我的食谱表中。当我单击提交按钮时,表单只是刷新, 我有一个成功集的回显,但它没有出现错误消息和我的数据库中没有数据。

这是HTML

<div class="row">
<div class="main-login main-center">
    <h1>Add Recipe</h1>
    <form enctype="multipart/form-data" action="#" method="post">

        <div class="form-group">
            <label for="recipe_name" class="cols-sm-2 control-label">Recipe Name</label>
            <div class="cols-sm-10">
                <div class="input-group">
                    <input type="text" class="form-control" name="recipe_name" id="recipe_name"  placeholder="Recipe Name"/>
                </div>
            </div>
        </div>

        <div class="form-group">
            <label for="recipe_duration" class="cols-sm-2 control-label"> Recipe Duration </label>
            <div class="cols-sm-10">
                <div class="input-group">
                    <input type="text" class="form-control" name="recipe_duration" id="recipe_duration"  placeholder="Recipe Duration"/>
                </div>
            </div>
        </div>

        <div class="form-group">
            <label for="recipe_ingredient" class="cols-sm-2 control-label"> Ingredients </label>
            <div class="cols-sm-10">
                <div class="input-group">
                    <input type="text" class="form-control" name="recipe_ingredient" id="recipe_ingredient"  placeholder="Ingredients"/>
                </div>
            </div>
        </div>

        <div class="form-group">
            <label for="recipe_nutrition" class="cols-sm-2 control-label"> Recipe Nutrition </label>
            <div class="cols-sm-10">
                <div class="input-group">
                    <input type="text" class="form-control" name="recipe_nutrition" id="recipe_nutrition"  placeholder="Recipe Nutrition"/>
                </div>
            </div>
        </div>

        <div class="form-group">
            <label for="recipe_method" class="cols-sm-2 control-label"> Recipe Directions </label>
            <div class="cols-sm-10">
                <div class="input-group">
                    <input type="text" class="form-control" name="recipe_method" id="recipe_method"  placeholder="Recipe Directions"/>
                </div>
            </div>
        </div>

        <div class="form-group">
            <label for="profile_image">Recipe Image</label>
            <input type="file" name="recipe_image" id="recipe_image">
            <p class="help-block">Upload an image of the recipe</p>
            <img class="recipeImage" src="./recipe_images/"/>
        </div>

        <div class="form-group">
            <button type="submit" class="btn btn-success btn-lg btn-block login-button">Submit</button>
        </div>

    </form>
</div>

和PHP

                    <?php
    if(isset($_POST['submit'])){

        //Insert DB
        $query = "INSERT INTO recipes (recipe_name, recipe_duration, recipe_ingredient, recipe_nutrition, recipe_method) VALUES (:recipe_name, :recipe_duration, :recipe_in

gredient, :recipe_nutrition, :recipe_method)";
    $result = $DBH->prepare($query);
    $result->bindParam(':recipe_name', $_POST['recipe_name']);
    $result->bindParam(':recipe_duration', $_POST['recipe_duration']);
    $result->bindParam(':recipe_ingredient', $_POST['recipe_ingredient']);
    $result->bindParam(':recipe_nutrition', $_POST['recipe_nutrition']);
    $result->bindParam(':recipe_method', $_POST['recipe_method']);
    if($target_file){
        $result->bindParam(':recipeImage', $newFilename);
    }
    if($result->execute()){
        echo '<div class="alert alert-success" role="alert">Recipe Added!</div>';
    }

    if($_FILES['recipeImage']["name"]){
        //Let's add a random string of numbers to the start of the filename to make it unique!

        $newFilename = md5(uniqid(rand(), true)).$_FILES["profile_image"]["name"];
        $target_file = "./recipe_images/" . basename($newFilename);
        $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

        // Check if image file is a actual image or fake image
        $check = getimagesize($_FILES["recipeImage"]["tmp_name"]);
        if($check === false) {
            echo "File is not an image!";
            $uploadError = true;
        }

        //Check file already exists - It really, really shouldn't!
        if (file_exists($target_file)) {
            echo "Sorry, file already exists.";
            $uploadError = true;
        }

        // Check file size
        if ($_FILES["recipeImage"]["size"] > 500000) {
            echo "Sorry, your file is too large.";
            $uploadError = true;
        }

        // Allow certain file formats
        if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
        && $imageFileType != "gif" ) {
            echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
            $uploadError = true;
        }

        // Did we hit an error?
        if ($uploadError) {
            echo "Sorry, your file was not uploaded.";
        } else {
            //Save file
            if (move_uploaded_file($_FILES["recipeImage"]["tmp_name"], $target_file)) {
                //Success!
            } else {
                echo "Sorry, there was an error uploading your file.";
            }
        }
    }
}

            ?>

4 个答案:

答案 0 :(得分:3)

因为您没有name="submit"的元素,而您在此处if(isset($_POST['submit'])){进行了测试

您的提交应该是input,而不是button

<input type="submit" class="btn btn-success btn-lg btn-block login-button" value="Submit" />

答案 1 :(得分:0)

<input type="submit" name="submit" value="Submit"class="btn btn-success btn-lg btn-block login-button">

像这样更改提交按钮

答案 2 :(得分:0)

 <button type="submit" class="btn btn-success btn-lg btn-block login-button">Submit</button>

而不是

 <input type="submit" class="btn btn-success btn-lg btn-block login-button" value="Submit" name="submit">

答案 3 :(得分:0)

更改

<button type="submit" class="btn btn-success btn-lg btn-block login-button">Submit</button>

<button type="submit" name="submit" class="btn btn-success btn-lg btn-block login-button">Submit</button>

实际上你需要为button元素添加一个name属性,这样你才能识别以下php行中的元素

if(isset($_POST['submit'])){

上面一行检查&#34; POST&#34; ed元素,用名称&#39;提交&#39;。