gpt4 book ai didi

php - 无法将表单数据插入数据库(MySQLIi)

转载 作者:行者123 更新时间:2023-11-28 23:49:06 25 4
gpt4 key购买 nike

编辑:我不确定该说什么。这些评论现在帮助我解决了问题。我不记得显示错误的事实,这对我有帮助。我几乎只是尝试了一些显然由于错误放置的变量等而无法正常工作的东西,并且显示错误代码让我知道哪些变量是错误的。感谢您的帮助,此问题现已解决!

长话短说,标题。我不知道为什么这段代码拒绝将插入的数据输入到数据库中,而且我已经尝试了很多方法,但结果没有任何改善。

连接:

<?php
session_start();

$host = 'host';
$dbusername = 'username';
$dbpassword = 'password';

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

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

?>

从中获取数据的表单:

echo '
<h2><center>Post a topic</center></h2>
<br /><br />
<div class="indexform">
<form action="index.php" method="POST">
Title: <input type="text" name="title"> <br /> <br />
Content: <textarea name="content" class="content"> </textarea>
<br /> <br />
<input type="submit" name="postTopicOnGeneral">
</form>
</div>
';

将其插入数据库的PHP代码

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

$username = $_POST['username'];
$title = $_POST['title'];
$content = $_POST['content'];
$general = $_POST['general'];

$addPostOnGeneral = $anslutning->prepare('INSERT INTO tblPosts(title, content, author, category) VALUES(?, ?, ?, ?)');
$addPostOnGeneral->bind_param("ssss", $title, $content, $username, $general);
$addPostOnGeneral->execute();

echo "<center>Post created!</center>";
sleep(2);
echo "<script> window.location.href = 'index.php?general=1' </script>";

}

最佳答案

Undefined index:username 和 Undefined:general 是我从中得到的。

您一定会遇到该错误,因为您没有从表单中的任何位置获取 $username$general

改变你的形式:

echo '
<h2><center>Post a topic</center></h2>
<br /><br />
<div class="indexform">
<form action="index.php" method="POST">
Title: <input type="text" name="title"> <br /> <br />
Content: <textarea name="content" class="content"> </textarea>
<br /> <br />
<input type="submit" name="postTopicOnGeneral">
</form>
</div>';

为此:

echo '
<h2><center>Post a topic</center></h2>
<br /><br />
<div class="indexform">
<form action="index.php" method="POST">
Title: <input type="text" name="title"> <br /> <br />
Content: <textarea name="content" class="content"> </textarea>
<br /> <br />
Username:
<input type="text" name="username">
General:
<input type="text" name="general">
<input type="submit" name="postTopicOnGeneral">
</form>
</div>';

然后在你的 index.php 中:

 if(isset($_POST['postTopicOnGeneral'])) {
echo $username = $_POST['username'];
echo $title = $_POST['title'];
echo $content = $_POST['content'];
echo $general = $_POST['general'];
// rest of your code

关于php - 无法将表单数据插入数据库(MySQLIi),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32932611/

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