drktvjp713333 2015-10-20 09:07 采纳率: 0%
浏览 5
已采纳

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

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.

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.

<?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条回答 默认 最新

  • dora12345678 2015-10-20 09:11
    关注

    Change

    $edit = $_GET['edit'];
    

    to

    $edit = $_POST['edit'];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