如何在两个字段中划分值,并使用PHP将结果插入另一个字段

时间:2014-11-13 02:33:23

标签: php mysql

我需要划分ProductID和Cost的值。

以下是我的代码示例:

<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "testting";
$a = "calculationexample.ProductID";
$b = "calculationexample.Cost";
$c = "(a/b)";

try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception

$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO calculationexample (ProductID, Cost, Ratio)
VALUES ('400', '200', '$c')";
// use exec() because no results are returned


$conn->exec($sql);
echo "New record created successfully";
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}

$conn = null;
?>

为比率字段的ProductID,Cost EXCEPT字段正确插入值。有人能告诉我正确的方法吗?

谢谢。

===问题更新====

<?php

    require 'database.php';


    if ( !empty($_POST)) {
        // keep track validation errors
        $ProductIDError = null;
        $CostError = null;
        $MonthError = null


        // keep track post values
        $ProductID = $_POST['ProductID'];
        $Cost = $_POST['Cost'];
        $Month = $_POST['Month'];


        // validate input
        $valid = true;
        if (empty($ProductID)) {
            $ProductIDError = 'Please enter product id';
            $valid = false;
        }


        $valid = true;
        if (empty($Cost)) {
            $CostError = 'Please enter cost value';
            $valid = false;
        }


        $valid = true;
        if (empty($Month)) {
            $MonthError = 'Please enter cost value';
            $valid = false;
        }



$conn= new PDO("mysql:host=$mysql_hostname;dbname=$mysql_dbname", $mysql_username, $mysql_password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

//$var = "John"; // based on an id column (varchar)

    $stmt = $conn->prepare('SELECT Month,ProductID,Cost,Ratio FROM Inventory');
   // $stmt->bindValue(1, $var); 
    $stmt->execute();
    while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {

$a = $row['ProductID'];
$b = $row['Cost'];
$m =$row['Month']
$c = ($a/$b);

//echo $c; // will output the ratio

            }
}



    try {



    $stmt = $conn->prepare("INSERT INTO Inventory 
    (Month,ProductID, Cost, Ratio)
    VALUES(:Month, :ProductID, :Cost, :Ratio)");
    $stmt->bindValue(':Month', $m,PDO::PARAM_INT);    
    $stmt->bindValue(':ProductID', $a,PDO::PARAM_INT);
    $stmt->bindValue(':Cost', $b,PDO::PARAM_INT);
    $stmt->bindValue(':Ratio', $c,PDO::PARAM_INT);
    $stmt->execute();

echo "New record created successfully";
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}

$conn = null;
?>



<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <link   href="css/bootstrap.min.css" rel="stylesheet">
    <script src="js/bootstrap.min.js"></script>
</head>

<body>
    <div class="container">

                <div class="span10 offset1">
                    <div class="row">
                        <h3>Inventory</h3>
                    </div>



                    <form class="form-horizontal" action="division.php" method="post">

                      <div class="control-group <?php echo !empty($MonthError)?'error':'';?>">
                        <label class="control-label">Month</label>
                        <div class="controls">
                            <input name="month" type="text"  placeholder="Month" value="<?php echo !empty($m)?$m:'';?>">
                            <?php if (!empty($MonthError)): ?>
                                <span class="help-inline"><?php echo $MonthError;?></span>
                            <?php endif; ?>
                        </div>
                      </div>



                      <div class="control-group <?php echo !empty($ProductID)?'error':'';?>">
                        <label class="control-label">ProductID</label>
                        <div class="controls">
                            <input name="ProductID" type="text" placeholder="ProductID" value="<?php echo !empty($ProductID)?$ProductID:'';?>">
                            <?php if (!empty($ProductIDError)): ?>
                                <span class="help-inline"><?php echo $ProductIDError;?></span>
                            <?php endif;?>
                        </div>
                      </div>



                      <div class="control-group <?php echo !empty($Cost)?'error':'';?>">
                        <label class="control-label">Cost</label>
                        <div class="controls">
                            <input name="Cost" type="text"  placeholder="Cost" value="<?php echo !empty($Cost)?$Cost:'';?>">
                            <?php if (!empty($CostError)): ?>
                                <span class="help-inline"><?php echo $CostError;?></span>
                            <?php endif;?>
                        </div>
                      </div>



                      <div class="form-actions">
                          <button type="submit" class="btn btn-success">Daftar</button>
                          <a class="btn" href="index.bulanan.php">Menu Utama</a>
                        </div>
                    </form>
                </div>

    </div> <!-- /container -->
  </body>
</html>

1 个答案:

答案 0 :(得分:3)

首先需要查询数据库以获取ProductID和Cost列中的值,以便能够获得在表中输入的比率。

正如我在评论区域中所述,您需要将$c = "(a/b)";更改为$c = ($a/$b);

就目前而言,“a”和“b”都被视为constants,并在引号内设置为字符串。

如果设置在引号内,$c = "($a/$b)";将产生(10/50),而$c = ($a/$b);将产生0.2,基于使用$a = 10; $b = 50;

采取的步骤:

  • 查询您的表格
  • 计算列中的值(必须为int)
  • 将查询表中的结果值插入所需的表格

旁注:不确定400和200在那里做('400', '200', '$c')是什么,以及这些是您希望用作比率的数字。您可能需要详细说明。

你的比率列需要为DECIMAL 50,2 / 50是一个夸大的数字,但是2将是小数点。

N.B。:比率将产生小数。

附加说明:$var = "John";下方,基于id列(VARCHAR

例如:

<?php

$mysql_hostname = 'xxx';   // change
$mysql_username = 'xxx';   // to
$mysql_password = 'xxx';   // your
$mysql_dbname   = 'xxx';   // own

$conn= new PDO("mysql:host=$mysql_hostname;dbname=$mysql_dbname", $mysql_username, $mysql_password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$var = "John"; // based on an id column (varchar)

    $stmt = $conn->prepare('SELECT Cost, ProductID FROM calculationexample WHERE id = ?');
    $stmt->bindValue(1, $var); 
    $stmt->execute();
    while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {

$a = $row['Cost'];
$b = $row['ProductID'];
$c = ($a/$b);

echo $c; // will output the ratio

            }

try {

    $prod_id = 400;
    $cost = 200;

    $stmt = $conn->prepare("INSERT INTO calculationexample 
    (ProductID, Cost, Ratio)
    VALUES(:prod_id, :cost, :ratio)");
    $stmt->bindValue(':prod_id', $prod_id,PDO::PARAM_INT);
    $stmt->bindValue(':cost', $cost,PDO::PARAM_INT);
    $stmt->bindValue(':ratio', $c,PDO::PARAM_INT);
    $stmt->execute();

echo "New record created successfully";
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}

$conn = null;
?>