PHP表单到数据库 - 没有错误,但没有结果

时间:2015-02-10 04:18:09

标签: php mysql database validation

我试图用PHP创建一个申请表。我已经设置了HTML标记,并创建了PHP处理器。不幸的是,无济于事。我没有收到任何错误,但数据库也没有更新。如果您能查看我的代码,那将非常棒。

HTML标记:

<form method="post" action="<?php echo base_url();?>index.php/new_post" id="application-form">
                        <table class="application">
                            <tbody>
                                <tr>
                                    <td class="left">Question 1</td>
                                    <td><input type="text" size="30" name="question1" id="question1"/></td>
                                </tr>
                                <tr>
                                    <td class="left">Question 2</td>
                                    <td><input type="text" size="30" name="question2" id="question2"/></td>
                                </tr>
                                <tr>
                                    <td class="left">Question 3</td>
                                    <td><input type="text" size="30" name="question3" id="question3"/></td>
                                </tr>
                                <tr>
                                    <td class="left">Question 4</td>
                                    <td><input type="text" size="30" name="question4" id="question4"/></td>
                                </tr>
                                <tr>
                                    <td class="left">Question 5</td>
                                    <td><textarea rows="4" cols="30" name="question5" id="question5"></textarea></td>
                                </tr>
                                <tr>
                                    <td class="left">Question 6</td>
                                    <td><textarea rows="4" cols="30" name="question6" id="question7"></textarea></td>
                                </tr>
                                <tr>
                                    <td class="left">Question 7</td>
                                    <td><input type="text" size="30" name="question7" id="question7"/></td>
                                </tr>
                                <tr>
                                    <td class="left">Question 8</td>
                                    <td><textarea rows="4" cols="30" name="question8" id="question8"></textarea></td>
                                </tr>
                                <tr>
                                    <td class="left">Question 9</td>
                                    <td><textarea rows="4" cols="30" name="question9" id="question9"></textarea></td>
                                </tr>
                                <tr>
                                    <td class="left">Question 10</td>
                                    <td><textarea rows="4" cols="30" name="question10" id="question10"></textarea></td>
                                </tr>
                                <tr>
                                    <td class="left">Question 11</td>
                                    <td><textarea rows="4" cols="30" name="question11" id="question11"></textarea></td>
                                </tr>
                                <tr>
                                    <td class="left">Question 12</td>
                                    <td><input type="text" size="30" name="question12" id="question12"/></td>
                                </tr>
                                <tr>
                                    <td class="left">Question 13</td>
                                    <td><textarea rows="4" cols="30" name="question13" id="question13"></textarea></td>
                                </tr>
                                <tr>
                                    <td class="left">Question 14</td>
                                    <td><textarea rows="4" cols="30" name="question14" id="question14"></textarea></td>
                                </tr>
                                <tr>
                                    <td class="left">Question 15</td>
                                    <td><input type="text" size="30" name="question15" id="question15"/></td>
                                </tr>                           
                            </tbody>
                        </table>
                        <button type="submit" class="btn btn-block btn-danger martop15">Send Application</button>
                    </form>

PHP处理器:

<?php 
    if(!empty($_POST)){
        $question1 = $_POST['question1']; 
        $question2 = $_POST['question2']; 
        $question3 = $_POST['question3']; 
        $question4 = $_POST['question4']; 
        $question5 = $_POST['question5']; 
        $question6 = $_POST['question6']; 
        $question7 = $_POST['question7']; 
        $question8 = $_POST['question8']; 
        $question9 = $_POST['question9']; 
        $question10 = $_POST['question10']; 
        $question11 = $_POST['question11']; 
        $question12 = $_POST['question12']; 
        $question13 = $_POST['question13']; 
        $question14 = $_POST['question14']; 
        $question15 = $_POST['question15']; 

    }

        $mysqli = new mysqli('localhost', 'root', '', 'database');

        $mysqli->query("INSERT INTO `applications`(`question1`, `question2`, `question3`, `question4`, `question5`, `question6`, `quesion7`, `question8`, `question9`, `question10`, `question11`, `question12`, `question13`, `question14`, `question15`) VALUES (`$question1`, `$question2`, `$question3`, `$question4`, `$question5`, `$question6`, `$question7`, `$question8`, `$question9`, `$question10`, `$question11`, `$question12`, `$question13`, `$question14`, `$question15`)");
?>

提前致谢。

3 个答案:

答案 0 :(得分:0)

请使用

   $queryResult =  $mysqli->query("INSERT INTO applications(question1, question2, question3, question4, question5, question6,quesion7, question8, question9, question10, question11, question12, question13, question14,question15) VALUES ('$question1', '$question2', '$question3', '$question4', '$question5', '$question6', '$question7', '$question8', '$question9', '$question10','$question11', '$question12', '$question13','$question14', '$question15')");

``

中删除insert into TableName (here,here)

用于错误处理

if ($queryResult === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: ";
}

答案 1 :(得分:0)

如果您的语句失败,您可以通过执行以下操作输出错误消息:

if (!$mysqli->query("INSERT INTO `applications`(`question1`, `question2`, `question3`, `question4`, `question5`, `question6`, `quesion7`, `question8`, `question9`, `question10`, `question11`, `question12`, `question13`, `question14`, `question15`) VALUES (`$question1`, `$question2`, `$question3`, `$question4`, `$question5`, `$question6`, `$question7`, `$question8`, `$question9`, `$question10`, `$question11`, `$question12`, `$question13`, `$question14`, `$question15`)")) {
  printf("Error message: %s\n", $mysqli->error);
}

对于INSERT语句,如果语句成功,$mysqli->query语句将返回TRUE,否则返回FALSE。上面的内容可以说明出了什么问题。


修改

您使用了不正确的identifiers作为您的价值观,而且应该是常规单引号。

将其更改为:

if (!$mysqli->query("INSERT INTO `applications` (`question1`, `question2`, `question3`, `question4`, `question5`, `question6`, `quesion7`, `question8`, `question9`, `question10`, `question11`, `question12`, `question13`, `question14`, `question15`) VALUES ('$question1', '$question2', '$question3', '$question4', '$question5', '$question6', '$question7', '$question8', '$question9', '$question10', '$question11', '$question12', '$question13', '$question14', '$question15')")) {
  printf("Error message: %s\n", $mysqli->error);
}

<强> N.B:

另外,你可能会误解你的问题&#34;问题7&#34;列,最有可能读作&#34;问题7&#34;。

如果是这种情况应该修改,但在上面的代码中没有。


重要提示:

答案 2 :(得分:-1)

在您的代码中,您添加了数据库字段名称的引号,我认为这是问题

$mysqli->query("INSERT INTO applications (question1, question2, question3, question4, question5, question6, quesion7, question8, question9, question10,question11, question12, question13, question14, question15) VALUES (`$question1`, `$question2`, `$question3`, `$question4`, `$question5`, `$question6`, `$question7`, `$question8`, `$question9`, `$question10`, `$question11`, `$question12`, `$question13`, `$question14`, `$question15`)");

试试这个。 我怀疑在表单操作中你通过“/”传递段是分开的是在核心php中有这种支持实际上我们只在一些框架中传递查询字符串我们使用“/”