无法从HTML表单中获取要插入数据库的ID(使用PHP)

时间:2015-10-20 09:07:41

标签: php html mysql

现在我正在尝试为我的论坛创建一个编辑表单,虽然我可以对其进行编辑,但它不会向数据库插入ID(将其呈现为0并因此出错),ID字段具有自动增量我已经仔细检查过它是主要领域。我多次尝试过这种情况,但我必须要做的就是我所缺少的东西。

数据库连接:

<?php
error_reporting(E_ALL);
session_start();

$host = 'HOSTNAME';
$dbusername = 'USERNAME';
$dbpassword = 'PASSWORD';

$anslutning = mysqli_connect($host, $dbusername, $dbpassword) or die("<b>Could not connect to database server</b>");

$anslutning->select_db('DATABASE NAME') or die("<b>Could not connect to the specified database</b>");

?>

您在此方案中编辑帖子($edit的表单是在点击&#34;编辑&#34;在帖子上)时获取的ID,以及我尝试更新数据库字段的位置

<?php

            if(isset($_GET['edit'])) {
            // If click on "edit"


                $edit = $_GET['edit'];
                // The post-editing ID

                $getEditData = $anslutning->prepare("SELECT postId, title, content FROM tblPosts WHERE postid='$edit' LIMIT 1");
                $getEditData->bind_result($postId, $title, $content);
                $getEditData->store_result();
                $getEditData->execute();

                while($row = $getEditData->fetch()) {

                    echo '
                    <div class="editForm">
                        <form action="index.php" method="POST">
                            <input type="hidden" name="author" value="'.$_SESSION['loggedIn'].'">
                            <input type="hidden" name="edit" value="'.$edit.'">
                            Title: <input type="text" name="new_title" value="'.$title.'"> <br /> <br />
                            Content: <textarea name="new_content"> '.$content.' </textarea> <br /> <br />

                            <input type="submit" name="editPost">
                        </form>
                    </div>
                    ';
            }
            }

            // Issue(s): Editing a post does not send postId/edit(id) to database

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

                $edit = $_GET['edit'];
                $author = $_POST['author'];
                $new_title = $_POST['new_title'];
                $new_content = $_POST['new_content'];

                $updatePost = $anslutning->prepare("UPDATE tblPosts SET postId=?, author=?, title=?, content=?");
                $updatePost->bind_param("isss", $edit, $author, $new_title, $new_content);
                $updatePost->execute();
                echo 'Post updated. Redirecting..';
                sleep(1);
                echo '<script> window.location.href = "index.php?forum=1" </script>';
            }

        ?>

1 个答案:

答案 0 :(得分:1)

更改

$edit = $_GET['edit'];

$edit = $_POST['edit'];