我正在尝试用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`)");
?>
提前谢谢。
请使用
$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: ";
}
如果您的语句失败,您可以通过以下操作输出错误消息:
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
。以上内容应该会让你知道出了什么问题。
编辑:
您在VALUES中使用了不正确的标识符,它们是记号,应该是常规的单引号。
更改为:
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);
}
注意:
此外,你可能把你的"问题7"栏拼写错误,很可能读作"问题7。
如果是这样的话,应该对其进行修改,但在上面的代码中没有进行修改。
重要提示:
- 您当前的代码对SQL注入是开放的。将
mysqli
与准备好的语句一起使用,或将PDO与准备好语句一起使用
在您的代码中,您为数据库字段名添加了引号,我认为这是的问题
$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`)");
试试这个。我怀疑在表单操作u中,通过"/"seprated传递分段是否在core php中有这种支持——实际上,我们只在一些使用"/"的框架中传递查询字符串