Ronel Posted March 9, 2022 Share Posted March 9, 2022 <?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 Quote Link to comment https://forums.phpfreaks.com/topic/314589-update-data-from-different-table/ Share on other sites More sharing options...
ginerjm Posted March 9, 2022 Share Posted March 9, 2022 What is the question here? Quote Link to comment https://forums.phpfreaks.com/topic/314589-update-data-from-different-table/#findComment-1594343 Share on other sites More sharing options...
Ronel Posted March 9, 2022 Author Share Posted March 9, 2022 (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 March 9, 2022 by Ronel Quote Link to comment https://forums.phpfreaks.com/topic/314589-update-data-from-different-table/#findComment-1594344 Share on other sites More sharing options...
ginerjm Posted March 9, 2022 Share Posted March 9, 2022 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. Quote Link to comment https://forums.phpfreaks.com/topic/314589-update-data-from-different-table/#findComment-1594345 Share on other sites More sharing options...
Ronel Posted March 9, 2022 Author Share Posted March 9, 2022 okay thanks for the solution Quote Link to comment https://forums.phpfreaks.com/topic/314589-update-data-from-different-table/#findComment-1594346 Share on other sites More sharing options...
ginerjm Posted March 9, 2022 Share Posted March 9, 2022 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. Quote Link to comment https://forums.phpfreaks.com/topic/314589-update-data-from-different-table/#findComment-1594347 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.