PHP SELECT INSERT data(x) from table A to B, then prevent data(x) appear on SELECT table A

时间:2017-06-09 12:53:47

标签: php mysql

I have 2 mySQL Tables:

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);

I have PHP page: (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>

Then I have another page: (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>";
?>

The (purchaser.php) page will output results below:

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

Now if I click the "Reorder Button", it outputs:

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

But What I want is this after click:

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

And it stays tat way even after page reload/refresh.

Pls help. Thanks.

2 个答案:

答案 0 :(得分:1)

在UI中加载第一个表:

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

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

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

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

答案 1 :(得分:0)

最后我自己找到了解决方案:

"SELECT * FROM inventory  
          WHERE qty_left<=qty_min 
                AND NOT EXISTS (SELECT cat_no FROM reorder_in_process WHERE cat_no=inventory.cat_no)
          ORDER BY supplier ASC, item ASC");