gpt4 book ai didi

php - 下一行或 id 从表和数据库中删除

转载 作者:行者123 更新时间:2023-11-28 23:17:48 25 4
gpt4 key购买 nike

我有一个章节表,每个章节都有一个删除按钮。当我单击删除按钮时,如果表中有更多记录,表的最后一行将被删除,或者传递给数据库的行 ID 错误。

<!doctype html>
<html>
<head>
<title>Files</title>
</head>
<body>

<form method="post" action="deleteFiles.php" enctype="multipart/form-data">

<style>
td {
text-align: center;
border: 1px solid black;
border-collapse: collapse;
}
</style>

Select rank :
<select name="type" id="type" onchange="this.form.submit()">

<?php if(isset($_POST['type']))
{ ?>
<option value="1" <?php echo($_POST['type']==1?"selected":"");?>>SSgt</option>

<option value="2" <?php echo($_POST['type']==2?"selected":"");?>>TSgt</option>
<option value="3" <?php echo($_POST['type']==3?"selected":"");?>>MSgt</option>
</select>
<br><br>
<?php
}
else
{
?>
<option value="1">SSgt</option>
<option value="2">TSgt</option>
<option value="3">MSgt</option>
</select>
<br><br>
<?php
}
?>

<table id="example" style="width:60%">
<tr>
<th><font size="5">Files</font></th>
</tr>

<?php



?>

<?php

ini_set('display_errors', 1);
error_reporting(1);
ini_set('error_reporting', E_ALL);

$dbh = new PDO('mysql:host=174;dbname=airman', 'airman', 'airma');


if(isset($_POST['action'])) {


$stmt = $dbh->prepare("DELETE FROM `files` WHERE `id`= " . $_POST['id']);

$stmt->execute();
$result = $stmt->fetch(PDO::FETCH_ASSOC);

if(count($result) > 0)
{
echo 'row deleted';
}
else{
echo 'row could not delete';
}
}


if(isset($_POST['type']))
{
$stmt = $dbh->prepare("SELECT * FROM files where type = :type");
$stmt->bindParam("type", $_POST['type']);
$stmt->execute();
$results = $stmt->fetchall(PDO::FETCH_ASSOC);

}
else{
$stmt = $dbh->prepare("SELECT * FROM files");
$stmt->execute();
$results = $stmt->fetchall(PDO::FETCH_ASSOC);

}

foreach($results as $file)

{
?>
<tr>
<td><?php echo $file['title'];?></td>
<td><input type="submit" id="<?php echo $file['id']?>" name="action" value="Delete">
<input type="submit" id="<?php echo $chap['id']?>" name="edit" value="Edit">
<input type="hidden" name="id" value="<?php echo $file['id']?>" />

</tr>
<?php
}
?>
</table>

</body>

</form>
</html>
<?php
?>

编辑:

    <!doctype html>
<html>
<head>
<title>Video Files</title>
</head>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>

