简体   繁体   中英

Take value from form input, multiply it by MySQL table cell value and write result to another cell in same table

Please forgive my ignorance, I am still very much a beginner, and I don't even know if this is possible.

I need to insert into the database a price list in one currency based on another (based on EUR, output in USD).

I have the form to take the input for the conversion multiplier value and to put the values into an array should be relatively simple (even for me!)

<?php
    // Make a MySQL Connection
    $query = "SELECT price FROM table"; 

    $result = mysql_query($query) or die(mysql_error());

    while($row = mysql_fetch_array($result)){
    echo $row[euros].

    //here is where I need to perform the calculation with the result going into     $row[dollars](euros * ex.rate = dollars)

   UPDATE table SET dollars=INT($row[dollars]);
}
?>

Unfortunately, I need to fiddle with this some more as it doesn't work.

What I can't figure out is how to multiply the form input against each euro value and write the result into the appropriate dollars cell as an integer.

What I would do is write a single SQL query for this:

UPDATE `table` SET dollars=euros*1.23 WHERE 1

You can, of course, do this in PHP:

<?php
// first, prevent anyone from tampering with the query!
$factor = floatval($user_input);
// then the query itself
$result = mysql_query("UPDATE `table` SET dollars=euros*{$factor} WHERE 1");
?>

While I'm writing, when using associative arrays, you should use single quotes, eg not $row[dollars] but $row['dollars']. Enclose these inside curlies:

echo "dollars: {$row['dollars']}";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM