我通过SELECT FROM...(MySQL)从数据库获取表中的值。这很好,但现在我想在每一行中都有一个delete按钮,用于从数据库中删除这一行。问题是创建日期都具有相同的name属性

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

函数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>

最佳答案

$delete_id = $_POST['checkbox'];此代码可能没有值。那是因为你的checkbox没有value。我想你的代码会在你改变时起作用:
<input type='checkbox' name='checkbox'>
进入
<input type='checkbox' name='checkbox' value="<?= $takttdaten[$i]->[YOUR_ID_VARIABLE] ?>">
编辑
您可以只使用带有GET变量的链接而不是表单。以下是您的案例示例:
假设注释/问题中的函数位于shiftenter文件中,请将PHP更改为:

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.";
    }
}

然后将HTML表中的PHP部分更改为:
<?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>";
}
?>

关于php - 使用PHP和MySQL从数据库中删除单个行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33646076/

10-14 05:19