Jump to content

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
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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