$('#form').submit(function(ev) {

$('<input type="hidden">').attr({ name: 'type', value: $("select[name='type']").val() }).appendTo($(event.currentTarget));
}

</script>
<form id="form" method="post" action="deleteVideoFiles.php" enctype="multipart/form-data">

<style>
td {
text-align: center;
border: 1px solid black;
border-collapse: collapse;
}
</style>


<br><br><br><br>
Select rank :
<select name="type" id="type" onchange="this.form.submit()">

<?php if(isset($_POST['type']))
{
/* session_start();

$_SESSION['vidType'] = $_POST['type'];*/

?>
<option value="">Select rank...</option>
<option value="1" <?php echo($_POST['type']==1?"selected":"");?>>SSgt</option>

<option value="2" <?php echo($_POST['type']==2?"selected":"");?>>TSgt</option>
<option value="3" <?php echo($_POST['type']==3?"selected":"");?>>MSgt</option>
</select>
<br><br>
<?php
}
else
{
/* session_start();

$_SESSION['vidType'] = $_POST['type'];*/

?>
<option value="">Select rank...</option>
<option value="1">SSgt</option>
<option value="2">TSgt</option>
<option value="3">MSgt</option>
</select>
<br><br>
<?php
}
?>

<table id="example" style="width:80%">
<tr>
<th><font size="5">Video Files</font></th>
</tr>

<?php

?>

<?php

ini_set('display_errors', 1);
error_reporting(1);
ini_set('error_reporting', E_ALL);

$dbh = new PDO('mysql:host=174.75.54;dbname=handbook', 'airman', 'airman12345');
// $dbh = new PDO('mysql:host=localhost;dbname=handbook', 'siddhi', 'siddhi');

if(isset($_POST['action'])) {

$stmt = $dbh->prepare("DELETE FROM `videos` WHERE `id`= :id");
$stmt->bindParam("id", $_POST['id']);
$stmt->execute();
$result = $stmt->fetch(PDO::FETCH_ASSOC);

if(count($result) < 0)
{
echo 'row could not delete';
}
}

if(isset($_POST['type']))
{

if(!empty($_POST['type']))

{

$stmt = $dbh->prepare("SELECT * FROM `videos` where type = :type");
$stmt->bindParam("type", $_POST['type']);
$stmt->execute();
$results = $stmt->fetchall(PDO::FETCH_ASSOC);
if(count($results) == 0)
{
echo 'No records available.';
}
}
else{

echo 'empty type';
$stmt = $dbh->prepare("SELECT * FROM `videos`");
$stmt->execute();
$results = $stmt->fetchall(PDO::FETCH_ASSOC);
if(count($results) == 0)
{
echo 'No records available.';
}

}
}
/* elseif(!empty($_SESSION['vidType'])){

echo $_SESSION['vidType'];
echo $_POST['type'];

$stmt = $dbh->prepare("SELECT * FROM `videos` where type = :type");
$stmt->bindParam("type", $_SESSION['vidType']);
$stmt->execute();
$results = $stmt->fetchall(PDO::FETCH_ASSOC);
if(count($results) == 0)
{
echo 'No records available.';
}
}*/

else{

$stmt = $dbh->prepare("SELECT * FROM `videos`");
$stmt->execute();
$results = $stmt->fetchall(PDO::FETCH_ASSOC);
if(count($results) == 0)
{
echo 'No records available.';
}
}
foreach($results as $file)

{
?>
<tr>
<td><?php echo $file['title'];?></td>
<td>
<form method="post" action="deleteVideoFiles.php" enctype="multipart/form-data">

<input type="submit" id="<?php echo $file['id']?>" name="action" value="Delete"><br>
<input type="hidden" name="id" value="<?php echo $file['id']?>"/>
</form> <!-- notice how we built different form for every id-->
</td>
</tr>
<?php
}
?>
</table>

</body>

</form>
</html>
<?php
?>

我正在为每个循环发送章节 ID,这里有什么问题?请帮忙谢谢..

最佳答案

这是因为以这种方式构建表单元素将导致同一表单内的许多 id 输入。像这样

<form>
<input name="id" value="1">
<input name="id" value="2">
</form>

在您的服务器代码上,php 会将 $_POST['id'] 设置为最后一个,在本例中为 2。这就是为什么当按下删除按钮并提交表单时,您删除了您在表单中构建的下一个或最后一个 id

如果您需要您的代码按照您希望的方式工作,您必须将每个 id 放在不同的表单元素中,否则您将需要一些 javascript 工作。

我更喜欢为每条记录构建不同的表格。替换该代码

foreach($results as $file)
{
?>
<tr>
<td><?php echo $file['title'];?></td>
<td><input type="submit" id="<?php echo $file['id']?>" name="action" value="Delete">
<input type="submit" id="<?php echo $chap['id']?>" name="edit" value="Edit">
<input type="hidden" name="id" value="<?php echo $file['id']?>" />
</tr>
<?php
}
?>

像这样的东西

foreach($results as $file)
{
?>
<tr>
<td><?php echo $file['title'];?></td>
<td>
<form method="post" action="deleteFiles.php" enctype="multipart/form-data">
<input type="submit" id="<?php echo $file['id']?>" name="action" value="Delete">
<input type="submit" id="<?php echo $chap['id']?>" name="edit" value="Edit">
<input type="hidden" name="id" value="<?php echo $file['id']?>" />
</form> <!-- notice how we built different form for every id-->
</td>
</tr>
<?php
}
?>

另一个注释:

$stmt = $dbh->prepare("DELETE FROM `files` WHERE `id`= " . $_POST['id']);

以这种方式使用准备好的语句会使您仍然容易受到 SQL 注入(inject)攻击。


编辑(对评论的 react )将另一个输入与删除表单一起发送到 PHP 服务器。您可以为此使用 JavaScript。

1- 将该类 deleteForm 添加到删除表单中,以便我们可以捕获这些表单并仅绑定(bind)到它们。

<form method="post" class="deleteForm" action="deleteVideoFiles.php" enctype="multipart/form-data">

2- 编辑文档中的脚本标签

<script>
$(function (){
$('form.deleteForm').submit(function(ev) {
$('<input type="hidden">').attr({ name: 'type', value: $("select[name='type']").val() }).appendTo($(ev.currentTarget));
});
})

</script>

关于php - 下一行或 id 从表和数据库中删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43090505/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com