gpt4 book ai didi

php - MYSQL 查询未正确链接

转载 作者:行者123 更新时间:2023-11-29 23:14:33 24 4
gpt4 key购买 nike

我正在尝试创建一个包含 151 个项目的下拉菜单,选择该菜单后,会在新页面上显示数据库该行中的所有信息。

我正在使用处理页面来尝试传递此信息,但是,每次我从列表中选择一个项目时,我都会收到通知,表明我尚未选择任何内容。或者就我而言,“你需要请求一个神奇宝贝!”

我当前有一个包含标题的数据库:

id |神奇宝贝名称 |高度|重量 |动图

我的主页使用的代码是;

<?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'])) {

更改它们以匹配。

关于php - MYSQL 查询未正确链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27912057/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com