PHP的MySQL乘以表格选择数据库值 [英] php mysql multiply database value with form selection

查看:80
本文介绍了PHP的MySQL乘以表格选择数据库值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我真的为此感到挣扎。我被要求开发一个脚本来计算油价,但无法使其正常工作。我已经能够设置一个表格来更新燃油价格。

Hello I am really struggling with this. I was asked to develop a script to calculate oil price but cannot get it to work. I have been able to setup a form to update fuel price.

我有一个名为fuel_price的表。在此表中将是按价格存储的每升燃料的成本。例如,如果每升油价为£0.50,我需要将此值乘以在表单下拉列表中选择的值。

I have a table called fuel_price. In this table will be cost per litre of fuel which is stored under Price. For example if oil price per litre is £0.50 I need to multiply this value by value selected within form dropdown.

有人可以指导我该怎么做吗?

Can anyone please guide me on what I am supposed to do??

确定,这里是更新代码预览。

Ok heres an update code preview.

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">

<select name="fueltype">
<option>- Select fuel type -</option>
<option value="Diesel">Diesel</option>
<option value="Red Diesel">Red Diesel</option>
<option value="Home Heating Oil">Home Heating Oil</option>
</select>
<select name="qtylitres">
<option>- Qty in Litres -</option>
<option value="100">100</option>
<option value="200">200</option>
<option value="400">400</option>
<option value="500">500</option>
<option value="900">900</option>
<option value="1000">1000</option>
</select>

<input type="hidden" name="id" value="" />
<input type="submit" name="submit" value="Submit" />
</form>

<?php

include 'mysql_connect.php';

$pdo = '';

    $stmt = $pdo->prepare("SELECT `Oil` from `fuel_price` WHERE id = '1'"); 
    if (!$stmt->execute()) { die($stmt->errorInfo[2]); } 
    $row = $stmt->fetch(); 

    $price = $row['Oil'];

    echo $_POST['qtylitres'] * $price;

?>

有人知道我要去哪里了吗?

Anyone know where I am going wrong??

谢谢

推荐答案

假定表中有价格列,并且唯一的结果包含正确的价格:

assuming that you have a column 'price' in your table, and that the only result contains the correct price:

include 'mysql_connect.php';
if (isset($_POST['submit'])) {
    // edit: added fueltype in the where clause
    $fueltype = mysql_real_escape_string($_POST['fueltype']);
    $q = "SELECT * FROM fuel_price WHERE id = '1' AND fueltype='$fueltype'";


    $result = mysql_query($q);
    $row= mysql_fetch_array($result);
    $price = $row['price'] * $_POST['qtylitres'];
    echo $price;

这篇关于PHP的MySQL乘以表格选择数据库值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