我正在尝试获取用户输入并更新数据库中的特定项目。出于某种原因,我似乎无法让项目得到更新。
<?php
include('../inclass/db_connect.php');
$id = $_GET["id"];
$sql = "SELECT id, teamName, coach, yearFormed, numStanleyCups, numPlayers, teamValue, totWins FROM hockeyTeams WHERE id=$id";
$result = $pdo->prepare($sql);
$result->execute();
$row = $result->fetch();
?>
<h1 align="center">Edit Team - <?php echo $row['teamName'] ?></h1>
<form method="post">
<table align="center" border="1">
<tr>
<td>Team Name:<input type="text" name="teamName" value="<?php echo $row['teamName'] ?>" required /></td>
</tr>
<tr>
<td>Coach Name:<input type="text" name="coach" placeholder="Coach Name" value="<?php echo $row['coach'] ?>" required /></td>
</tr>
<tr>
<td>Year Team Formed:<input type="date" name="yearFormed" value="<?php echo $row['yearFormed'] ?>" required /></td>
</tr>
<tr>
<td>Number of Stanley Cups Won:<select name="numStanleyCups"><option value="" selected><?php echo $row['numStanleyCups'] ?></option><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option><option value="6">6</option><option value="7">7</option><option value="8">8</option><option value="9">9</option><option value="10">10</option><option value="11">11</option><option value="12">12</option><option value="13">13</option><option value="14">14</option><option value="15">15</option><option value="16">16</option><option value="17">17</option><option value="18">18</option><option value="19">19</option><option value="20">20</option><option value="21">21</option><option value="22">22</option><option value="23">23</option><option value="24">24</option><option value="25">25</option></select></td>
</tr>
<tr>
<td>Number of Players:<input type="text" name="numPlayers" value="<?php echo $row['numPlayers'] ?>"/></td>
</tr>
<tr>
<td>Team Value: <input type="text" name="teamValue" value="<?php echo $row['teamValue'] ?>"/></td>
</tr>
<tr>
<td>Total Team Wins: <input type="text" name="totWins" value="<?php echo $row['totWins'] ?>" /></td>
</tr>
<tr>
<td>
<button type="submit" name="btn-edit"><strong>Edit Team</strong></button></td> </tr>
</table>
</form>
<?php
if(isset($_POST['btn-edit']))
{
$sql = "UPDATE hockeyTeams SET teamName = '".$_POST['teamName']."', coach = '".$_POST['coach']."', yearFormed = '".$_POST['yearFormed']."', numStanleyCups = '".$_POST['numStanleyCups']."', numPlayers = '".$_POST['numPlayers']."', teamValue = '".$_POST['teamValue']."', totWins = '".$_POST['totWins']."' WHERE id = ".$_GET['id'];
$result = $pdo->prepare($sql);
$result->execute();
}
当我取出PHP的顶部时,更新功能奇怪地工作了一次而不是破坏。我似乎无法理解为什么更新不会起作用,除了与我拉动文件顶部的数据有关。
我似乎无法找到任何能找到错误答案的帖子,我检查确保我的所有'"
都正确并正常工作,所以我不知道为什么它不会更新!
非常感谢任何帮助。谢谢!
答案 0 :(得分:3)
问题是您在执行更新时显示之前表单。所以你要显示数据库中的旧值。将执行更新的代码移到顶部。
由于你正在使用PDO,你应该使用$pdo->bindParam()
作为参数,而不是将字符串连接到SQL中。
<?php
include('../inclass/db_connect.php');
$id = $_GET["id"];
if(isset($_POST['btn-edit']))
{
$sql = "UPDATE hockeyTeams SET teamName = '".$_POST['teamName']."', coach = '".$_POST['coach']."', yearFormed = '".$_POST['yearFormed']."', numStanleyCups = '".$_POST['numStanleyCups']."', numPlayers = '".$_POST['numPlayers']."', teamValue = '".$_POST['teamValue']."', totWins = '".$_POST['totWins']."' WHERE id = ".$_GET['id'];
$result = $pdo->prepare($sql);
$result->execute();
}
$sql = "SELECT id, teamName, coach, yearFormed, numStanleyCups, numPlayers, teamValue, totWins FROM hockeyTeams WHERE id=$id";
$result = $pdo->prepare($sql);
$result->execute();
$row = $result->fetch();
?>
<h1 align="center">Edit Team - <?php echo $row['teamName'] ?></h1>
<form method="post">
<table align="center" border="1">
<tr>
<td>Team Name:<input type="text" name="teamName" value="<?php echo $row['teamName'] ?>" required /></td>
</tr>
<tr>
<td>Coach Name:<input type="text" name="coach" placeholder="Coach Name" value="<?php echo $row['coach'] ?>" required /></td>
</tr>
<tr>
<td>Year Team Formed:<input type="date" name="yearFormed" value="<?php echo $row['yearFormed'] ?>" required /></td>
</tr>
<tr>
<td>Number of Stanley Cups Won:<select name="numStanleyCups"><option value="" selected><?php echo $row['numStanleyCups'] ?></option><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option><option value="6">6</option><option value="7">7</option><option value="8">8</option><option value="9">9</option><option value="10">10</option><option value="11">11</option><option value="12">12</option><option value="13">13</option><option value="14">14</option><option value="15">15</option><option value="16">16</option><option value="17">17</option><option value="18">18</option><option value="19">19</option><option value="20">20</option><option value="21">21</option><option value="22">22</option><option value="23">23</option><option value="24">24</option><option value="25">25</option></select></td>
</tr>
<tr>
<td>Number of Players:<input type="text" name="numPlayers" value="<?php echo $row['numPlayers'] ?>"/></td>
</tr>
<tr>
<td>Team Value: <input type="text" name="teamValue" value="<?php echo $row['teamValue'] ?>"/></td>
</tr>
<tr>
<td>Total Team Wins: <input type="text" name="totWins" value="<?php echo $row['totWins'] ?>" /></td>
</tr>
<tr>
<td>
<button type="submit" name="btn-edit"><strong>Edit Team</strong></button></td> </tr>
</table>
</form>