gpt4 book ai didi

php - 从我页面的第二个表中删除行的问题

转载 作者:行者123 更新时间:2023-11-29 03:03:57 24 4
gpt4 key购买 nike

我有一个包含 2 个表格的报告页面,一个显示库存商品,另一个显示已售商品。我为他们做了一个删除选项,它在表格的右端提供了一个按钮,用于从我的表格中删除行。我遇到的问题是该代码适用于第一个表,但对于第二个表,代码将执行但数据不会从数据库中删除。

我认为发生的事情是,由于我使用相同的代码从两个表中删除,所以只有 1 个有效。 (我想我不确定)在查看了一段时间后,试图找出我犯的潜在错误并试图查看其他可能的问题,我决定向您寻求帮助!

这里是代码:

<?php


$config['conn'] = array(
'host' => 'localhost',
'username' => 'root',
'password' => '',
'dbname' => 'inventarisdb'
);

$conn = new PDO('mysql:host=' . $config['conn']['host'] . ';dbname=' . $config['conn']['dbname'], $config['conn']['username'], $config['conn']['password']);


$action = isset($_GET['action']) ? $_GET['action']: "";

if($action=='delete'){ //if the user clicked ok, run our delete query
try {

$query = "DELETE FROM BCD WHERE id = ?";
$stmt = $conn->prepare($query);
$stmt->bindParam(1, $_GET['id']);

$result = $stmt->execute();
echo "<div>Record was deleted.</div>";

}catch(PDOException $exception){ //to handle error
echo "Error: " . $exception->getMessage();
}

}

//select all data
$query = "SELECT ID, Categorie, SerieNummer, MacAdress, ProductCode, Prijs, RekNummer, PaletNummer, Hoeveelheid, Aantekeningen FROM BCD";
$stmt = $conn->prepare( $query );
$stmt->execute();

//this is how to get number of rows returned
$num = $stmt->rowCount();



if($num>0){ //check if more than 0 record found

echo "<table border='1'>";//start table

//creating our table heading
echo "<tr>";
echo "<th>Categorie</th>";
echo "<th>SerieNummer</th>";
echo "<th>MacAdress</th>";
echo "<th>ProductCode</th>";
echo "<th>Prijs</th>";
echo "<th>RekNummer</th>";
echo "<th>PaletNummer</th>";
echo "<th>Hoeveelheid</th>";
echo "<th>Aantekeningen</th>";
echo "</tr>";

//retrieve our table contents
//fetch() is faster than fetchAll()
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
//extract row
//this will make $row['firstname'] to
//just $firstname only
extract($row);

//creating new table row per record
echo "<tr>";
echo "<td>{$Categorie}</td>";
echo "<td>{$SerieNummer}</td>";
echo "<td>{$MacAdress}</td>";
echo "<td>{$ProductCode}</td>";
echo "<td>{$Prijs}</td>";
echo "<td>{$RekNummer}</td>";
echo "<td>{$PaletNummer}</td>";
echo "<td>{$Hoeveelheid}</td>";
echo "<td>{$Aantekeningen}</td>";
echo "<td>";


//we will use this links on next part of this post
echo "<a href='#' onclick='delete_user( {$ID} );'>Delete</a>";
echo "</td>";
echo "</tr>";
}

echo "</table>";//end table

}else{ //if no records found
echo "No records found.";
}

?>

<script type='text/javascript'>

function delete_user( id ){
var answer = confirm('Are you sure?');
if ( answer ){ //if user clicked ok
//redirect to url with action as delete and id to the record to be deleted
window.location = 'Remove.php?action=delete&id=' + id;
}
}
</script>

<br/><br/><br/><br/><br/><br/><br/><br/><br/>



<?php


$config['conn'] = array(
'host' => 'localhost',
'username' => 'root',
'password' => '',
'dbname' => 'inventarisdb2'
);

$conn = new PDO('mysql:host=' . $config['conn']['host'] . ';dbname=' . $config['conn']['dbname'], $config['conn']['username'], $config['conn']['password']);


$action = isset($_GET['action']) ? $_GET['action']: "";

