MYSQL查询没有正确链接

时间:2015-01-12 22:47:24

标签: php mysql database

我正在尝试创建一个包含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;



?>

2 个答案:

答案 0 :(得分:0)

错字,看:

<select name="pokemonsubmit">

但是你正在检查:

if (isset($_POST['pokemon_submit'])) {

将它们更改为匹配。

答案 1 :(得分:0)

您的$ _POST ['pokemon_submit']未设置。您的输入名称为pokemonsubmit,您的PHP代码无法通过pokemon_submit找到POST。