0

我对 PHP 比较陌生,在一个不相关的领域工作了几年后,我回到了学校。我正在为我爸爸做一个外部项目,并且遇到了表面上看起来很容易解决但我似乎无法解决的问题。

在有问题的页面上,它显示了一个联赛中的球员名单和他们的得分。它下面的表格有一个文本框,允许您输入名称和三个按钮;第一个添加名称作为玩家(默认得分为 0),从玩家列表中删除该玩家,第三个用于调整该玩家的分数。我让前两个(“添加”和“删除”)按我的意愿工作,但是我遇到的问题是分数调整一个。

如果您单击“调整分数”按钮,它会显示一个新的文本框以输入分数以及另一个按钮以实际提交分数。问题是当您按下新按钮来调整分数时没有任何反应,此时我刚刚得到了一个测试“回声”语句,应该在提交时显示,但这并没有发生。下面是有问题的代码。关于如何解决的任何想法?感谢您的任何建议。

<html>
<head>
    <LINK REL="stylesheet" type="text/css" href="styles/footballTest.css" />
    <title>VFW Football testing</title>
</head>
<body>
    <?php
        $dbConnect = mysql_connect("localhost", "root","");
        $dbName = "vfwleaguetest";
        $getPlayers = "SELECT * FROM `players`";
        if(!$dbConnect)
        {
            echo "<p>Connection failed</p>";
        }   
        if(mysql_select_db($dbName, $dbConnect) === false)
        {
            echo "<p>Could not select the database ".$dbName ."<br/>".mysql_error($dbConnect)." </p>";
        }
        if(isset($_POST['submit']))
        {
            $name=@$_POST['nameAdd'];
            $playerAdd = "INSERT INTO `vfwleaguetest`.`players` (`playerName`, `seasonTotal`) VALUES ('".$name."', '0')";
            if(mysql_query($playerAdd, $dbConnect) === false)
            {
                echo "<p>Error adding player to database: ".mysql_error($dbConnect)."</p>";
            }
        }
        unset($_POST['submit']);
        if(isset($_POST['delete']))
        {
            $name= @$_POST['nameAdd'];
            $playerDelete = "DELETE FROM `vfwleaguetest`.`players` WHERE `players`.`playerName` = '".$name."'";
            if(mysql_query($playerDelete, $dbConnect) ===false)
            {
                echo "<p>Error deleting player to database: ".mysql_error($dbConnect)."</p>";
            }
            else
            {
                echo $name." successfully removed from player list";
            }
        }
        if(isset($_POST['adjust']))
        {
            ?>
            <br/>
            <input type='text' name='scoreAdjust' size=5 />&nbsp;
            <input type='submit' name='fixScore' value='new season total for <?php echo $_POST['nameAdd'];?>' />
            <?php
            if(isset($_POST['fixScore'])) //THIS IS WHERE THE PROBLEM SEEMS TO LIE
            {
                echo "after fixScore click";
                //add sql below
            }
        }   
    ?>
    <form name="players" action="" method="post">
    <?php
        $playerList = mysql_query($getPlayers, $dbConnect);
        echo "<table><th><tr>";
        echo "<td>Player name</td><td>Season total</td></tr></th>";
        while(($row = mysql_fetch_row($playerList)) != false)
        {
            echo "<tr><td>$row[0]</td><td>$row[1]</td></tr>";
        }
        echo "</table>";
    ?>
    Manage players&nbsp;<input type="text" name="nameAdd" />&nbsp;
<input type="submit" name="submit" value="add" />&nbsp;
<input type="submit" name="delete" value="delete" />&nbsp;
<input type="submit" name="adjust" value="adjust score" />
    </form>
</body>
</html>    
4

3 回答 3

0

您的<input>元素scoreAdjust需要在<form>正在提交的元素内。

于 2012-07-10T04:26:53.810 回答
0

使用此代码:

            <br/>
            <form method="post">
                <input type='text' name='scoreAdjust' size=5 />&nbsp;
                <input type='submit' name='fixScore' value='new season total for <?php echo $_POST['nameAdd'];?>' />
            </form>
            <?php
            if(isset($_POST['fixScore'])) //THIS IS WHERE THE PROBLEM SEEMS TO LIE
            {
                echo "after fixScore click";
                //add sql below
            }

对于要提交到 的字段$_POST,它需要在form.

于 2012-07-10T04:29:31.310 回答
0

问题是分数调整输入不符合格式:

<input type="text" size="5" name="scoreAdjust">
<input type="submit" value="new season total for aaa" name="fixScore">

你需要:

<form method="post" action=''>
  <input type="text" size="5" name="scoreAdjust">
  <input type="submit" value="new season total for aaa" name="fixScore">
</form>
于 2012-07-10T04:31:48.020 回答