繁体   English   中英

PHP表单提交给我白页,没有成功消息

[英]PHP form submit gives me a white page with no success message

我正在处理将数据添加到mysql表的php脚本,我想要4个字段,我想我得到的大部分代码都是正确的,因为我从php书中复制了代码,然后进行了一些更改反映我在寻找特定结构的内容,但是当我测试提交功能时,它只是给我一个白色的屏幕,而不是成功/错误消息,这让我很困惑从何去何从。 下面是我的代码的副本。

<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');
 ?>

即使不使用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个占位符(?, ?, ?, ?, ?) ,因此删除其中一个占位符,它将立即起作用。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM