编辑:我不知道该说什么。这些评论是我现在发布的问题。我不记得显示错误的事实,这正是对我的帮助。我几乎只是尝试了一些显然不能工作的东西,因为错误地放置了变量等,并显示错误代码让我知道哪些变量是错误的。感谢您的帮助,现在解决了这个问题!无法插入表单数据到数据库(MySQLIi)
这么长的故事,标题。我不知道为什么这段代码拒绝将插入的数据输入到数据库中,并且我尝试了一些没有得到更好结果的东西。
连接:
<?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>";
}
你收到任何错误输出?如果没有,尝试用'error_reporting(E_ALL)'激活php文件顶部的错误报告,并再次测试。你收到什么? –
你有*任何*输出吗?启用'error_reporting',检查['mysqli :: $ error'](http://stackoverflow.com/questions/22662488/how-to-get-mysqli-error-in-different-environments)。删除所有用于测试的isset。 – mario
@FabianPicone未定义的索引:用户名和未定义:一般是我从中得到的。试图现在摆弄这些问题.. – Xariez