gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 01:23:04 27 4
gpt4 key购买 nike

现在我正在尝试为我的论坛创建一个编辑表单,虽然我可以对其进行编辑,但它不会将 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 是它在点击帖子上的“编辑”时获取的 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>';
}

?>

最佳答案

改变

$edit = $_GET['edit'];

$edit = $_POST['edit'];

关于php - 无法从 HTML 表单获取 ID 以插入数据库(使用 PHP),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33232463/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com