我正在尝试为足球网站的管理部分创建一个球员编辑系统。该过程如下:
一旦教练登录"教练.php",他们就可以通过下拉菜单选择他们想要查看的教练课程,然后填充"玩家"下拉菜单(通过下面的js完成(
教练.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>
教练.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>';
}
}
?>
完成所有这些操作后,教练提交表格,将他们带到教练.php。这就是问题开始的地方。
CoachPlayer.php是一个模板页面,空白字段填充了Echo,以便在必要时回显玩家详细信息。运行查询以获取所选玩家的 ID,显示他们的详细信息并填充页面。但是,相反,如果查询无法通过$playerCount找到匹配的结果,则通常会出现如下所示,例如"Player 不存在"。
教练员.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">
在教练.php。用
isset($_POST['thisID']
好的,我承认这不是对你的问题的答案,但是,老实说,没有"你的问题"这样的东西 - 有四个文件的内容,每个文件都有自己的问题,以及一个隐含的请求来搜索所有这 4 个文件并告诉您哪些不起作用以及如何使其工作。
话虽如此:
- 分而治之。确保你的第一个脚本完全执行需要完成的操作。然后是第二,然后是第三,然后是第四。
- 使用工具:对于javascript - 开发工具或Firebug。对于查询 - MySQL 工作台测试JS时使用控制台(在这里你可以交互式地试用你的js代码。( 和源选项卡 - 您可以在其中设置断点并逐行跟踪执行。查看网络选项卡 - 在那里您可以看到请求(标头(和响应。调试 PHP 时,请注释掉所有代码,并在每一步中使用var_dump。我使用 PHP Storm,这样我就可以逐行实时调试 PHP。
最好提出可以用最少的代码行描述的问题
附言。您可以通过在浏览器中键入 url 来模拟 GET 请求 - 这样您就可以知道您的服务器端是否工作,而无需依赖不可靠的 JS
只是快速浏览一下,但您的sql语句似乎错误。您的 sql 查询将搜索 ID 为"$targetU"的播放器。确保正确输入变量