i have a database 'phar' which need to subract its values 'stk' through the request made and gets updated to the db. i used arrays to do subraction.. but not able to store back the values to the db in its position.. my calculations work perfect, but not able to store back..pls help..

$con=mysqli_connect("localhost","root","","ivf");

if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

echo " <form action = 'ivf.html' method= 'POST'>";
if(isset($_POST['submit']))
{
$mrd = trim($_POST['mrd']);
$pro= trim($_POST['pro']);
echo "$pro";
echo "<br><br>";

$qu = $_POST['qu'];
print_r ($_POST['qu']);
echo "<br><br>";

$stk = $_POST['stk'];
print_r ($_POST['stk']);
echo "<br><br>";

$res  = array();
for($i=0;$i<count($stk);$i++){
   $res[$i] = $stk[$i]-$qu[$i];

$result=mysqli_query($con,"UPDATE phar  SET $stk = '$res[$i]' WHERE pro = '$pro'");
}
print_r($res);

}


echo "<input type='submit' name='submit' value='BACK' ></count>";
echo " </form>";
mysqli_close($con);
?>

Make certain your query string is building correctly, check for erros and based on This You might need to free the results.

Change to free results:

for ($i = 0; $i < count($stk); $i++)
{
    $res[$i] = $stk[$i] - $qu[$i];
    $result = mysqli_query($con, "UPDATE phar SET $stk = '$res[$i]' WHERE pro = '$pro'");
    mysqli_free_result($result);
}

Additional note: Looking at your query: the where clause would cause everything that matches 'pro' to be set to whatever the last entry of $res is.

why is it giving "mysqli_free_result() expects parameter 1 to be mysqli_result, boolean given in..." ??
also the db is getting updates the same value for all rows where pro='$pro'
i need the array values to get updates to the right positions..

Might not need the mysqli_free_result() for an update query, might only be tied to select queries.

I don't know your exact dataset, or datamodel, but I suspect the query would be closer to this:

$result = mysqli_query($con, "UPDATE phar SET $stk = '$res[$i]' WHERE pro = '$pro' and $stk = '$stk[$i]'");

This would tie back to the original 'stk' before the subtraction was made.

:( my positioning in db is not working.. thanks for the help

one more help pls..
why is my php not displaying full string data??its only showing first word of the data..

if(isset($_POST['submit']))
{
$mrd = $_POST['mrd'];

$result = mysqli_query($con,"SELECT * FROM phar  
JOIN ivf  
ON phar.pro = ivf.pro  
WHERE phar.pro= ivf.pro AND mrd = '$mrd'");
while($row=mysqli_fetch_array($result))  
{ 

echo "<td align='center'><input type='text' style='text-align: center;' name='inme[]'value=".$row['iname']."></td>"; 

Hmm, it only prints the 'echo' on line 12 once?

If you run the exact query on its own, how many results are you expecting? Is there anything else in the while loop after the echo?