[英]MYSQL Query Not Linking Correctly
我正在尝试创建一个包含151个项目的下拉菜单,这些菜单在被选中后会在新页面上显示该数据库行中的所有信息。
我正在使用一个处理页面来尝试提供此页面,但是,每次我从列表中选择一个项目时,我都会收到一条通知,通知我没有选择任何内容。 或者在我的情况下,“您需要请求神奇宝贝!”
我目前有一个带有标题的数据库:
id | pokemon_name | 高度 重量 GIF
我在主页上使用的代码是:
<?php
require_once ('../recaptcha/recaptchalib.php');
$publickey = "key";
$recaptcha_form = recaptcha_get_html($publickey);
?>
<?php
// connect to database
$db_hostname = 'localhost';
$db_database = "database"; //replace with your db name
$db_username = "username"; //replace with the db username that you created
$db_password = "password"; //replace with the db password that you created
$db_status = 'not initialised';
$db_server = mysqli_connect($db_hostname, $db_username, $db_password);
$db_status = "connected";
if (!$db_server) {
die("Unable to connect to MySQL: " . mysqli_connect_error());
$db_status = "not connected";
} else {
// If connected, get device names from database and write out DropDownMenu
mysqli_select_db($db_server, $db_database);
$query = "SELECT pokemon_name FROM pokemon_info";
$result = mysqli_query($db_server, $query);
if (!$result)
die("Query failed: " . mysqli_error($db_server));
while($row = mysqli_fetch_array($result)){
$str_options .= "<option value='" . $row[ 'ID'] . "'>";
$str_options .= $row['pokemon_name'] ;
$str_options .= "</option>";
}
mysqli_free_result($result);
}
?>
<!doctype html>
<html>
<head>
<title>Pokédexical Information!</title>
<link href="mainindex.css" rel="stylesheet" type="text/css" />
<h1>Testing Connection...</h1>
<p>
Database <?php
echo $db_database;
?> is...
<strong>
<?php
echo $db_status;
?>
</strong>
</p>
<!-----------THIS IS THE FORM----------->
<form id="pokemonform" action="file_process.php" method="post">
<select name="pokemonsubmit">
<?php echo $str_options; ?>
</select>
<input type="submit" id="submit" name="submit" value="Submit form" />
</form>
<!-----------COMMENTS----------->
</div>
<?php mysqli_close($db_server); ?>
并且我的处理页面的代码是;
<?php
function clean_string($db_server = null, $string){
// Remove whitespace
$string = trim($string);
$string = utf8_decode($string);
// Test whether a connection is open, or the it returns an error
if($db_server){
if (mysqli_real_escape_string($db_server, $string)) {
//Remove characters potentially harmful to the database
$string = mysqli_real_escape_string($db_server, $string);
}
}
// Strip dangerous escape characters (stripslahes is in the get_magic_quotes library)
if (get_magic_quotes_gpc()) {
$string = stripslashes($string);
}
// Return the processed data, with HTML tags transform into harmless escape characters
return htmlentities($string);
}
$db_hostname = 'localhost';
$db_database = 'database'; //'Your database name'
$db_username = 'username'; //'your username';
$db_password = 'password'; //'Your password';
$db_status = 'not initialised';
$db_server = mysqli_connect($db_hostname, $db_username, $db_password);
$db_status = "connected";
if (!$db_server){
die("Unable to connect to MySQL: " . mysqli_error());
$db_status = "not connected";
}
else {
// CODE TO QUERY DATABASE TO GO HERE
//Capture form data, if anything was submitted
if (isset($_POST['pokemon_submit'])) {
$pokemon_submit = clean_string($db_server, $_POST['pokemon_submit']);
// create the SQL query
$query = "SELECT * FROM pokemon_info WHERE pokemon_name='$pokemon_submit'";
// query the database
mysqli_select_db($db_server, $db_database);
$result = mysqli_query($db_server, $query);
if (!$result) die("Database access failed: " . mysqli_error($db_server));
// if there are any rows, print out the contents
if ($row = mysqli_fetch_array($result)) {
$output = '<p>' . $row['pokemon_name'] . ' (' . $row['gif'] . '), ' .
$row['gif'] . ', voted ' .
'<strong>' . $row['gif'] . '</strong> ' .
'to raising tuition fees</p>';
} else {
$output = 'Well, you must have invented a new Pokémon, cause it is not on this website!';
}
mysqli_free_result($result);
} else {
$output = 'You need to request a Pokemon!';
}
// Code to end the query
}
// YOUR CODE HERE BIT end
echo $output;
?>
错字,看:
<select name="pokemonsubmit">
但随后您正在检查:
if (isset($_POST['pokemon_submit'])) {
更改它们以使其匹配。
您的$ _POST ['pokemon_submit']未设置。 您输入的名称是pokemonsubmit,而您的PHP代码无法使用pokemon_submit找到POST。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.