How to fetch updated data from temporary table to display in php?

I have got a save edit button. After I update the amount of package through all the textboxes, I select the checkbox and clicked on the save edit button the changes will be saved to the temporary table. In the next page, it will display the data and user can print out the updated data.

<?php
if(!empty($_POST['pro']))
{
	if(isset($_POST['printpreview1']))
	{
		$sdate=$_POST['sdate'];
		$txt0101=$_POST['txt0101'];
		$txt0102=$_POST['txt0102'];
		$txt0103=$_POST['txt0103'];
		$txt0104=$_POST['txt0104'];
		$txt0105=$_POST['txt0105'];
		$txt0106=$_POST['txt0106'];
	}
	
	$db=mysqli_connect("localhost","root","","vegetable");
	$sql1="CREATE TEMPORARY TABLE tsalmast AS (SELECT pdesc,custarea,weight,packet FROM salmast WHERE sdate='".$sdate."' ORDER BY location ASC)";
	$sql2="UPDATE tsalmast SET packet='".$txt0101."' WHERE sdate='".$sdate."' AND location='0101'";
	$sql3="UPDATE tsalmast SET packet='".$txt0102."' WHERE sdate='".$sdate."' AND location='0102'";
	$sql4="UPDATE tsalmast SET packet='".$txt0103."' WHERE sdate='".$sdate."' AND location='0103'";
	$sql5="UPDATE tsalmast SET packet='".$txt0104."' WHERE sdate='".$sdate."' AND location='0104'";
	$sql6="UPDATE tsalmast SET packet='".$txt0105."' WHERE sdate='".$sdate."' AND location='0105'";
	$sql7="UPDATE tsalmast SET packet='".$txt0106."' WHERE sdate='".$sdate."' AND location='0106'";	
	$sql8="SELECT * FROM tsalmast";
		
	$result1=mysqli_query($db,$sql1);
	$result2=mysqli_query($db,$sql2);
	$result3=mysqli_query($db,$sql3);
	$result4=mysqli_query($db,$sql4);
	$result5=mysqli_query($db,$sql5);
	$result6=mysqli_query($db,$sql6);
	$result7=mysqli_query($db,$sql7);
	$result8=mysqli_query($db,$sql8) or die("Error: ".mysqli_error($db));
	
	while ($row=mysqli_fetch_array($result8))
	{
		if($row['packet'] != 0)
		{
			for($i=0;$i<$row['packet'];$i++)
			{
				echo"<form action='' method='post'>";

				echo"<table style='border-collapse: collapse; text-align: left;' width='278' height='170' border='1'>";
				echo"<br><br>";
				echo"<tr>";
					echo"<th width='310' height='63' rowspan='1'>&nbsp;".$row['pdesc']."</th>";
					echo"<th width='174' rowspan='1'>&nbsp;".$row['weight']." KG</th>";
				echo"</tr>";
				echo"<tr>";
				echo"<th colspan = '2'><p>&nbsp;Name 名字:</p>";
				echo"<p>".$row['custarea']."</p></th>";
				echo"</tr>";
				echo"</table>";
				echo"<br><br><br>";
			}
		}
	}
}
else
{
	echo "None<br>";
}
?>

I can’t quite see what the question is here. Is there a part of your code that isn’t working, or not doing what you want it to do? If so, which part? I notice you run several queries to create and update a temporary table, but you only check that the last one has run properly - if you are having problems with that side of it, would adding error checks on the other queries help narrow it down?

@droopsnoot The code is not doing what I want to do. The result display is from $sql1 and $sql8, the process have ignored $sql2 - $sql7

One thing jumps out at me, based on that extra information. You create a temporary table:

$sql1="CREATE TEMPORARY TABLE tsalmast AS (SELECT pdesc,custarea,weight,packet FROM salmast WHERE sdate='".$sdate."' ORDER BY location ASC)"

and then try to update the data in the temporary table:

$sql2="UPDATE tsalmast SET packet='".$txt0101."' WHERE sdate='".$sdate."' AND location='0101'";

But the temporary table you created only contains the columns called pdesc, custarea, weight and packet, so your other query conditions will either (a) never be true because you’re comparing the ‘location’ and ‘sdate’ columns that don’t exist in the temporary table, or more likely (b) will fail with an SQL error for that reason.

A check on each query when you run it would show those errors, I think.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.