2012-08-01 82 views
0

我试图让一个下拉框从数据库中的数据填充,但它似乎并没有工作。它传递一个错误,说数据库中没有数据要读取,但是有数据。以下是我用来使其工作的脚本。PHP下拉框没有填充

<?php 
@ini_set('display_errors', 'on'); 
echo "<h1>Register</h1>"; 
if ($_SERVER['REQUEST_METHOD'] == 'POST'){ 

$errors = array(); 
if (empty($_POST['firstname'])){ 
$errors[] = 'Your forgot to enter your first name.'; 
}else{ 
    $firstname = trim($_POST['firstname']); 
    } 
if (empty($_POST['lastname'])){ 
$errors[] = 'Your forgot to enter your last name.'; 
}else{ 
    $lastname = trim($_POST['lastname']); 
} 
if (empty($_POST['username'])){ 
$errors[] = 'Your forgot to enter your username.'; 
}else{ 
    $username = trim($_POST['username']); 
} 
if (!empty($_POST['password1'])) { 
if ($_POST['password1'] != $_POST ['password2']) { 
    $errors[] = 'Your password did not match the confirmed password!'; 
}else{ 
    $password = trim($_POST['password1']); 
    } 
} else { 
    $errors[] = 'You forgot to enter your password!'; 
} 
if (empty($_POST['birthdate'])){ 
$errors[] = 'Your forgot to enter your birthdate.'; 
}else{ 
    $birthdate = trim($_POST['birthdate']); 
} 
if (empty($_POST['gamespyid'])){ 
$errors[] = 'Your forgot to enter your gamespy id.'; 
}else{ 
    $gamespyid = trim($_POST['gamespyid']); 
} 
    if (empty($errors)) { 
    if (is_file('admin/mysqli_connect.php')) { echo '<p>The connection file is there.</p>';require('admin/mysqli_connect.php'); } 

else { 
    echo '<p>The connection file is not there</p>'; 
    } 
    $q="INSERT INTO Users (firstname, lastname, username, password1, birthdate, gamespyid, base) VALUES ('$firstname', '$lastname', '$username', md5('$password1'), '$birthdate', '$gamespyid', '$base')"; 
$r = mysql_query($dbc, $q); 
if ($r){ 
echo'<p>You are now registered</p>'; 
}else{ 
    echo'<p>You have not been registered</p>'; 
              } 
    } else { 
    echo 'Error<br> <p>The following errors have occured:<br/>'; 
    foreach ($error as $msg) { 
     echo " - $msg<br/>\n"; 
    } 
    echo '</p><p>Please try again.</p><p><br/></p>'; 
    } //if no errors 

}  //submit 

?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2//EN"> 
<html> 
<head> 
    <title></title> 
</head> 

<body> 
    <form action="http://www.virtual-aviation.org/gatewayaviation/index.php?p=register" method='POST'> 
    <table summary="REgform"> 
     <tr> 
     <td>First Name:</td> 

     <td><input type='text' name='firstname' value='<?php echo $firstname; ?>'></td> 
     </tr> 

     <tr> 
     <td>Last Name:</td> 

     <td><input type='text' name='lastname'value='<?php echo $lastname; ?>'></td> 
     </tr> 

     <tr> 
     <td>Username:</td> 

     <td><input type='text' name='username'value='<?php echo $username; ?>'></td> 
     </tr> 

     <tr> 
     <td>Password:</td> 

     <td><input type='password' name='password1'></td> 
     </tr> 

     <tr> 
     <td>Repeat Password:</td> 

     <td><input type='password' name='password2'></td> 
     </tr> 

     <tr> 
     <td>Birthdate:</td> 

     <td><input type='text ' name='birthdate'value='<?php echo $birthdate; ?>'></td> 
     </tr> 

     <tr> 
     <td>Gamespy Id:</td> 

     <td><input type='text' name='gamespyid'value='<?php echo $gamespyid; ?>'></td> 
     </tr> 
     <td>Base:</td> 
     <tr> 
     <td><select name="base" size="1"> 
      <option> 
      Select One 
      </option> 
     <?php 
     $qf = "SELECT airport_id, CONCAT_WS(' ', airport_name, airport_code) FROM airports ORDER BY airport_code ASC"; 
     $rf = mysqli_query ($dbc, $qf); 

     if (mysqli_num_rows($rf) > 0) { 
     while ($row = mysql_fetch_array ($rf, MYSQLI_NUM)) { 
     echo "<option value=\"$row[0]\""; 
     if (isset($_POST['existing']) && ($_POST['existing'] == $row[0])) echo 'selected="selected"'; echo ">$row[1]</option>\n"; 
     } 
    } else { 
    echo '<option>Please a new airport first.</option>'; 
    } 
    //mysqli_close($dbc); 

     ?> 
     </select></td> 
     </tr> 
    </table> 
    <input type='submit' name='submit' value='Register'/> 
    </form> 
    <?php 
    echo '<p>debugger<p>' . $rf ; 
    ?> 
