0

I have a web page that outputs data from my sql database. Each row has a delete button that should delete the data in this particular row. I am having a issue where when I click the delete button it always deletes the row with the first/lowest ID regardless of which button I click. It doesnt delete the row I want it to delete. Here is my code: HTML

<form action="process.php" method="post">
<?php
$sql = "
    SELECT *
      FROM playerTeam
";
$result = mysqli_query($connection, $sql);
?>      
<table>
    <?php
    if (mysqli_num_rows($result) > 0) {
         while ($row = mysqli_fetch_assoc($result)) { ?>
            <tr>
                <td><input type="text" name="id" value="<?php echo $row['id']?>"></td>
                <td><input type="text" name="fName[]" value="<?php echo $row['firstName']?>"></td>
                <td><input type="text" name="sName[]" value="<?php echo $row['surName']?>"></td>
                <td><input type="text" name="team[]" value="<?php echo $row['team']?>"></td>
                <td><input type="submit" name="delete" value="Delete"></td>
                <td><input type="submit" name="update" value="Update"></td>
            </tr>
            <?php
        }
    } 
    ?>
    </table>
</form>

process.php

if (isset($_POST['delete'])) {
    $id = $_POST['id'];
    $sql = "DELETE FROM playerTeam WHERE id='$id'";

    if (mysqli_query($connection, $sql)) {
        echo "Record deleted";
    } else {
        echo "Error: " . $sql . "<br>" . mysqli_error($connection);
    }
}

please could someone help me. Thanks

3
  • input type="submit" doesn't have a name, so your name=delete gets ignored Commented Feb 20, 2016 at 1:09
  • Try echoing out $id to double check if it is the row you want to delete Commented Feb 20, 2016 at 1:13
  • @luweiqi when I echo $id it doesnt output the right value, every button always outputs the first id in my table. If I make the hidden id input into a array and itterate over it, it outputs all of the id's. Commented Feb 20, 2016 at 1:15

3 Answers 3

1

Because you delete the value of last id in

$id = $_POST['id'];

$sql = "DELETE FROM playerTeam WHERE id='$id'";

the value of $_POST['id'] is equal to last row in <?php echo $row['id']?> in each run of the while the $_POST['id'] value replaced by new $row['id'] so the last on would be read in $_POST['id']

Sign up to request clarification or add additional context in comments.

Comments

1

As you are using while($row = mysqli_fetch_assoc($result)), this will loop to the last row in your MySQL Table, thus every time, it will have the last id of your table.

You can code it in a way that your delete script will get the id. So, your delete button for each row will be have the row id, e.g. process.php?1, process.php?2, process.php?3 and so on.

tablepage.php (not sure of your page's real name)

Replace this line:

<td><input type="submit" name="delete" value="Delete"></td>

With this:

<td><p><a href="/process.php?id=<?php echo $id; ?>"></td>

process.php?id=1

// this will get `id=1` and thus your `id` will be `1`, and it will delete row `1`.
$id = $_GET['id'];

$sql = "DELETE FROM playerTeam WHERE id='$id'";

if (mysqli_query($connection, $sql)) {
    echo "Record deleted";
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($connection);
}

Hope that helps, thanks!

4 Comments

Thanks for your answer, if i use e.g $sql = "SELECT * FROM playerTeam WHERE id=1" wont this just output the one row that contains the id value of 1
@SnowmanPusha Yes, but isn't that you want to delete one row?
yes I want to delete one row, but I want the user to choose the row by clicking the delete button. Each row has a delete button which when clicked deletes that particular row. Apologies if my op wasnt clear.
@SnowmanPusha Updated code, maybe it's a better solution, thanks
0

In actual in this condition ,use ajax and jquery is much more easier i think .But do only by php gonna be use logic code

  $i = 0;
  while($row = mysqli_fetch_assoc($result)) 
     {
     ?>
        <tr>
            <td><input type="text" name="id_<?= $i ?>" value="<?php echo $row['id']?>"></td>
            <td><input type="text" name="fName[]" value="<?php echo $row['firstName']?>"></td>
            <td><input type="text" name="sName[]" value="<?php echo $row['surName']?>"></td>
            <td><input type="text" name="team[]" value="<?php echo $row['team']?>"></td>
            <td><input type="submit" name="delete[<?= $i ?>]" value="Delete"></td>
            <td><input type="submit" name="update[<?= $i ?>]" value="Update"></td>
        </tr>
        <?php
      $i++; //for unique num for each row
    }

process.php

if (isset($_POST['delete'])){
    $delete_id = array_keys($_POST['delete']);//print array key as a array
    $delete_id = $delete_id[0];//so get array key
    $id = $_POST["id_$delete_id"];//get its relative id
    .....
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.