gpt4 book ai didi

javascript - 表单未发布变量/mySQL 查询未正确搜索

转载 作者:行者123 更新时间:2023-11-29 12:31:19 25 4
gpt4 key购买 nike

我正在尝试为足球网站的管理部分创建一个球员编辑系统。流程如下:

教练登录“coaches.php”后,他们可以通过下拉菜单选择他们想要查看的教练类(class),然后填充“球员”下拉菜单(通过下面的 js 完成)

coaches.php 表单

            <form id="form1" name="form1" method="post" action="coachplayer.php?id=' .$id. '">
<label>Activity :</label>
<select name="activity" class="activity">
<option selected="selected">--Select Activity Group--</option>
<?php
include('dbconnect.php');
$sql=mysql_query("select activity from coaches where username='$coach'");
while($row=mysql_fetch_array($sql))
{
$activity2=explode(",",$row["activity"]);
foreach ($activity2 as $activity)
echo '<option value="'.$activity.'">'.$activity.'</option>';
} ?>
</select> <br/><br/>
<label>Player :</label> <select name="username" class="username">
<option selected="selected">--Select Player--</option>

</select>
<input name="thisID" type="hidden" value="<?php echo $id; ?>" />
<input type="submit" name="button" id="button" value="Log In" />

</form>

coaches.php js函数

<script type="text/javascript">
$(document).ready(function()
{
$(".activity").change(function()
{
var activity=$(this).val();
var dataString = 'activity='+ activity;

$.ajax
({
type: "POST",
url: "ajax_city.php",
data: dataString,
cache: false,
success: function(html)
{
$(".username").html(html);
}
});

});
});
</script>
<style>
label
{
font-weight:bold;
padding:10px;
}
</style>

如上面的 js 所示,玩家列表是通过一个单独的页面处理的,其查询如下:

<?php
if($_POST['activity'])
{
$activity=$_POST['activity'];
$sql=mysql_query("SELECT id, username FROM stats WHERE activity='$activity'");

while($row=mysql_fetch_array($sql))
{
$id=$row['id'];
$username=$row['username'];
$activity=$row['activity'];
echo '<option value="'.$username.'">'.$username.'</option>';

}
}

?>

完成所有这些后,教练将提交表单,并将其带到 coachplayer.php。这就是问题开始的地方。

coachplayer.php 是一个模板页面,其中的空字段填充有 echo,以便在必要时回显球员详细信息。运行查询以获取所选玩家的 ID、显示其详细信息并填充页面。然而,它会回显如果查询无法通过 $playerCount 找到匹配结果时通常会出现的情况,如下所示,显示“玩家不存在”。

coachplayer.php SQL查询

<?php 
// Check to see the URL variable is set and that it exists in the database
if (isset($_GET['id'])) {
// Connect to the MySQL database
$targetU = preg_replace('#[^0-9]#i', '', $_GET['id']);
// Use this var to check to see if this ID exists, if yes then get the player
// details, if no then exit this script and give message why
$sql = mysql_query("SELECT * FROM stats WHERE id='$targetU' LIMIT 1");
$playerCount = mysql_num_rows($sql); // count the output amount
if ($playerCount > 0) {
// get all the product details
while($row = mysql_fetch_array($sql)){
$id = $row["id"];
$username = $row["username"];
$position = $row["position"];
$activity = $row["activity"];
$agegroup = $row["agegroup"];
$coach = $row["coach"];
$goals = $row["goals"];
$assists = $row["assists"];
$cleans = $row["cleans"];
$motm = $row["motm"];
$attend = $row["attend"];
}

} else {
echo "Player doesn't exist.";
exit();
}

} else {
echo "Data to render this page is missing.";
exit();
}
?>

我相信你可以看出,我不是一个很棒的编码员,所以很可能这只是一个需要更改的简单变量,但任何我出错的想法都将非常感激。

提前谢谢您。

最佳答案

您正在使用带有 post 方法的表单。而且操作 URL 似乎很不同

   <form id="form1" name="form1" method="post" action="coachplayer.php?id=' .$id. '">

更改为

    <form id="form1" name="form1" method="post" action="coachplayer.php">

和 coachplayer.php 中。使用

    isset($_POST['thisID']

关于javascript - 表单未发布变量/mySQL 查询未正确搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27459027/

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