Jump to content

update data from different table


Recommended Posts

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

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

			array_push($errors,"This Purchase ID already exists");
		$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); 		
		$sqlstock = "UPDATE `stock` SET 
		`stockqty` =  ABS(COALESCE(stockqty,0)+?)		
		WHERE `id` = ?";
		$querystock = $con->prepare($sqlstock);		
		$querystock->bind_param("ii", $_POST['poqty'], $_POST['id']); 		
		$num = $con -> affected_rows;			
		$message = (!empty($num) ? "Stock Updated" : 'No Changes Made');	
		header("refresh: 3; URL=edit.php");

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

<!DOCTYPE html>
    <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>

	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";
	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";
	echo '</div>'."\r";

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>Update Stocks</th>
			$sql = "select `id`, `itemname` from stock where itemname = ? "; 
			$query = $con->prepare($sql);		
			$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>
		echo '</table>
	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;
			        method: "POST",
			            itemname : x

below is my two different tables stock and stock_orders


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.

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.


  • 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.