我正在处理一个php脚本,它将数据添加到mysql表中有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');
?>
答案 0 :(得分: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个占位符,因此请删除其中一个占位符并将其删除现在会工作。
编辑:在回答您的问题和答案中的评论时,还要检查连接文件是否正确地包含在主脚本中。