if($action=='delete'){ //if the user clicked ok, run our delete query
try {

$query = "DELETE FROM CDE WHERE id = ?";
$stmt = $conn->prepare($query);
$stmt->bindParam(1, $_GET['id']);

$result = $stmt->execute();
echo "<div>Record was deleted.</div>";

}catch(PDOException $exception){ //to handle error
echo "Error: " . $exception->getMessage();
}

}

//select all data
$query = "SELECT ID2, Klant, Categorie1, SerieNummer1, MacAdress1, ProductCode1, Prijs1, Hoeveelheid1, Aantekeningen1 FROM CDE";
$stmt = $conn->prepare( $query );
$stmt->execute();

//this is how to get number of rows returned
$num = $stmt->rowCount();



if($num>0){ //check if more than 0 record found

echo "<table border='1'>";//start table

//creating our table heading
echo "<tr>";
echo "<th>Klant</th>";
echo "<th>Categorie1</th>";
echo "<th>SerieNummer1</th>";
echo "<th>MacAdress1</th>";
echo "<th>ProductCode1</th>";
echo "<th>Prijs1</th>";
echo "<th>Hoeveelheid1</th>";
echo "<th>Aantekeningen1</th>";
echo "</tr>";

//retrieve our table contents
//fetch() is faster than fetchAll()
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
//extract row
//this will make $row['firstname'] to
//just $firstname only
extract($row);

//creating new table row per record
echo "<tr>";
echo "<td>{$Klant}</td>";
echo "<td>{$Categorie1}</td>";
echo "<td>{$SerieNummer1}</td>";
echo "<td>{$MacAdress1}</td>";
echo "<td>{$ProductCode1}</td>";
echo "<td>{$Prijs1}</td>";
echo "<td>{$Hoeveelheid1}</td>";
echo "<td>{$Aantekeningen1}</td>";
echo "<td>";

//we will use this links on next part of this post
echo "<a href='#' onclick='delete_user( {$ID2} );'>Delete</a>";
echo "</td>";
echo "</tr>";
}

echo "</table>";//end table

}else{ //if no records found
echo "No records found.";
}

?>

<script type='text/javascript'>

function delete_user( id ){
var answer = confirm('Are you sure?');
if ( answer ){ //if user clicked ok
//redirect to url with action as delete and id to the record to be deleted
window.location = 'Remove.php?action=delete&id=' + id;
}
}
</script>

简而言之:第一个表:一切正常,所有数据都被删除 第 2 个表:似乎可以正常工作,但在确认删除后,数据仍然存在并且没有从我的数据库中删除。

表2的代码与表1的代码完全相同,除了DB和表的名称等。

我希望您能查看我的代码,看看您是否注意到任何可能导致此问题的原因。也许如果你同意我的想法,相同的代码不适用于同一页面上的两个表,你可以举一个例子或一个链接来说明我如何解决这个问题?对不起,长代码!先谢谢你!

最佳答案

你不应该混合删除,因为你可能有一个 id 与另一个相同的实例,所以你会删除错误的东西。但我不认为这是你的主要问题:

你的选择是

SELECT ID2, Klant, Categorie1, SerieNummer1, MacAdress1, ProductCode1, Prijs1, Hoeveelheid1, Aantekeningen1 FROM CDE

但是你的删除是:

DELETE FROM CDE WHERE id = ?";

你要删除的应该是:

DELETE FROM CDE WHERE ID2 = ?";

防止误删:

这里最简单的事情就是更改您要删除用户的 JavaScript 以接受操作参数并指定要执行的删除操作,因为这两个删除尝试现在都在运行。

JavaScript

您不需要在同一页面上使用 JavaScript 两次。只需在您的 HEAD 或正文末尾之前使用一次即可。

<script type='text/javascript'>
function delete_user( action, id ){
var answer = confirm('Are you sure?');
if ( answer ){ //if user clicked ok
//redirect to url with action as delete and id to the record to be deleted
window.location = 'Remove.php?action=' + action + '&id=' + id;
}
}
</script>

检查操作

if ($action=='delete_BCD') { 
// or
if ($action=='delete_CDE') {

渲染行

echo "<a href='#' onclick='delete_user( \"delete_BCD\", {$ID2} );'>Delete</a>";
// or
echo "<a href='#' onclick='delete_user( \"delete_CDE\", {$ID2} );'>Delete</a>";

关于php - 从我页面的第二个表中删除行的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18740611/

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