</body> 
</html> 

以下是我查看源代码时遇到的一些错误;错误出现在选项框内容应该在的地方。

警告:mysqli_query()预计参数1是mysqli的,在/home5/virtua15/public_html/gatewayaviation/pages/register.inc.php空给定线

警告:mysqli_num_rows()预计参数1被mysqli_result,在/home5/virtua15/public_html/gatewayaviation/pages/register.inc.php空给定线

Please a new airport first. 
+1

你在做什么是安全的。使用准备好的查询来避免SQL注入攻击! – Brad 2012-08-01 18:04:57

+1

为什么混合传统的mysql_ *和更新的mysqli_ *函数? – 2012-08-01 18:06:52

+0

错误信息的哪些部分不清楚? – Leigh 2012-08-01 18:07:15

回答

2

可以使用mysql_ *或mysqli_ *函数,但不能同时使用两者。我宁愿选择后者,了解准备的语句:http://php.net/manual/en/mysqli.quickstart.prepared-statements.php

的问题是你的$dbc变量是最有可能的一个MySQL资源或空,不是mysqli的资源,所以当你将它传递给mysqli_ *功能它失败。有办法检查并确保你有一个有效的资源:

$mysqli = new mysqli("example.com", "user", "password", "database"); 
if ($mysqli->connect_errno) { 
    echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error; 
} 

解决你的问题,你需要调整你的代码中使用的mysqli并确保$dbc是一个有效的mysqli资源。

+0

它说'null给'可能未能连接到意大利面以上的某处,并且它从未被检查过。或者根据代码,从来没有尝试过连接。 – Leigh 2012-08-01 18:10:47

+0

我想知道为什么这行不会出错? '$ R =的mysql_query($ DBC,$ Q);' – 2012-08-01 18:11:17

+0

那么这是一个向后一开始,应该是'$ q'第一和'$ dbc'第二。我不想去想它为什么不起作用,整个事情都是可憎的。 – Leigh 2012-08-01 18:13:15

0

在你的例子的第123行,你有$rf = mysqli_query ($dbc, $qf);参数#1是$dbc,MySQL连接对象。该错误消息指出连接对象为null,这是真的 - 它没有在脚本中的任何位置声明。

0

那么,

警告:mysqli_query()预计参数1是mysqli的,在/home5/virtua15/public_html/gatewayaviation/pages/register.inc.php空给定线182上

是因为你不能使用mysqli_query($ dbc,$ qf);当$ dbc不是从mysql_connect或mysql_init返回时,因为它需要链接作为第一个参数,并返回它需要的链接。

这也是为什么你会得到第二个错误。 mysqli没有将任何东西返回给$ rf。

我会建议重构整个页面。从给变量赋予有意义的名字开始,转义你的SQL字符串参数以避免注入。

清除这些警告并再次拍摄。