1

下面的代码从表单中获取信息并将其发送到 mysql。它成功地做到了(对于类别、内容、日期和用户 ID),但最近我在数据库中添加了一个名为“seclevel”的新列,该列也需要填写。我没有看到添加 seclevel 破坏代码的逻辑原因,并且我在日志中没有收到任何错误。它只是一个用户从 1-9 中选择的整数,所以除非我错误地使用 $_POST['seclevel'] 我被难住了。有任何想法吗?

send_post.php

<?php
include 'db_connect.php';
include 'functions.php';
sec_session_start();

$userId = $_SESSION['user_id'];
if(login_check($mysqli) == true) {

//Connecting to sql db.
$connect=mysqli_connect("localhost","mylogin","mypass","mydb");

header("Location: http://somekindasite.com/index_3.php");

if (mysqli_connect_errno()) { echo "Fail"; } else { echo "Success"; }

//Sending form data to sql db.
$stmt = $mysqli -> prepare('INSERT INTO opwire (category, contents, date, userid, seclevel) 
                            VALUES (?,?,NOW(),?,?)');
$stmt -> bind_param('ssi', $_POST['category'], $_POST['contents'], $userId, $_POST['seclevel']);
$stmt -> execute();
$stmt -> close();

} else {
   echo 'Access denied. <br/>';
}

?>

这是提交给 send_post.php 的相关表单

<html>
<div style="width:  330px;  height:  130px;  overflow:  auto;">
<form STYLE="color: #f4d468;" action="send_post.php" method="post">

    Category: <select STYLE="color: #919191; font-family: Veranda; font-weight: bold; font-size: 10px; background-color: #000000;" name="category">
<option value="1">1</option>
<option value="1">2</option>
<option value="1">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option STYLE="color: #c31717;" value="8">8</option>
<option value="Other">Other</option>
</select>

    Seclevel: <select STYLE="color: #919191; font-family: Veranda; font-weight: bold; font-size: 10px; background-color: #000000;" name="seclevel">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
</select> <br>

    <textarea overflow: scroll; rows="4" cols="60" STYLE="color: #919191; font-family: Veranda; font-weight: bold; font-size: 10px; background-color: #000000; width:300px; height:80px; margin:0; padding:0;" name="contents"></textarea><br>
    <input type="submit" STYLE="color: #919191; font-family: Veranda; font-weight: bold; font-size: 10px; background-color: #000000;" value="Create Log">
</form>
</div>
</html>
4

1 回答 1

1

MySQLi 的参数绑定有两部分,而你错过了其中之一。您需要将s字符串或i整数(或d双精度、bblob)的类型添加到bind_param().

// Looks like you're adding another integer...
$stmt -> bind_param('ssii', $_POST['category'], $_POST['contents'], $userId, $_POST['seclevel']);
//---------------------^^^

由于失败mysqli_stmt::bind_param()返回FALSE,你应该进行一些错误检查。

if ($stmt->bind_param('ssii', $_POST['category'], $_POST['contents'], $userId, $_POST['seclevel'])) {
  $stmt->execute();
}
else {
  echo $stmt->error;
}

我还要注意,我预计bind_param()会发出致命错误。在开发代码时,始终建议启动错误报告并在屏幕上显示错误。

error_reporting(E_ALL);
ini_set('display_errors', 1);
于 2013-11-13T21:26:43.547 回答