gpt4 book ai didi

php - 使用 MySQL、PHP 的未定义索引

转载 作者:行者123 更新时间:2023-11-29 21:09:14 25 4
gpt4 key购买 nike

我搜索了类似的问题,但找不到解决我的问题的方法。我该如何调试 PHP 中的 MySQL 问题(比如这样)?找出这些错误的原因总是非常耗时的。查找/修复 JavaScript 中的错误要容易得多。

错误:

Notice: Undefined index: role in C:\xampp\htdocs\Noobs_Search\serverCode\findPlayers.php on line 48

Notice: Undefined index: gender in C:\xampp\htdocs\Noobs_Search\serverCode\findPlayers.php on line 49

它指的是这些行:

$role   = $row['role'];
$g = $row['gender'];

PHP 代码:

<?php

require "connect.php";

$searchBy = $_GET["field"];
$searchText = $_GET["text"];

$playerSQL = "SELECT *
FROM vw_playerTeamName";

if( isset($GET_["role"]) )
{
$role = $_GET["role"];
$playerSQL .= " AND role = $role";// .= is same as +=
}

if( isset($GET_["gender"]) )
{
$gender = $_GET["gender"];
$playerSQL .= " AND gender = $gender";// .= is same as +=
}

$result = $mysqli->query($playerSQL);

$pTable = "<div class='table'>
<div class='heading'>
<div class='cell'>ID</div>
<div class='cell'>First</div>
<div class='cell'>Last</div>
<div class='cell'>Email</div>
<div class='cell'>Team</div>
<div class='cell'>Gender</div>
<div class='cell'>Role</div>
</div>";

while($row = $result->fetch_array())
{
$ID = $row['ID'];
$first = $row['first'];
$last = $row['last'];
$email = $row['email'];
$team = $row['team_name'];
$role = $row['role'];
$g = $row['gender'];

$pTable .= "<div class='row'>
<div class='cell'>$ID</div>
<div class='cell'>$first</div>
<div class='cell'>$last</div>
<div class='cell'>$email</div>
<div class='cell'>$team</div>
<div class='cell'>$g</div>
<div class='cell'>$role</div>
</div>";
}

$pTable .= "</div>";

$mysqli->close();

echo $pTable;
?>

JavaScript:

$(document).ready(function()
{

$('#search').click(function()
{
var searchField = $("#searchType").val();
var searchText = $("#searchText").val();
var g = $("input[name=gender]:checked").val();
var r = $("input[name=role]:checked").val();

$.get("serverCode/findPlayers.php",
{
field:searchField,
text:searchText,
role:r,
gender:g
},function(response)
{
$("#placeholder").html(response);
});

});

});

HTML 代码:

<?php

$doc = "<!DOCTYPE html>
<html>
<head>
<title>Noobs Search Page</title>
<script src='clientCode/jquery-1.12.2.min.js'></script>
<script src='clientCode/search.js'></script>
<link href='css/tableStyle.css' rel='stylesheet'>
</head>
<body>

<select id='searchType'>
<option value='first'>First Name</option>
<option value='last'>Last Name</option>
<option value='email'>Email</option>
</select>

<input id='searchText' type='text' name='text'>

<p>Role<br>
<input type='radio' name='role' value='Pion'>Pion
<input type='radio' name='role' value='Captain'>Captain
<input type='radio' name='role' value='Assistant Captain'>Assistant Captain
</p>

<p>Gender<br>
<input type='radio' name='gender' value='M'>Male
<input type='radio' name='gender' value='F'>Female
</p>

<p>
<input type='button' name='search' id='search' value='search'>
<input type='button' name='clear' value='clear'>
</p>

<div id='placeholder'></div>

</body>
</html>";

echo $doc;
?>

最佳答案

您似乎拼写错误,它是 $_GET

 if( isset($_GET["role"]) )
{
$role = $_GET["role"];
$playerSQL .= " AND role = $role";// .= is same as +=
}

if( isset($_GET["gender"]) )
{
$gender = $_GET["gender"];
$playerSQL .= " AND gender = $gender";// .= is same as +=
}

关于php - 使用 MySQL、PHP 的未定义索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36538074/

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