mysql查询后表没有更新

时间:2014-04-18 18:14:27

标签: php html mysql

我有一个administrator.php,它显示了一个名为'player'的表中的300条记录。在每个记录旁边,有一个编辑选项,可将您重定向到edit.php,该记录的15列(包括主键 - playerid)显示在文本框内。下面的代码行:

<a href='edit.php?playerid=".$query2['playerid']."'>Edit</a>

在edit.php上,您可以更改这些列的数据。提交后,将发送更新查询以更新表,但不幸的是,它无法正常工作。我的错误信息继续显示(“测试错误...”);不知道为什么。

//Setups up the database connection
    $link = mysql_connect("localhost", "root", "");
    mysql_select_db("fantasymock", $link);

    if(isset($_GET['playerid'])) {
        $playerid = $_GET['playerid'];
        //Query to display results in input box
        $query1 = mysql_query("SELECT * from player WHERE playerid = '$playerid'");
        $query2 = mysql_fetch_array($query1);
    }

    if(isset($_POST['submit'])) {
        $playerid = $_POST['playerid'];
        $preranking = $_POST['preranking'];
        $playerlast = $_POST['playerlast'];
        $playerfirst = $_POST['playerfirst'];
        $position = $_POST['position'];
        $battingavg = $_POST['battingavg'];
        $run = $_POST['run'];
        $homerun = $_POST['homerun'];
        $rbi = $_POST['rbi'];
        $sb = $_POST['sb'];
        $win = $_POST['win'];
        $save = $_POST['save'];
        $strikeout = $_POST['strikeout'];
        $era = $_POST['era'];
        $whip = $_POST['whip'];
        //Query to update dB
        $query3 = mysql_query("UPDATE player SET playerid='$playerid', preranking='$preranking', playerlast='$playerlast', playerfirst='$playerfirst', position='$position', battingavg='$battingavg', run='$run', homerun='$homerun', rbi='$rbi', sb='$sb', win='$win', save='$save', strikeout='$strikeout', era='$era', whip='$whip' WHERE playerid='$playerid'");
        header("Location: administrator.php");
    } else {
        echo "Testing For Error....";

    }
?>

<form action="" method="POST">
    Player ID:<input type="text" name="playerid" value="<?php echo $query2['playerid'];?>"/> <br/>
    Preranking:<input type="text" name="preranking" value="<?php echo $query2['preranking'];?>"/> <br/>
    Last Name:<input type="text" name="playerlast" value="<?php echo $query2['playerlast'];?>"/> <br/>
    First Name:<input type="text" name="playerfirst" value="<?php echo $query2['playerfirst'];?>"/> <br/>
    Position:<input type="text" name="position" value="<?php echo $query2['position'];?>"/> <br/>
    Batting Avg:<input type="text" name="battingavg" value="<?php echo $query2['battingavg'];?>"/> <br/>
    Runs:<input type="text" name="run" value="<?php echo $query2['run'];?>"/> <br/> 
    Homeruns:<input type="text" name="homerun" value="<?php echo $query2['homerun'];?>"/> <br/>
    Rbi:<input type="text" name="rbi" value="<?php echo $query2['rbi'];?>"/> <br/>
    Sb:<input type="text" name="sb" value="<?php echo $query2['sb'];?>"/> <br/>
    Wins:<input type="text" name="win" value="<?php echo $query2['win'];?>"/> <br/>
    Saves:<input type="text" name="save" value="<?php echo $query2['save'];?>"/> <br/>
    Strikeouts:<input type="text" name="strikeout" value="<?php echo $query2['strikeout'];?>"/> <br/>
    Era:<input type="text" name="era" value="<?php echo $query2['era'];?>"/> <br/>
    Whip:<input type="text" name="whip" value="<?php echo $query2['whip'];?>"/> <br/>
    <br>
    <input type="submit" name="submit" value="submit">
</form>

仅供参考:表格和表格名称中的每一列都拼写正确,我在发布之前已进行了三次检查。我知道MySQL注入。有人能看到问题吗?提前谢谢!

编辑:我刚刚添加了一个额外的if语句if($query3),现在它可以正常工作。

3 个答案:

答案 0 :(得分:0)

您正在检查POST变量,但是您将通过GET请求进入edit.php。 $_POST上没有任何内容。因此,它会下降到if块的else,并打印出Testing For Error...

答案 1 :(得分:0)

进入else部分的脚本。这意味着它没有像$_POST['submit']那样得到任何东西。确保您的submit button name属性必须为submit

<input type="submit" name="submit" value="" />

答案 2 :(得分:0)

请检查error.log文件中显示的内容。您可以在edit.php文件

中插入这些行
error_reporting(E_ALL);
ini_set('display_errors', 1);

显示错误。 用此替换您的其他部分以获取更详细的mysql错误

else{ echo "Testing For Error...." .mysql_error(); }