Jump to content

help with basic query


gammaman

Recommended Posts

what is wrong with this Update query

 

<?php

  $conn=mysql_connect("localhost","fierm","13183");

  if(!$conn){
   echo "failed";
}else{
   mysql_select_db(fierm);

   $Amt=$_POST['amt'];
   $Item=$_POST['new'];
   
   echo "$Amt";
   echo "$Item";

   $result=mysql_query("select InventoryLevel,ProductID from Inventory where ProductID='$Item' and '$Amt' < InventoryLevel");  
   $cou=mysql_num_rows($result);
   echo "$cou";

   if ($cou > 0)
   {
     mysql_query("Update table Inventory set InventoryLevel = InventoryLevel-'$Amt' 
                   Where ProductID='$Item'"); 

   }



}

?>

Link to comment
https://forums.phpfreaks.com/topic/103030-help-with-basic-query/
Share on other sites

First u can echo variable without double quotes: echo $amt;

 

Second, as stated above theres no need to: "UPDATE table Inventory", just "UPDATE Inventory".

 

Third im not sure about "set InventoryLevel = InventoryLevel-'$Amt'". U should calculate the difference in php and then update the db.

You are right, I can't believe I missed that one.

 

Will something like I have in the clause of this if stament work.

<?php

 $conn=mysql_connect("localhost","fierm","13183");

 if(!$conn){
  echo "failed";
}else{
  mysql_select_db(fierm);

  $Amt=$_POST['amt'];
  $Item=$_POST['new'];
  
  echo "$Amt";
  echo "$Item";

  $result=mysql_query("select InventoryLevel, ProductID from Inventory where ProductID='$Item'");  
  $cou=mysql_num_rows($result);
  
  echo "$cou";

  if (InventoryLevel > $Amt )
  {
    echo "<br/>";
    echo "1";
    mysql_query("Update  Inventory set InventoryLevel = InventoryLevel-'$Amt' 
                  Where ProductID='$Item'"); 

  }



}
?>

Archived

This topic is now archived and is closed to further replies.

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