SQL Update查询不更新记录

时间:2015-02-03 01:53:57

标签: php mysql

我有一个游戏列表,点击后会生成一个允许用户在更新前编辑记录的表单。

这是编辑游戏页面;

<?php
$gameID = isset($_GET['gameID']) ? $_GET['gameID'] : '';
$gameYear = isset($_GET['gameYear']) ? $_GET['gameYear'] : '';
$gamePrice = isset($_GET['gamePrice']) ? $_GET['gamePrice'] : '';
$gameName = isset($_GET['gameName']) ? $_GET['gameName'] : '';

$sql = "SELECT * FROM game WHERE gameName = $gameName";
  $queryresult = mysqli_query($conn, $sql) 
  or die (mysqli_error($conn));

$row = mysqli_fetch_assoc($queryresult);        

    $gameID = $row['gameID'];
    $gameYear = $row['gameYear'];
    $gamePrice = $row['gamePrice'];
    $gameName = $row['gameName'];       

 ?>

<div id="form">
<form action="updateGame.php" id="Update" method="get">
<label> Game ID
  <input id="text" name="id" value = "<?php echo $gameID; ?>" />
</label>
<label> Year
  <input type = "text" name = "year" value = "<?php echo $gameYear; ?>" />
</label>
<label> Price
  <input type = "text" name = "price" value = "<?php echo $gamePrice; ?>" />
</label>
<label> Name
  <input type = "text" name = "name" value = "<?php echo $gameName; ?>" />
</label>

<input type = "submit" value = "Update">

以下是单击提交按钮时运行的更新页面;

<?php
$gameID = isset($_GET['gameID']) ? $_GET['gameID'] : '';
$gameYear = isset($_GET['gameYear']) ? $_GET['gameYear'] : '';
$gamePrice = isset($_GET['gamePrice']) ? $_GET['gamePrice'] : '';
$gameName = isset($_GET['gameName']) ? $_GET['gameName'] : '';

$sql = "UPDATE game SET gameYear = '$gameYear', gamePrice = '$gamePrice', gameName = '$gameName' WHERE gameID = '$gameID'";

mysqli_query($conn, $sql) 
or die (mysqli_error($conn));
mysqli_close($conn);

echo "Updated";

?>

编辑游戏页面有效,我可以编辑记录,当我按下提交按钮时,它会显示回复声明,说明记录已更新,但是,没有发生任何变化,我不知道为什么。

2 个答案:

答案 0 :(得分:0)

您已忘记在查询中附加变量。我应该是这样的:

$sql = "UPDATE game SET gameYear = '".$gameYear."', gamePrice = '".$gamePrice."', gameName = '".$gameName."' WHERE gameID = '".$gameID."'";

答案 1 :(得分:0)

您的表单使用的是name="id",而不是name="gameID",因此您的$_GET['gameID']变量未设置在您的任何请求上,但$_GET['id']是。GET。更新您的表单或{{1}}页面,以便名称匹配。