PHP下拉框未填充
我试图从数据库中的数据中获得一个下拉框来填充,但它似乎不起作用。它传递了一个错误,表示数据库中没有数据可供它读取,但有。下面是我用来让它工作的脚本PHP下拉框未填充,php,drop-down-menu,Php,Drop Down Menu,我试图从数据库中的数据中获得一个下拉框来填充,但它似乎不起作用。它传递了一个错误,表示数据库中没有数据可供它读取,但有。下面是我用来让它工作的脚本 <?php @ini_set('display_errors', 'on'); echo "<h1>Register</h1>"; if ($_SERVER['REQUEST_METHOD'] == 'POST'){ $errors = array(); if (empty($_POST['firstname
<?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>
可以使用mysql_*或mysqli_*函数,但不能同时使用两者。我希望您选择后者并了解准备好的声明: 问题在于
$dbc
变量很可能是mysql资源或null,而不是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资源。在示例的第123行,您有$rf=mysqli\u查询($dbc,$qf)
参数#1是MySQL连接对象$dbc
。错误消息指出连接对象为null
,这是真的-它没有在脚本中的任何位置声明。好
警告:mysqli_query()要求参数1为mysqli,在第182行的/home5/virtual15/public_html/gatewayaviation/pages/register.inc.php中给出空值
是因为你不能使用mysqli_查询($dbc,$qf);$dbc不是来自mysql\u connect或mysql\u init的返回,因为它需要一个链接作为第一个参数,而这些参数返回它需要的链接
这也是您出现第二个错误的原因。mysqli没有向$rf返回任何内容
我建议重构整个页面。从给变量有意义的名称开始,转义SQL字符串参数以帮助避免注入
清除这些警告,再试一次。你所做的绝不是安全的。使用准备好的查询来避免SQL注入攻击!为什么要混合使用传统的mysql_*和更新的mysqli_*函数?错误消息的哪一部分不清楚?另外,请删除无关代码(它被解释为噪音,阻止社区帮助您)。@Leigh OOPS!我不知道我在做什么。上面说,
null-given
可能无法连接到意大利面上的某个地方,而且从未检查过。或者根据代码,从来没有尝试过连接。我想知道为什么这一行没有抛出错误<代码>$r=mysql\u查询($dbc,$q)代码>一开始是向后的,它应该是$q
第一个和$dbc
第二个。我不想去想为什么它不起作用,整个事情都是令人讨厌的。@Leigh Yeah,让你想知道为什么人们在做一碗意大利面之前不查阅文档,不花时间尝试学习。
$mysqli = new mysqli("example.com", "user", "password", "database");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}