0

I am new in PHP world, basically what I want to do is divide two columns and set the result automatically in a third column.

In my table 'Building' I have columns 'Selling' and 'Size'. I want to divide those two to get the Sell price per Sqm and set it in the column 'SqmPrice'.

Like this every time I input the price and the size it calculates it automatically and displays it in my web page.

Daniël Knippers
  • 3,049
  • 1
  • 11
  • 17
Mushroom
  • 285
  • 2
  • 17

2 Answers2

3

you have two option

Option 1: create a trigger that set the column to use the formula you assigned. example

delimiter ~

create trigger my_table_insert before insert on tablename
for each row begin
    set new.sqmprice = new.Selling / new.Size; 
end~

delimiter ;

Option2: Use php to calculate the value for formula and insert it

like this

$con=mysqli_connect("localhost","username","password","dbname");
$selling="2";
$size="2";
$SqmPrice = ($selling/$size);

$sql = "insert into tablename(Selling,Size,SqmPrice) values($sellin,$size,$SqmPrice) ";
$query = mysqli_query($sql,$con)or die(mysqli_error($con);

NOTE: Option2 does not update the sqmprice if the selling / size value is updated later

krishna
  • 4,069
  • 2
  • 29
  • 56
1
UPDATE mytable
SET SqmPrice=Selling/Size
Mikpa
  • 1,912
  • 15
  • 20