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

时间:2014-12-13 12:55:53

标签: javascript php jquery mysql forms

我试图为足球网站的管理部门创建一个播放器编辑系统。 过程如下:

一旦教练登录了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是一个模板页面,空白字段填充了echo,以便在必要时回显玩家详细信息。运行查询以获取所选播放器的ID,显示其详细信息并填充页面。相反,如果查询无法通过$ playerCount找到匹配的结果,如下所示,它会回应通常会出现的情况,说&#34;播放器不存在&#34;。

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();
}
?>

我确信你可以告诉我,我不是一个编码器太棒了,所以很可能它只是一个需要改变的变量而是我的任何想法我会非常感激。我们非常感激。

提前谢谢。

3 个答案:

答案 0 :(得分:1)

您正在使用带有post方法的表单。行动网址似乎完全不同

   <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']

答案 1 :(得分:1)

好的,我承认这不是你的问题的答案,但说实话,没有“你的问题” - 有四个文件的内容,每个文件都有自己的问题,并且隐含请求grock所有这4个文件,并告诉你什么不起作用,以及它应该如何工作。

说完了:

  • 分而治之。确保您的第一个脚本确实完成了需要完成的操作。然后是第二,然后是第3,然后是第4。
  • 使用工具:适用于javascript - Dev Tools或Firebug。对于查询 - MySQL Workbench 测试JS时使用控制台(在这里你可以交互式地试用你的js代码。 )和源选项卡 - 您可以在那里设置断点并逐行执行。查看网络选项卡 - 您可以在其中查看请求(标题)和响应。 调试PHP时,注释掉所有代码,并在每一步使用var_dump。我使用PHP Storm,以便我可以逐行实时调试PHP。

最好提出可用最少的代码行描述的问题

PS。您可以通过在浏览器中键入url来模拟GET请求 - 这样您就可以知道服务器端是否正常工作而不依赖于不可靠的JS

答案 2 :(得分:-1)

只是快速查看,但你的sql语句似乎错了。您的SQL查询将搜索ID为'$ targetU'的Player。确保正确输入变量