0

我正在编写一个将数据添加到 mysql 表的 php 脚本,有 4 个字段我都需要,我想我得到了大部分正确的代码,因为我从 php 书中复制了代码然后做了一些更改反映我在寻找我的特定 srcipt 的内容,但是当我去测试提交功能时,它只会给我一个白屏而不是成功/错误消息,我有点不知道从这里去哪里。下面是我的代码的副本。

<html>
<head>
</head>
<body>
<?php

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
//input validation all required
if (!empty($_POST['airport_name'])) {
$an = trim($_POST['airport_name']);
if (!empty($_POST['airport_code'])){
$ac = trim($_POST['airport_code']);
if (!empty($_POST['airport_lat'])){
$alat = trim($_POST['airport_lat']);
if (!empty($_POST['airport_long'])){
$along = trim($_POST['airport_long']);

//if (!empty($_POST['airport_name'])){

//$an  = trip($_POST['airport_name']);


require('/../mysqli_connect.php');
$q = 'INSERT INTO airports (airport_name, airport_code, airport_lat, airport_long) VALUES (?, ?, ?, ?)';
$stmt = mysqli_prepare($dbc, $q);
mysqli_stmt_bind_param($stmt, 'ssss', $an, $ac, $alat, $along);
mysqli_stmt_execute($stmt);

if (mysqli_stmt_affected_rows($stmt) == 1) {
  echo '<p>The airport has been added!</p>';
  $_POST = array();
} else {
  $error = 'The new airport could not be added to the database!';
}
mysqli_stmt_close($stmt);
mysqli_close($dbc);




} //if airport lat
   else{
     $error = 'Please enter airport lat';
   }
}   //if airport lat
else{
  $error = 'Please enter airport latitude';
}
}  //if airport code
else {
  $error = 'Please enter an airport code';
}
}  //if airport name
else {
  $error = 'Please enter an airport name';
}
} // end of submission if
if (isset($error)) {
  echo '<h1>Error!</h1>
  <p stlye="font-weight: bold; color: #COO">'. $error . 'Please try again</p>';
}

?>
<h1>Add Airport</h1>
<form action="add_apt.php" method="post">
<fieldset><legend>Fill out the for to and and airpot:</legend>
<p><b>Airport Name:</b><input type="text" name="airport_name" size="10" value="<?php if (isset($_POST['airport_name'])) echo $_POST['airport_name']; ?>"/></p>
<p><b>Airport Code:</b><input type="text" name="airport_code" size="4" maxlegnth="4" value="<?php if (isset($_POST['airport_code'])) echo $_POST['airport_code']; ?>"/></p>
<p><b>Airport Lat:</b><input type="text" name="airport_lat" size="10" maxlegnth="40" value="<?php if (isset($_POST['airport_lat'])) echo $_POST['airport_lat']; ?>"/></p>
<p><b>Airport Long:</b><input type="text" name="airport_long" size="10" maxlegnth="40" value="<?php if (isset($_POST['airport_long'])) echo $_POST['airport_Long']; ?>"/></p>
</fieldset>
<div align="center"><input type="submit" name="submit" Value="Add Airport"/></div>
</form>
</body>
</html>

以下是我启用错误时得到的错误警告:mysqli_prepare() 期望参数 1 为 mysqli,在第 29 行的 /home5/virtua15/public_html/gatewayaviation/add_apt.php 中给出 null

警告:mysqli_stmt_bind_param() 期望参数 1 为 mysqli_stmt,在第 30 行的 /home5/virtua15/public_html/gatewayaviation/add_apt.php 中给出 null

警告:mysqli_stmt_execute() 期望参数 1 为 mysqli_stmt,在第 31 行的 /home5/virtua15/public_html/gatewayaviation/add_apt.php 中给出 null

警告:mysqli_stmt_affected_rows() 期望参数 1 为 mysqli_stmt,在第 33 行的 /home5/virtua15/public_html/gatewayaviation/add_apt.php 中给出 null

警告:mysqli_stmt_close() 期望参数 1 为 mysqli_stmt,在第 39 行的 /home5/virtua15/public_html/gatewayaviation/add_apt.php 中给出 null

警告:mysqli_close() 期望参数 1 为 mysqli,在第 40 行的 /home5/virtua15/public_html/gatewayaviation/add_apt.php 中给出 null

Mysqli_connect.php 脚本

<?php

DEFINE ('DB_USER', '******_******');
DEFINE ('DB_PASSWORD', '*******');
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', '******_gateway');

$dbc = @mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) OR die ('Could not connect to MySQL: ' . mysqli_connect_error() );

mysqli_set_charset($dbc, 'utf8');
 ?>
4

1 回答 1

1

有一种简单的方法可以找出错误所在,即使不使用 display_errors。在您正在构建和执行查询的脚本部分中,您应该将其更改为以下内容:

if ($stmt = mysqli_prepare($dbc, $q)) {
    mysqli_stmt_bind_param($stmt, 'ssss', $an, $ac, $alat, $along);
    mysqli_stmt_execute($stmt);

    if (mysqli_stmt_affected_rows($stmt) == 1) {
        echo '<p>The airport has been added!</p>';
        $_POST = array();
    } else {
        $error = 'The new airport could not be added to the database!';
    }
    mysqli_stmt_close($stmt);
    mysqli_close($dbc);
}
else {
    echo '<p>'.mysqli_error($dbc).'</p>';
}

通过这样做,您将收到查询中发生的任何 MySQL 错误。在这种情况下,生成的错误是:

Column count doesn't match value count at row 1

仔细查看代码,出现此错误是因为您尝试添加 4 个字段(airport_name, airport_code, airport_lat, airport_long),但您的 SQL 语句中有 5 个占位符(?, ?, ?, ?, ?),因此删除其中一个占位符,它现在可以工作了。

编辑:针对您的问题和此答案中的评论,还要检查连接文件是否正确包含在您的主脚本中。

于 2012-07-19T08:40:40.197 回答