Jump to content

update data from different table


Ronel
 Share

Recommended Posts

<?php 
$con = new mysqli("localhost","root","","loginsystem");

$errors = array();
if($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['updatestock'])):
/*
echo "<pre>";
print_r($_POST);	 
echo "</pre>";
*/
	$fields = array(	
		 "itemname" => "Item Name"
	    ,"purchaseid" => "Purchase ID"	
	    ,"poqty" => "Purchase Order QTY"
	    
	); 
	
	foreach($fields as $field => $label):
		if(empty($_POST[$field])): 
			array_push($errors,"Please Enter ".$label);
		endif;
	endforeach;
	
	if(empty($errors)):
		$sqlstockid = "SELECT `id` FROM `stock_orders` WHERE stockid = ? LIMIT 1"; 
		$querystockid = $con->prepare($sqlstockid);	
		$querystockid->bind_param("i", $_POST['stockid']); 
		$querystockid->execute(); 
		$resultstockid = $querystockid->get_result();	
		$rowstockid = $resultstockid->fetch_assoc();

		if(!empty($rowstockid)){ 
			array_push($errors,"This Purchase ID already exists");
		}
	endif;		
	
	
	if(empty($errors)):	
	
		$today = date('Y-m-d');
		
		$sqlstockorder = "INSERT INTO `stock_orders`(`id`, `stockid`, `poqty`, `remarks`) 
		VALUES (?,?,?,?)";
		
		$querystockorder = $con->prepare($sqlstockorder);		
		$querystockorder->bind_param("iiis", $_POST['id'], $_POST['stockid'], $_POST['poqty'], $remarks); 		
		$querystockorder->execute();
	
	
		$sqlstock = "UPDATE `stock` SET 
		`stockqty` =  ABS(COALESCE(stockqty,0)+?)		
		WHERE `id` = ?";
		$querystock = $con->prepare($sqlstock);		
		$querystock->bind_param("ii", $_POST['poqty'], $_POST['id']); 		
		$querystock->execute();
		$num = $con -> affected_rows;			
		$message = (!empty($num) ? "Stock Updated" : 'No Changes Made');	
		
		header("refresh: 3; URL=edit.php");
	endif;
	
endif;

$sql = "SELECT `itemname` FROM `stock`";
$res = mysqli_query($con, $sql);
?>


<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script type="text/javascript" src="fetch.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js"></script>
    </head>
<body>
	
        
<?php	

if(!empty($message)):
	echo '<div style="width:500px; text-align: center; margin:20px auto;">'."\r";
		echo '<span style="color:green"><b>'.$message.'</b></span></br >'."\r";
	echo '</div>'."\r";
endif;
if(!empty($errors)):
	echo '<div style="width:500px; text-align: center; margin:20px auto;">'."\r";
	foreach($errors as $error):
		echo '<span style="color:red">'.$error.'</span></br >'."\r";
	endforeach;
	echo '</div>'."\r";
endif;



			
if($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['itemname'])){

		echo '<form action="" method="post">
			<table class="table table-bordered">
				<thead class="table-primary" style="white-space:nowrap;">
				<th>Stock ID</th>	 
				<th>Item Name</th>
				<th>Stock Quantity</th>
				<th>Remarks</th>				
				<th>Update Stocks</th>
				</thead>'."\r";	
		
			$sql = "select `id`, `itemname` from stock where itemname = ? "; 
			$query = $con->prepare($sql);		
			$query->bind_param("s",$_POST['itemname']); 		
			$query->execute();
			$result = $query->get_result();
			while($rows = $result->fetch_assoc()){	
			
				//$purchaseid = (!empty($_POST['purchaseid']) ? $_POST['purchaseid'] : '');
				//$poqty = (!empty($_POST['poqty']) ? $_POST['poqty'] : '');
			
				echo '<tr>
					<td><input style="width:100%" type="text" name="id" value="'.$rows['id'].'" readonly /></td>
					<td><input style="width:100%" type="text" name="itemname" value="'.$rows['itemname'].'" readonly /></td>
					<td><input style="width:100%" type="text" name="stockqty" value="'.$stockqty.'" /></td>
					<td><input style="width:100%" type="text" name="remarks" value="'.$remarks.'" /></td>					
					<td><input style="width:100%" type="submit" name="updatestock" value="Update" /></td>
				</tr>'."\r";
			}
		echo '</table>
	</form>'."\r";
	         
}else{ 
	echo '<b class="d-inline p-2 bg-primary text-white">Update Stock Here</b><hr>    
	<b>Select Item to update :</b> 
    <select id="itemname" onchange="selectItem()">
        <option value="Select Item here" selected>Select Item here</option>'."\r";
	
		while ($rows = mysqli_fetch_array($res)){		 
			echo '<option value="'.$rows['itemname'].'">'.$rows['itemname'].'</option>'."\r";
		}
		   
    echo '</select><br><br>'."\r";
	echo '<div id="ans"></div>';
}
?>
	
	<script type="text/javascript">
		function selectItem(){
			    var x = document.getElementById("itemname").value;
			    
			    $.ajax({
			       url:"edit.php",
			        method: "POST",
			        data:{
			            itemname : x
			        },
	        success:function(DataView){
	        $("#ans").html(DataView);
	    }
  
});
		}
	</script>
    </body>
</html>

below is my two different tables stock and stock_orders

save.png.520f880078a2cdd593e84c38b8e2b691.pngUntitled.thumb.png.4b389d2a9347cfdb5d1a7c4b95ac3c6c.png

Link to comment
Share on other sites

Posted (edited)

sir i want to update my tables i have tried using these codes but not being able to update! i have two tables stock and stock_orders can you help me 

Edited by Ronel
Link to comment
Share on other sites

Have you added any debugging code?  Something that verifies that you are not creating any errors?  LIke when you do a prepare do you check the result?  When you run the query do you check if it ran?  Do you have error checking enabled and turned on?  All important things when you are having problems to solve.  Echo out the results of things when you don't think you are getting the right answers.   All part of being a good coder.

Link to comment
Share on other sites

You call my comments a solution?  Why?  All I did was give you some pointers on how to resolve your issue maybe.  At least it will give you some direction I hope.  It may not be the 'solution' though.   :)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.