Zgbrell Zgbrell - 7 months ago 8
PHP Question

Simply trying to update an item in mysql database

I am trying to take a user input and update that specific item in my database. For some reason I cannot seem to get the item to get updated.

<?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();
}


When I took out the top portion of the PHP the update function oddly enough worked once and than broke. I cant seem to put together a reasoning of why the update wouldnt be working other than having something to do with me pulling in the data at the top of the file.

I couldnt seem to find any posts that were finding a good answer to what was going wrong, I checked to make sure all my
'"
are correct and working so I am stuck on why it wont update!

Any help is greatly appreciated. Thanks!

Answer

The problem is that you're displaying the form before you perform the update. So you're showing the old values from the database. Move the code that performs the update to the top.

And since you're using PDO, you should use $pdo->bindParam() for your parameters, instead of concatenating strings into the 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>