2015-10-04 108 views
1

编辑:我不知道该说什么。这些评论是我现在发布的问题。我不记得显示错误的事实,这正是对我的帮助。我几乎只是尝试了一些显然不能工作的东西,因为错误地放置了变量等,并显示错误代码让我知道哪些变量是错误的。感谢您的帮助,现在解决了这个问题!无法插入表单数据到数据库(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>"; 

      } 
+0

你收到任何错误输出?如果没有,尝试用'error_reporting(E_ALL)'激活php文件顶部的错误报告,并再次测试。你收到什么? –

+0

你有*任何*输出吗?启用'error_reporting',检查['mysqli :: $ error'](http://stackoverflow.com/questions/22662488/how-to-get-mysqli-error-in-different-environments)。删除所有用于测试的isset。 – mario

+0

@FabianPicone未定义的索引:用户名和未定义:一般是我从中得到的。试图现在摆弄这些问题.. – Xariez

回答

0

尝试改变

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

有了这个:

$username = isset($_POST['username']) ? $_POST['username'] : ''; 
$title = isset($_POST['title']) ? $_POST['title'] : ''; 
$content = isset($_POST['content']) ? $_POST['content'] : ''; 
$general = isset($_POST['general']) ? $_POST['general'] : ''; 
1

未定义指数:用户名和未定义:一般是我从那个得到。

由于您没有从表单中的任何位置获取$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