PHP表单提交给我一个没有成功消息的白色页面
我正在编写一个php脚本,将数据添加到mysql表中有4个字段,我希望所有字段都是必需的,我想我的代码主要是正确的,我从一本php书中复制了代码,然后做了一些更改,以反映我对特定srcipt的要求,但是当我测试提交函数时,它只会给我一个白色屏幕,而不是一条成功/错误消息,我有点不知所措。下面是我的代码副本PHP表单提交给我一个没有成功消息的白色页面,php,forms,mysqli,Php,Forms,Mysqli,我正在编写一个php脚本,将数据添加到mysql表中有4个字段,我希望所有字段都是必需的,我想我的代码主要是正确的,我从一本php书中复制了代码,然后做了一些更改,以反映我对特定srcipt的要求,但是当我测试提交函数时,它只会给我一个白色屏幕,而不是一条成功/错误消息,我有点不知所措。下面是我的代码副本 <html> <head> </head> <body> <?php if ($_SERVER['REQUEST_METHOD'] ==
<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>
加机场
填写to和airpot的表格:
机场名称:有一种简单的方法可以找到错误,即使不使用显示错误。在脚本中构建和执行查询的部分中,应将其更改为以下内容:
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>';
}
仔细查看代码,发生此错误的原因是您试图添加4个字段(机场名称、机场代码、机场纬度、机场长度)
,但SQL语句中有5个占位符(?,?,?,?,?)
,因此请删除其中一个占位符,它现在就可以工作了
编辑:在回答问题和答案中的注释时,还要检查连接文件是否正确包含在主脚本中。顶部提示,缩进代码。。。以这种格式阅读几乎是不可能的。通常,当它给出一个白色页面时,是php错误停止了页面的正常流动:add@ini_set('display_errors','on');在页面顶部查看可能出现的问题。在insert语句中,列计数与值计数不匹配。@user1537210:好的,由于函数调用前面的@
符号,您目前不会看到mysqli\u connect错误。这就排除了任何错误。删除它(以便它读取mysqli\u connect(DBHOST…etc)
),然后将显示连接上发生的任何错误。如果有,请发布,如果没有,请告诉我们。@user1537210:在这种情况下,您需要检查主脚本中是否正确包含了该文件。替换行require('/../mysqli_connect.php')使用if(is_file('/../mysqli_connect.php'){echo'连接文件在那里。';require('/../mysqli_connect.php');}否则{echo'连接文件不在那里';}
并发布结果我尝试删除一个占位符,但没有做任何事情。我发布了启用“显示错误”代码时收到的错误。如果我在sql数据库的airports表中有一个Id列,这是否也会影响我的脚本?如果没有考虑到这一点,会不会弄乱我的脚本?@user1537210:首先,没有Id字段并不重要。我测试了它没有一个,并得到了它的工作正常。至于错误,它们表明$dbc
变量不是有效的mysqli参数,因此我认为在mysqli\u connect.php
文件中的mysqli\u connect无法正常工作。你能把它的内容贴出来吗?
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>';
}
Column count doesn't match value count at row 1