首页 > 解决方案 > How to debug a HTML/PHP form not submitting properly

问题描述

I've looked at this code until I'm cross-eyed and can't see the error I'm making. I'm a bit of a beginner.

My HTML - editPost.php:

<?php 
    session_start();
    include "includes/header.php";
    include "connectioninfo.php";
    include "functions.php";

    if(isset($_SESSION['user']))
    {
        editPost();
    }
    else
    {
        header("Location: /");
    }

    $return = getPost();
?>

<div class="container">
    <form action="editPost.php" method="post">
            <?php $id = $_GET['id']?>
            <input type="hidden" name="id" value="<?php echo $id?>">
        <div class="row">
            <div class="lab">
                <label for="category">Category:<br/></label>
            </div>
            <div class="inp">
                <select id="category" required autofocus name="category">
                    <option value="" selected disabled hidden>Choose a category.</option>
                    <option value="Something">About</option>
                    <option value="Something else">Coding</option>
                </select>
            </div>
        </div>
        <div class="row">
            <div class="lab">
                <label for="title">Title.</label>
            </div>
            <div class="inp">
                <input type="text" name="title" placeholder="Title" required value="<?php echo $return[0]?>">
            </div>
        </div>
        <div class="row">
            <div class="lab">
                <label for="content">Content.</label>
            </div>
            <div class="inp">
                <textarea name="content" id="content" style="height: 30em;"><?php echo $return[1]?></textarea>
            </div>
        </div>
        <div class="row">
            <input type="submit" name="submit" value="Post.">
        </div>
    </form>
</div>
<?php
include "includes/footer.php";
?>

getPost() is just getting the values to autofill the form. it's a function in the included functions.php:

function getPost()
{
    global $connection;

    $id=$_GET['id'];
    $query = "SELECT * FROM database WHERE id = '$id'";
    $result = $connection->query($query);    
    if($result)
    {
        while($post = $result->fetch_object())
        {
            $id = $post->id;
            $title = $post->title;
            $link = $post->permalink;
            $summary = $post->summary;
            $category = $post->category;
            $content = $post->content;
            $pubDate = $post->pubDate; 
            $author = $post->author;
            $return = array($title,$content);
            return $return;
        }
    }
    else
    {
        die('Query FAILED!' . mysqli_error());
    }
}

and finally, editPost()

function editPost()
{
    global $connection;

    if(isset($_POST['submit']))
    {
        global $connection;

        $title = mysqli_real_escape_string($connection,$_POST['title']);
        $content = mysqli_real_escape_string($connection,$_POST['content']);
        $category = $_POST['category'];
        $id = $_POST['id'];
        //Permalink
        $link = strtolower(trim($title));
        $link = preg_replace('/[^a-z0-9-]/', '-', $link);
        $link = preg_replace('/-+/', "-", $link);
        $link = rtrim($link, '-');
        $link = preg_replace('/\s+/', '-', $link);        

        $query = "UPDATE database SET title = '$title', permalink = '$link', content = '$content', category = '$category' ";
        $query .= "WHERE id = '$id'";

        $result = $connection->query($query);
        if(!$result)
        {
            die('Query FAILED!' . mysqli_error());
        }
        else
        {
            header("Location: /");
        }
        $result->close();
    }
}

Clicking on the edit link of a post brings me to this form, and it looks great - title and content are filled out with what's in the database, and I'm ready to edit.

The process (both html and function) is nearly identical to my createPost.php, and that works fine. but editPost.php just sends me back to the same page, with no values in the fields, and the post hasn't been updated. No error messages either.

What am I missing?

Edit

As a reference, I'm posting the contents of newPost.php and the function newPost() - which are working fine.

newPost.php:

<?php 
    session_start();
    include "connectioninfo.php";
    include "functions.php";

    if(isset($_SESSION['user']))
    {
        newPost();
    }
    else
    {
        header("Location: /");
    }

    include "includes/header.php";
?>

<div class="container">
    <form action="newPost.php" method="post">
        <div class="row">
            <div class="lab">
                <label for="category">Category.</label>
            </div>
            <div class="inp">
                <select id="category" required autofocus name="category">
                    <option value="" selected disabled hidden>Choose a category.</option>
                    <option value="About">About</option>
                    <option value="Coding">Coding</option>
                </select>
            </div>
        </div>
        <div class="row">
            <div class ="lab">
                <label for="title">Title.</label>
            </div>
            <div class ="inp">
                <input type="text" name="title" required placeholder="Title">
            </div>
        </div>
        <div class="row">
            <div class ="lab">
                <label for="summary">Summary.</label>
            </div>
            <div class ="inp">
                <input type="text" name="summary" required placeholder="Summary (for the RSS feed and Twitter)">
            </div>
        </div>
        <div class="row">
            <div class="lab">
                <label for="content">Content.</label>
            </div>
            <div class="inp">
                <textarea name="content" id="content" placeholder="The content of the post" style="height: 30em;"></textarea>
            </div>
        </div>
        <div class="row">
            <input type="submit" name="submit" value="Post.">
        </div>
    </form>   
</div>
<?php
include "includes/footer.php";
?>

newPost():

function newPost()
{
    if(isset($_POST['submit']))
    {
        global $connection;

        $title = mysqli_real_escape_string($connection,$_POST['title']);
        $summary = mysqli_real_escape_string($connection,$_POST['summary']);
        $content = mysqli_real_escape_string($connection,$_POST['content']);
        $category = $_POST['category'];
        $pubDate = date("Y-m-d H:i:s");
        $author = $_SESSION['user'];
        //Permalink
        $link = strtolower(trim($title));
        $link = preg_replace('/[^a-z0-9-]/', '-', $link);
        $link = preg_replace('/-+/', "-", $link);
        $link = rtrim($link, '-');
        $link = preg_replace('/\s+/', '-', $link);

        $query = "INSERT INTO database(title, permalink, category, summary, content, pubDate, author) ";
        $query .= "VALUES ('$title', '$link', '$category', '$summary', '$content', '$pubDate', '$author')";

        $result = $connection->query($query);
        if(!$result)
        {
            die('Query FAILED!' . mysqli_error());
        }
        else
        {
            header("Location: /");
        }
        $result->close();
    }
}

标签: phphtmlmysqlforms

解决方案


thanks to everyone for their help. As I found out and stated in the comments, the problem was in my .htaccess

I do a rewrite in .htaccess - mysite.com/editPost.php?id=1 is actually mysite.com/edit/1 - running the long form WORKS, the short form is giving me the error.

My .htaccess has RewriteRule ^edit/([^/.]+)?$ /editPost?id=$1 [L] I just had to change <form action="editPost.php" method="post"> in editPost.php to <form action="edit" method="post"> and it works no problem :-/


推荐阅读