2012-07-19 56 views
0

我工作的php脚本添加数据到mysql表中有4个字段,我想要的所有这些,我想我得到了主要的代码正确因为我从PHP书中复制了代码,然后做了一些更改以反映我正在寻找的特定srcipt,但是当我去测试提交函数时,它只是给我一个白色屏幕而不是成功/错误消息,im有点难以置信从哪里出发。下面是我的代码的副本。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> 

这里是我得到当我emabled错误 警告的错误:mysqli_prepare()预计参数1是mysqli的,在/home5/virtua15/public_html/gatewayaviation/add_apt.php空给出线29

警告:mysqli_stmt_bind_param()预计参数1被mysqli_stmt,空在/home5/virtua15/public_html/gatewayaviation/add_apt.php给定在线路30上

警告:mysqli_stmt_execute()预计参数1被mysqli_stmt,空给定在线31上的/home5/virtua15/public_html/gatewayaviation/add_apt.php

警告:mysqli_stmt_affected_rows()预计参数1被mysqli_stmt,在/home5/virtua15/public_html/gatewayaviation/add_apt.php空给定线路33

警告:mysqli_stmt_close()预计参数1被mysqli_stmt,在给定的/home5/virtua15/public_html/gatewayaviation/add_apt.php第39行上

警告无效:mysqli_close()预计参数1是mysqli的,空在/home5/virtua15/public_html/gatewayaviation/add_apt.php上给出第40行

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

顶级提示,缩进你的代码......在这种格式下阅读几乎是不可能的。 – 2012-07-19 08:27:06

+2

经常当它给你一个白页这是一个PHP错误,停止正常的页面流:add @ini_set('display_errors','on');在页面顶部看看可能是什么问题。 – kawashita86 2012-07-19 08:27:21

+0

在插入语句中,列计数与值计数不匹配。 – redbirdo 2012-07-19 08:44:58

回答

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个占位符,因此将其删除其中一个占位符,现在它将工作。

编辑:为了回应您的问题和此答案中的意见,还请检查连接文件是否正确包含在您的主脚本中。

+0

我试图删除一个占位符,我什么也没做。我发布了当我启用显示错误代码时得到的错误。如果我在我的sql数据库的机场表中有一个Id列,如果没有考虑到我的脚本,那么它也会影响吗? – user1537210 2012-07-19 10:04:46

+0

@ user1537210:首先,您没有id字段并不重要。我没有测试它,并让它正常工作。至于这些错误,他们指出'$ dbc'变量不是一个有效的mysqli参数,所以我认为mysqli_connect在你的'mysqli_connect.php'文件中工作不正常。你能发表那些内容吗? – hellsgate 2012-07-19 10:22:36

+0

好的,我添加了上面的mysqli_connect.php – user1537210 2012-07-19 10:39:32

相关问题