【问题标题】:Form not posting variables/mySQL Query not searching corrently表单未发布变量/mySQL 查询未正确搜索
【发布时间】: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


【解决方案1】:

您正在使用带有 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']

【讨论】:

  • 好的,那我该怎么办?
  • 不知何故设置了 $_GET['id'],否则他会收到:“缺少呈现此页面的数据。”
  • 更改操作 URL 和 isset 后......尝试回显 $targetU 并查看您是否传递了正确的 id
  • 嗨@IndraKumarS 我做了你的更改,然后将“播放器不存在”输出更改为 echo $targetU;但页面只是空白,不回显 ID
  • 在查询之前回显。
【解决方案2】:

好的,我承认这不是您问题的答案,但是,老实说,没有“您的问题”之类的东西 - 有四个文件的内容,每个文件都有自己的问题,并且隐含请求grock 所有这 4 个文件,并告诉您哪些不起作用以及如何使其起作用。

话虽如此:

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

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

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

【讨论】:

    【解决方案3】:

    只是快速看一下,但您的 sql 语句似乎错误。您的 sql 查询将搜索 ID 为“$targetU”的播放器。确保正确输入变量

    【讨论】:

      猜你喜欢
      • 2011-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-10
      • 1970-01-01
      相关资源
      最近更新 更多