Jump to content

Add value to database if maths


Mutley

Recommended Posts

I'm trying to add a value to a database, depending on the comparison of 2 values.

So:
if($value1 > $value2) {
...insert value3 into the database.

The value1 and value2 will also be in the database. So is something like this correct:

[code]
<?php

"SELECT value1, value2 FROM table1";
$result = mysql_query($sql);

$value1 = "$_GET['value1']";
$value2 = "$_GET['value2']";

if($value1 > $value2) {
$value3 = "Yes";
} else {
$value3 = "No";
}
INSERT INTO `table1` (value3) VALUES ('".$value3."');
?>
[/code]

I'm really not very good at the lines of code to insert/view database but it's hopefully enough for some idea on what I'm trying to do.


Link to comment
https://forums.phpfreaks.com/topic/31094-add-value-to-database-if-maths/
Share on other sites

Try something like this:

[code]<?php
// Query the database for the values
$sql = "SELECT value1, value2 FROM table1"; // unless you only have one row you'll want a WHERE clause here
$result = mysql_query($sql);
$row = mysql_fetch_array($result);

// Check the values and assign accordingly
if ($row['value1'] > $row['value2']){
  $value3 = "Yes";
}
else {
  $value3 = "No";
}

// Insert new data into the database
$sql = "INSERT INTO `table1` (value3) VALUES ('$value3')";  // Again, if using a WHERE clause, you'll want it here too.
$mysql_query($sql);
?>[/code]

Regards
Huggie

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.