1

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>
15
  • I think for this case you should use AJAX. Send the id, recover it, delete the row. Commented Nov 11, 2015 at 7:45
  • then i have the problem that every row has the same id? Do i understand that right? Commented Nov 11, 2015 at 7:48
  • Hu ? Each row has its own id Commented Nov 11, 2015 at 7:49
  • if i write <tr id="test"> then every row has the id test, i think. Of course i could be wrong, I'm new in web developing Commented Nov 11, 2015 at 7:53
  • No, you should pass the MySQL id to your button or link, so that you can send it to the server call. Like echo "<a href='javascript:send($takttdaten[$i]->id)' ..."; Commented Nov 11, 2015 at 7:55

2 Answers 2

3

$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>";
}
?>
Sign up to request clarification or add additional context in comments.

7 Comments

unfortunately it doesn't, but thanks! Now i try it like: i give the create date as a parameter to the delete function, the delete function works but i have a form in a form an thats the problem now, and all rows have the same name for example "date", thats another problem because i give the name date to the function as parameter so the query isn't working now with 3 or 4 etc.. parameters
Hmm, yeah i understand. So what is your goal. Just delete one record right?
Yes, i just want to delete one row out of the database. As i sad the function works, but theres the name and form problem.
Yeah, but in your delete function you have a foreach loop. That's an indication that you try to achieve delete multiple rows right?
Oh sry for better understanding: I have a new function i send the date as parameter with post/request to the function, now the problem is i cant send 3 parameters or 4 etc.. because they all have the same name attribute
|
0

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);

1 Comment

thanks for the answer! I tried it, also worked fine! :D but in my case i'd prefer first solution! Thank you! ;)

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.