【发布时间】:2015-02-12 01:44:06
【问题描述】:
我正在尝试为足球网站的管理部分创建一个球员编辑系统。 流程如下:
一旦教练登录“coaches.php”,他们就可以通过下拉菜单选择他们想要查看的教练课程,然后填充“球员”下拉菜单(通过下面的 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 是一个模板页面,其中空白字段填充了回显,以在必要时回显球员详细信息。运行查询以获取所选玩家的 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();
}
?>
我敢肯定你会说,我不是一个非常擅长编码的人,所以它很可能只是一个需要更改的简单 var,但任何我出错的想法都将不胜感激。
提前谢谢你。
【问题讨论】:
-
在执行查询之前回显 $targetU 只是为了确保您传递了正确的 ID。
-
@sskoko 我在 echo $targetU 后面的行中放入 $targetU = preg_replace('#[^0-9]#i', '', $_GET['id']);但页面仍然只显示“播放器不存在”。
-
看起来你的 $targetU 在 preg_replace 之后是空白的。
-
@sskoko 你这是什么意思?我认为它被调用并与 id 相关联,然后通过查询进行查找?
-
有一种古老的秘密技术叫做调试。首先确定没有按预期工作的行,然后确定它的哪一部分不是您认为应该的,在这种情况下只有一个部分是未知的:$targetU。检查它的值,如果不是应该的值,检查分配 $targetU 的行...从头开始重复...另一个古老的智慧是使用工具 - 用于调试正则表达式的在线正则表达式测试器,可视化 sql 工具,如MySql Workbench 用于确保您的查询良好。这样的事情会让你成为一个更好的程序员
标签: javascript php jquery mysql forms