gpt4 book ai didi

php - 如何设置代码以在将行复制到表2后自动从表1中删除行

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

我设置 php 代码通过单击 html 按钮将特定行从 table1 复制到 table2。现在我希望在复制到 table2 时自动从 table1 中删除该特定行。在复制行的同一个 php 文件上,我想要有一组辅助代码,应在复制功能完成后生成。请给我关于如何构建该结构的想法...

/*This files View data from table "rfid_add" & contains php codes and form to INSERT specific row to another table "approved" */
<html>
<head>

<link rel="stylesheet" type="text/css" href="view_style.css">
<meta http-equiv="refresh" content="60">
<title> Request for card </title>
</head>

<body>
<div id="main">
<center>
<h1> Request for RFID Keyfob </h1>
<table border=1 cellpadding=1 cellspacing=1>
<tr>
<th>SL </th>
<th>Applicant Name </th>
<th>Flat No </th>
<th>Previous Card </th>
<th>Gender </th>
<th>Reason</th>
<th> Auth </th>
<th> Request Date</th>
<th> Action</th>
</tr>

<?php
// Create Connection
$conn = mysqli_connect('localhost', 'root', '');

//Select DB
if(!mysqli_select_db($conn, 'lock_access'))
{ echo "Database not selected"; }
else {
echo "";}


//Select Query
$sql = "SELECT * FROM rfid_add";

//Execute the SQL query
$records = mysqli_query($conn,$sql);

while ($row = mysqli_fetch_array($records))
{
$idnt = $row['id'];
$fln = $row['full_name'];
$fnum= $row['flat_number'];
$prid= $row['previous_id'];
$gndr= $row['gender'];
$rsn = $row['reason'];
$atcd= $row['auth_code'];
$cdate= $row['date'];



// View Table data
echo "<tr>";
echo "<td> $idnt </td>";
echo "<td> $fln </td>";
echo "<td> $fnum </td>";
echo "<td> $prid </td>";
echo "<td> $gndr </td>";
echo "<td> $rsn </td>";
echo "<td> $atcd </td>";
echo "<td> $cdate </td>";

// These form appears to copy viewed file
echo "<td> <form action='approve-insert.php' method='POST'>

<input type='hidden' name='full_name' value='$fln'>
<input type='hidden' name='flatno' value='$fnum'>
<input type='hidden' name='gender' value='$gndr'>
<input type='text' name='pcardno' value=''>
<input type='submit' name='submit' value='Approve'>


</form></td>";
echo "</tr>";
}


?>

</table>
</center>
</div>
</body>

</html>
// These files contains php codes to execute copy to table "approved"
<?php

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "lock_access";

$conn = mysqli_connect($servername, $username, $password);

if(!$conn)
{ echo 'Not connected';
}
if(!mysqli_select_db($conn,$dbname))
{
echo 'DB Not selected';
}

$fn = $_POST['full_name'];
$fln = $_POST['flatno'];
$gn = $_POST['gender'];
$pcn = $_POST['pcardno'];



$sql = "INSERT INTO approved (full_name, flat_number, gender, new_card) VALUES('$fn', '$fln', '$gn', '$pcn')";


if(!mysqli_query($conn, $sql))
{
echo "Application not received. ";
}
else {
echo "<center>
<div style='margin-top:400px; margin-bottom:10%'>
<p style='color:black; font-size:30px;'> Application from <b> $fn </p> </b> <h2>is </h2>
<p style='color:green; font-size:40px;'> APPROVED. </p>
<p style='color:red; font-size:30px;'>Did you enroll the card? </p>
</center> </div>" ;
}

header("refresh:1000; url=view-update.php");
?>

最佳答案

如果 id 字段是第一个表中行的唯一 ID,请将其传递到您的 approve-insert.php 文件中,就像处理其他数据一样,并在成功运行插入查询后,以相同的方式运行另一个查询以从第一个表中删除,其中 id = 您传入的 id。

事实上,我会进一步更改代码,以便不发送要插入的数据,而只发送 id。然后在approve-insert中,从第一个表中检索数据,将其插入到第二个表中,然后当您知道插入已成功时从第一个表中删除。

或者,如果两个表在其他方面相同,则只需设置一个“已批准” bool 列,这样就无需复制/删除数据。

关于php - 如何设置代码以在将行复制到表2后自动从表1中删除行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58131595/

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