PHP SELECT INSERT数据(x)从表A到B,然后防止数据(x)出现在SELECT表A上

标签 php mysql

我有 2 个 mySQL 表:

A)`inventory` (`id`, `item`, `qty_left`, `qty_min`, `qty_max`, `cat_no`, `supplier`) VALUES
(1, 'Orange', 8, 10, 50, 1001, 'ACOMP'),
(2, 'Apple', 4, 10, 20, 1002, 'BCOMP'),
(3, 'Pear', 80, 20, 100, 1003, 'ACOMP'),
(4, 'Durian', 9, 60, 100, 1004, 'CCOMP');

B)`reorder_in_process` (`id`, `item`, `to_order`, `cat_no`, `supplier`, `time`) VALUES
(EMPTY);

我有 PHP 页面:(purchaser.php)

<!---------------Ajax Script To Confirm Purchase From Clicked Supplier------------>
<script>
  function tCat(value) {
          xmlhttp = new XMLHttpRequest();
          xmlhttp.onreadystatechange = function() {
              if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
  				document.getElementById("reorder_in_process").innerHTML = xmlhttp.responseText;
  																		}
          											 }
          xmlhttp.open("GET","purchaser_confirm_purchase.php?q="+value,true);
          xmlhttp.send();
      				 }
  </script>
  
<style>

#left_col {
	float: left;
	width: 33%;}
#mid_col {
	float: right;
	width: 33%;}
#reorder_in_process {
	float: none;
	width: 100%;}
#supplier {
	float: none;}	
</style>


<div id="left_Col">
<div>Items Need Reorder</div>
<?PHP
include ('db.php');
$result = mysqli_query($con, "SELECT * FROM inventory  WHERE qty_left<=qty_min ORDER BY supplier ASC, item ASC");
$first_iteration = true;
$current_supplier = null;
while($row = mysqli_fetch_assoc($result)){
$to_order = $row['qty_max']-$row['qty_left'];		
// If a new supplier is encountered, close the first table (if its no the first iteration) and add new header + start new table 
    if ($row['supplier'] != $current_supplier) {
        if ($first_iteration == false)
            echo "</table></div>";
        $i = 1;
        $first_iteration = false;
        $current_supplier = $row['supplier'];
        echo "<h4>".$row['supplier']."<button id='supplier' value=". $row['supplier']." onClick='tCat(value)'>Reorder Now</button></h4>";
        echo  "<div><table><tr>
                    <th>No</th>
                    <th>Item</th>
                    <th>Cat. No</th>                                            
                    <th>Buy QTY</th>
                    <th>Supplier</th>                   
                </tr>";
    }
    echo "<tr>
                <td>".$i. "</td>
               <td>".$row['item']."</td>
               <td>".$row['cat_no']."</td>
               <td>".$to_order."</td>
               <td>".$row['supplier']."</td>      
         </tr>";
    $i++;
}
echo "</table></div>";
?>

<div id="try"></div>
</div>
<div id="mid_col">
<div>Reorder In Process</div>
<div id="reorder_in_process">
<?PHP
include ('db.php');
$result = mysqli_query($con, "SELECT * FROM reorder_in_process ORDER BY supplier");
$first_iteration = true;
$current_supplier = null;

while($row = mysqli_fetch_assoc($result)){
    // If a new supplier is encountered, close the first table (if its no the first iteration) and add new header + start new table 
    if ($row['supplier'] != $current_supplier) {
        if ($first_iteration == false)
            echo "</table></div>";

        $i = 1;
        $first_iteration = false;
        $current_supplier = $row['supplier'];
        echo "<h4>".$row['supplier']."</h4>";
        echo  "<div><table><tr>
                    <th>No</th>
                    <th>Item</th>
                    <th>Cat. No</th>                                            
                    <th>Buy QTY</th>
                    <th>Supplier</th>
					  <th>Ordered Time</th>                   
                </tr>";
    }
    echo "<tr>
                <td>".$i. "</td>
               <td>".$row['item']."</td>
               <td>".$row['cat_no']."</td>
               <td>".$row['to_order']."</td>
			    <td>".$row['supplier']."</td>
               <td>".$row['time']."</td>      
         </tr>";
    $i++;
}
echo "</table></div>";
?>
</div>
</div>

然后我有另一个页面:(purchaser_confirm_purchase.php)

<?php
include ('db.php');
$q = strval($_GET['q']);

