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

时间:2022-09-25 20:07:02

Right now I am trying to create a edit form for my forum, and while I CAN get it to edit, it does not insert an ID to the database (rendering it 0 and thus faulty), the ID field have Auto increment and I've double checked it is the primary field. Tried looking over it many times, but there's got to be something I am missing.

现在我正在尝试为我的论坛创建一个编辑表单,虽然我可以让它进行编辑,但它不会向数据库插入一个ID(将其呈现为0因此有问题),ID字段具有自动增量而且我'双重检查它是主要领域。试过多次看它,但必须有一些我想念的东西。

The DB connection:

数据库连接:

<?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>");

?>

The form where you edit the post ($edit in this scenario is the ID it grabs when clicking "edit" on a post), as well as where I try to update the database field.

编辑帖子的表单(此场景中的$ edit是在帖子上单击“编辑”时抓取的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 个解决方案

#1


1  

Change

更改

$edit = $_GET['edit'];

to

$edit = $_POST['edit'];

#1


1  

Change

更改

$edit = $_GET['edit'];

to

$edit = $_POST['edit'];