Valcone
Valcone

Reputation: 55

Delete individual rows from database with PHP and MySQL

I get the values in the table from the database by SELECT FROM...(MySQL). This works fine, but now i want to have a delete button in each row that deletes this row from database. The problem is that the create dates all have the same name attribute

<?php  $createDate = $_REQUEST['createDate'] ;

function deleteQuantityPlan($createDate) {

createConnection();

$delete_quantity_plan = "DELETE FROM andon_quantity_plan where erstelldatum = '". $createdate ."'";

$result = mysql_query($delete_quantity_plan) or die ("error");

return;   }?>

HTML:

<form action="shiftenter.php" method="post">
                <div class="col-lg-2" style="background-color:#E1E1E1;width:65%;border-radius:10px;float:left;margin-left:17.5%;margin-right:17.5%;margin-top:2.5%">
                  <h3>Taktvorgabe hinzuf&uuml;gen!</h3>
                    <div style="height:5px;"></div>
                            <table style="font-size:12px;" id="tabelle" class="table">
                                <thead>
                                  <tr>
                                    <th>Erstelldatum</th>
                                    <th>G&uuml;ltig von</th>
                                    <th>G&uuml;ltig bis</th>
                                    <th>Schichtnummer</th>
                                    <th>St&uuml;ck/min</th>
                                    <th style='float:right'>Taktvorgabe l&ouml;schen</th>
                                  </tr>
                                </thead>
                            <tbody id="table1Body">

                                <?php
                                $takttdaten = getAllQuantityPlans();

                                for ($i = 0; $i < sizeof($takttdaten); $i++) {
                                    echo "<tr>";
                                    echo "<form action='shiftenter.php' method='post'><td name='datum'>" . $takttdaten[$i]->erstelldatum . "</td>";
                                    echo "<td>" . $takttdaten[$i]->validfrom . "</td>";
                                    echo "<td>" . $takttdaten[$i]->validuntil . "</td>";
                                    echo "<td>" . $takttdaten[$i]->shiftnumber . "</td>";
                                    echo "<td>" . $takttdaten[$i]->planquantity . "</td>";
                                    echo "<td><button style='float:right;' class='btn btn-danger' type='submit'>X</button></td></form>";
                                    echo "</tr>";
                                }
                                ?>
                         </tbody>
                     </table>
                </div>
            <button type="submit" style="width:15%;margin-top:35px;" class="btn btn-info">
                <span style="float: left;" class="glyphicon glyphicon-floppy-disk"></span>Taktvorgabe speichern!</button>
        </form>

Upvotes: 1

Views: 133

Answers (2)

Trond
Trond

Reputation: 403

From the code, I cannot see you are referencing the Id you want to delete.

This is your code:

echo "<td><form action='shiftenter.php' method='post'><input type='checkbox' name='checkbox'>
<button style='float:right;' name='deletePlan' class='btn btn-danger' type='submit'>X</button></form></td>";
echo "</tr>";

I would perhaps refactor your code.

In stead of having the a form element inside the last td, I would put the form around the whole table. You must then name the checkbox like this checkbox[] so it is defined as an array of checkboxes. You then get an array of checkboxes to loop over - those that has values that is (If I remember this correctly).

Based on the current code I have the following suggestions:

When you want to process this in PHP, you will do $_POST['checkbox'] in your shiftenter.php-file But the checkbox does not have the number of your post in the database. You can solve this by adding value to your input, or you could add a input type='hidden' name='itemid' value='[YOUR_ID_VARIABLE] ?>

This would then look something like this:

Solution 1: (which was suggested above)

echo "<td><form action='shiftenter.php' method='post'><input type='checkbox' name='checkbox' value='<?php echo $takttdaten[$i]->[YOUR_ID_VARIABLE] ?>'>
<button style='float:right;' name='deletePlan' class='btn btn-danger' type='submit'>X</button></form></td>";
echo "</tr>";

Solution 2:

echo "<td><form action='shiftenter.php' method='post'>
<input type='hidden' name='itemid' value='<?php echo $takttdaten[$i]->[YOUR_ID_VARIABLE] ?>'>
<input type='checkbox' name='checkbox' value='<?php echo $takttdaten[$i]->[YOUR_ID_VARIABLE] ?>'>
<button style='float:right;' name='deletePlan' class='btn btn-danger' type='submit'>X</button></form></td>";
echo "</tr>";

You then request this with $_POST['itemid'];

Alternative 3:

echo "<td><form action='shiftenter.php?itemid=<?php echo $takttdaten[$i]->[YOUR_ID_VARIABLE] ?>' method='post'>
<input type='checkbox' name='checkbox' value='<?php echo $takttdaten[$i]->[YOUR_ID_VARIABLE] ?>'>
<button style='float:right;' name='deletePlan' class='btn btn-danger' type='submit'>X</button></form></td>";
echo "</tr>";

You can then get this value with $_GET['itemid'];

Do remember to check your values so they are ints (in this case). You can do this either like this: $itemId = (int) $_POST['itemid']; or using PHP sanitize filters which you can read about here: http://php.net/manual/en/filter.filters.sanitize.php

Example of santizing here:

$myInt = "asfdsfsdafdsafs";
echo filter_var($myInt, FILTER_SANITIZE_NUMBER_INT);

Upvotes: 0

S.Pols
S.Pols

Reputation: 3434

$delete_id = $_POST['checkbox']; this code probably won't have a value. That's because your checkbox doesn't have a value. I think your code will work when you change:

<input type='checkbox' name='checkbox'>

Into

<input type='checkbox' name='checkbox' value="<?= $takttdaten[$i]->[YOUR_ID_VARIABLE] ?>">

EDIT

You can use just a link with GET variables instead of a form. Here's an example for your case:

Assuming the function in your comment/question is in the shiftenter file, change the PHP to :

if (isset($_GET['deletePlan']) && $_GET['deletePlan'] == 'true') {
    $deleteDate = $_GET['deletePlanDate'];

    if($deleteDate != null && $deleteDate != "")
    {
        deleteQuantityPlan($deleteDate);
    }
    else
    {
        echo "Plan delete failed, Date [".$deletePlanDate."] was not valid.";
    }       
}

Then change the PHP section in your HTML table to:

<?php
$takttdaten = getAllQuantityPlans();
for ($i = 0; $i < sizeof($takttdaten); $i++) {
    echo "<tr>";
    echo "<td>" . $takttdaten[$i]->erstelldatum . "</td>";
    echo "<td>" . $takttdaten[$i]->validfrom . "</td>";
    echo "<td>" . $takttdaten[$i]->validuntil . "</td>";
    echo "<td>" . $takttdaten[$i]->shiftnumber . "</td>";
    echo "<td>" . $takttdaten[$i]->planquantity . "</td>";
    echo "<td> <a href='shiftenter.php?deletePlan=true&deletePlanDate=".$takttdaten[$i]->erstelldatum ."'>Delete</a></td>";
    echo "</tr>";
}
?>

Upvotes: 3

Related Questions