使用 PHP 和 MySQL 从数据库中删除单独的行
Delete individual rows from database with PHP and MySQL
我通过 SELECT FROM...
(MySQL) 从数据库中获取 table 中的值。这工作正常,但现在我想在每一行中都有一个删除按钮,从数据库中删除这一行。问题是创建日期都具有相同的名称属性
<?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ügen!</h3>
<div style="height:5px;"></div>
<table style="font-size:12px;" id="tabelle" class="table">
<thead>
<tr>
<th>Erstelldatum</th>
<th>Gültig von</th>
<th>Gültig bis</th>
<th>Schichtnummer</th>
<th>Stück/min</th>
<th style='float:right'>Taktvorgabe lö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 变量的 link 而不是表单。这是您的案例的示例:
假设 comment/question 中的函数在 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 table 中的 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>";
}
?>
从代码中,我看不到您引用的是要删除的 ID。
这是您的代码:
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>";
我可能会重构您的代码。
我不会将表单元素放在最后一个 td 中,而是将表单放在整个 table 周围。
然后您必须像这样命名复选框 checkbox[] 以便它被定义为一个复选框数组。然后你会得到一个复选框数组来循环 - 那些有值的是(如果我没记错的话)。
根据目前的代码我有以下建议:
如果你想在 PHP 中处理这个,你将在你的 shiftenter.php 文件中执行 $_POST['checkbox']
但是该复选框在数据库中没有您的 post 的编号。
您可以通过向输入添加值来解决此问题,或者您可以添加输入 type='hidden' name='itemid' value='[YOUR_ID_VARIABLE] ?>
这看起来像这样:
解决方案 1:(上面建议的)
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>";
解决方案 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>";
然后您使用 $_POST['itemid'];
请求
选项 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>";
然后您可以使用 $_GET['itemid'];
获取此值
请记得检查您的值,确保它们是整数(在本例中)。
您可以这样做:
$itemId = (int) $_POST['itemid'];
或使用 PHP 清理过滤器,您可以在此处阅读:http://php.net/manual/en/filter.filters.sanitize.php
此处的消毒示例:
$myInt = "asfdsfsdafdsafs";
echo filter_var($myInt, FILTER_SANITIZE_NUMBER_INT);
我通过 SELECT FROM...
(MySQL) 从数据库中获取 table 中的值。这工作正常,但现在我想在每一行中都有一个删除按钮,从数据库中删除这一行。问题是创建日期都具有相同的名称属性
<?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ügen!</h3>
<div style="height:5px;"></div>
<table style="font-size:12px;" id="tabelle" class="table">
<thead>
<tr>
<th>Erstelldatum</th>
<th>Gültig von</th>
<th>Gültig bis</th>
<th>Schichtnummer</th>
<th>Stück/min</th>
<th style='float:right'>Taktvorgabe lö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 变量的 link 而不是表单。这是您的案例的示例:
假设 comment/question 中的函数在 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 table 中的 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>";
}
?>
从代码中,我看不到您引用的是要删除的 ID。
这是您的代码:
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>";
我可能会重构您的代码。
我不会将表单元素放在最后一个 td 中,而是将表单放在整个 table 周围。 然后您必须像这样命名复选框 checkbox[] 以便它被定义为一个复选框数组。然后你会得到一个复选框数组来循环 - 那些有值的是(如果我没记错的话)。
根据目前的代码我有以下建议:
如果你想在 PHP 中处理这个,你将在你的 shiftenter.php 文件中执行 $_POST['checkbox'] 但是该复选框在数据库中没有您的 post 的编号。 您可以通过向输入添加值来解决此问题,或者您可以添加输入 type='hidden' name='itemid' value='[YOUR_ID_VARIABLE] ?>
这看起来像这样:
解决方案 1:(上面建议的)
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>";
解决方案 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>";
然后您使用 $_POST['itemid'];
请求选项 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>";
然后您可以使用 $_GET['itemid'];
获取此值请记得检查您的值,确保它们是整数(在本例中)。 您可以这样做: $itemId = (int) $_POST['itemid']; 或使用 PHP 清理过滤器,您可以在此处阅读:http://php.net/manual/en/filter.filters.sanitize.php
此处的消毒示例:
$myInt = "asfdsfsdafdsafs";
echo filter_var($myInt, FILTER_SANITIZE_NUMBER_INT);