$sql2 = "SELECT * FROM inventory WHERE qty_left<=qty_min AND supplier='$q'";
$result2 = mysqli_query($con,$sql2);
while($row2 = mysqli_fetch_array($result2)) {
$item = $row2['item'];
$cat_no = $row2['cat_no'];
$qty_max = $row2['qty_max'];
$qty_left = $row2['qty_left'];
$to_order = $row2['qty_max']-$row2['qty_left'];

$qql = "INSERT INTO reorder_in_process VALUES (NULL, '$item', '$to_order', '$cat_no', '$q', now())";
$result_qql = mysqli_query($con,$qql);

}

//-----------------Update reorder_in_process to the PHP page if any new insert.--------------------//

$result = mysqli_query($con, "SELECT * FROM reorder_in_process ORDER BY supplier");
$first_iteration = true;
$current_supplier = null;

while($row = mysqli_fetch_assoc($result)){
    // If a new supplier is encountered, close the first table (if its no the first iteration) and add new header + start new table 
    if ($row['supplier'] != $current_supplier) {
        if ($first_iteration == false)
            echo "</table></div>";

        $i = 1;
        $first_iteration = false;
        $current_supplier = $row['supplier'];
        echo "<h4>".$row['supplier']."</h4>";
        echo  "<div><table><tr>
                    <th>No</th>
                    <th>Item</th>
                    <th>Cat. No</th>                                            
                    <th>Buy QTY</th>
                    <th>Supplier</th>
					  <th>Ordered Time</th>                   
                </tr>";
    }

    echo "<tr>
                <td>".$i. "</td>
               <td>".$row['item']."</td>
               <td>".$row['cat_no']."</td>
               <td>".$row['to_order']."</td>
               <td>".$row['supplier']."</td>
			    <td>".$row['time']."</td>      
         </tr>";
    $i++;
}
echo "</table></div>";
?>

(purchaser.php)页面将输出以下结果:

Items Need Reorder                                     Reorder In Process

ACOMP     <Button"Reorder Now">

No  Item    Cat. No Buy QTY Supplier
1   Orange  1001        42  ACOMP

BCOMP     <Button"Reorder Now">

No  Item    Cat. No Buy QTY Supplier
1   Apple   1002        16  BCOMP

CCOMP     <Button"Reorder Now">

No  Item    Cat. No Buy QTY Supplier
1   Durian  1004        91  CCOMP

现在,如果我单击“重新排序按钮”,它会输出:

Items Need Reorder                                 Reorder In Process

ACOMP     <Button"Reorder Now">             ACOMP

No  Item    Cat. No Buy QTY Supplier        No  Item    Cat. No Buy QTY Supplier    Ordered Time
1   Orange  1001        42  ACOMP           1   Orange  1001        42  ACOMP   2017-06-09 23:06:42

BCOMP     <Button"Reorder Now">

No  Item    Cat. No Buy QTY Supplier
1   Apple   1002        16  BCOMP

CCOMP     <Button"Reorder Now">

No  Item    Cat. No Buy QTY Supplier
1   Durian  1004        91  CCOMP

但是我想要的是点击后的结果:

Items Need Reorder                                 Reorder In Process

BCOMP     <Button"Reorder Now">             ACOMP

No  Item    Cat. No Buy QTY Supplier        No  Item    Cat. No Buy QTY Supplier    Ordered Time
1   Apple   1002        16  BCOMP           1   Orange  1001        42  ACOMP   2017-06-09 23:06:42


CCOMP     <Button"Reorder Now">

No  Item    Cat. No Buy QTY Supplier
1   Durian  1004        91  CCOMP

即使在页面重新加载/刷新后,它仍然保持不变。

请帮忙。谢谢。

最佳答案

在 UI 中加载第一个表格:

Select * from table1 t1 left join table2 t2 on t1.id=t2.id
where t2.id is null

即使刷新后,它也会在两个表中保留正确的值。

如果 table2 填充了页面加载,则无需担心渲染/隐藏。

但是,如果您通过 AJAX 执行此操作,正如您所说,您可以通过与其他行不同的 rowid 或 class 删除 DOM 元素。最终必须使用第二个表中的 DOM 元素创建相同的值。

关于PHP SELECT INSERT数据(x)从表A到B,然后防止数据(x)出现在SELECT表A上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44458385/

相关文章:

php - 在 PHP 中执行依赖于变量的 SQL 查询

php - 复选框在 javascript/php 中始终返回 true

php - 使用 Codeigniter 和 REST API 服务找不到类 'REST_Controller'

mysql - 如何在node.js View 中执行MySQL查询?

php - JQuery-Tabledit 一次只更新一列(我的代码有什么问题)

python - 安装 MySQL Python(在 Windows 7 上)

javascript - MYSQL 数据插入查询不起作用

php - 选择查询未正确返回

MySQL滞后/领先功能?

python - 唯一增量及其表宽最大值 + 1