提交表单数据时php脚本更改

时间:2017-05-15 07:50:58

标签: php html mysql forms

我构建了一个名为add_post.php的脚本,用于在数据库中提交表单数据。 脚本在这里:

    <?php include ("includes/header.php"); ?>
    <div class="content" style="text-align: center;">
        <header>
            <h1 style="color: rgb(245, 106, 106);">ADMIN AREA</h1><br/>
            <p>Movie Post Area</p>
        </header>
    </div>
</section>
    <?php
    include("includes/startsession.php");       
    if(isset($_SESSION['username'])){

?>
<Section>
<?php
            if(isset($_POST['submit'])){
                $name =mysqli_real_escape_string($con, trim($_POST['name']));
                $description = mysqli_real_escape_string($con, trim($_POST['description']));
                $year = mysqli_real_escape_string($con, trim($_POST['year']));
                $popularity = mysqli_real_escape_string($con, trim($_POST['popularity']));
                $download_link = mysqli_real_escape_string($con, trim($_POST['download']));
                $tags = mysqli_real_escape_string($con, trim($_POST['tags']));

                //upload the name to the temporary folder
                $images = mysqli_real_escape_string($con, trim($_FILES['image']['name']));
                //select the image from the temporary folder
                $images_tmp = $_FILES['image']['tmp_name']; 
                //move the uploaded image to the `images` folder
                move_uploaded_file($images_tmp, "../images/$images");

                $output_form = false;
                if( empty($name) || empty($description) || empty($year) || empty($popularity)   || empty($download_link)    || empty($tags) || empty($images)){
                    echo '<h2>Oops! You seem to forget writing something</h2>';
                    $output_form = true;
                }

                if( (!empty($name)) && (!empty($description))   && (!empty($year))  && (!empty($popularity))    && (!empty($download_link)) && (!empty($tags))  && (!empty($images))){


                $insert = "INSERT INTO movies (name, description, year, image, popularity, download_link, tags)
                            VALUES ('$name', '$description', '$year', '$images', '$popularity', '$download_link', '$tags') " or die(mysqli_error);

                $run = mysqli_query($con, $insert);

                echo "<h2>The Movie has been added.<h2>";
                echo '<a href="'.$_SERVER['PHP_SELF'].'" class="button">RESET</a>';
                }
            } else{
                    $output_form = true;
                    $name = '';
                    $description = '';
                    $year = '';
                    $popularity = '';
                    $download_link = '';
                    $tags = '';
                    $images = '';
                }
        ?>

<?php if($output_form = true){ ?>
    <form enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
                        <div class="row uniform">
                            <div class="6u 12u$(xsmall)" style="float:none">
                                <label>Movie Name</label>
                                <input type="text" name="name" value="<?php echo $name; ?>" id="demo-name" value="" placeholder="Enter Movie Name" />
                            </div>
                            <br/>
                            <div class="6u 12u$(xsmall)" style="float:none">
                                <label>Description</label>
                                <textarea name="description"  id="demo-message" placeholder="Enter Description" rows="6"><?php echo $description; ?></textarea>
                            </div>
                            <br/>
                            <div class="6u 12u$(xsmall)" style="float:none">
                                <label>Year</label>
                                <input type="text" name="year" value="<?php echo $year; ?>" id="demo-name" placeholder="Enter Year" />
                            </div>
                            <br/>                       
                            <div class="6u 12u$(xsmall)" style="float:none">
                                <label>Image</label>
                                <input type="file" name="image" value="<?php echo $images; ?>" id="demo-name" class="special" />
                            </div>
                            <div class="12u$" style="float:none">
                                <div class="select-wrapper">
                                    <label>Popularity</label>
                                    <select name="popularity" id="demo-category">
                                        <option <?php if ($popularity == 'Yes'){echo 'selected';} ?> >Yes</option>
                                        <option <?php if ($popularity == 'No'){echo 'selected';} ?> >No</option>
                                    </select>
                                </div>
                            </div>
                            <div class="6u 12u$(xsmall)" style="float:none">
                                <label>Download Link</label>
                                <input type="text" name="download" value="<?php echo $download_link; ?>" id="demo-name" value="" placeholder="Enter Download Link" />
                            </div>
                            <div class="6u 12u$(xsmall)" style="float:none">
                                <label>Tags</label>
                                <input type="text" name="tags" value="<?php echo $tags; ?>" id="demo-name" value="" placeholder="Enter Tags seperated by commas(,)" />
                            </div>

                            <!-- Break -->
                            <div class="12u$" style="float:none">
                                <ul class="actions">
                                    <li><input type="submit" name="submit" value="ADD MOVIE" class="special" /></li>

                                </ul>
                            </div>
                        </div>
                    </form>
<?php } ?>

<?php 
    } else{
        header('Location: login.php');
    }
?>

当我提交表单时,页面返回index.php的脚本。当我刷新页面时,页面显示add_post.php的内容,最重要的是表单的所有数据内容都没有添加到数据库中。 我不知道是什么导致了这个问题,以及如何解决这个问题。

0 个答案:

没有答案