在两列中执行两次计算,答案将保存在另一列中

时间:2016-10-14 01:44:58

标签: php mysql sql

如何在我的数据库ex中执行自动计算。股票和数量(数量 - 股票)的价值答案将保存在CarryO列

create.php

<?php
 require_once 'dbconfig.php';
$con = mysql_connect("localhost","root","");
if($con)
 {
mysql_select_db("testproduct",$con);
 }

 if($_POST)
{


    $sql = mysql_query("SELECT * FROM tblproduct WHERE id = '".$_POST['pid']."'");
    $prod = mysql_fetch_array($sql);
    $pname = $prod['name'];
    $actualprice = $prod['actualprice'];
    $sellprice = $prod['sellprice'];
    $stock = $prod['Stock'];

    $gname = $_POST['gname'];
    $saledate = $_POST['saledate'];
    $quantity = $_POST['quantity'];
    $profit = $_POST['profit'];
    $carryO = $_POST['carryO'];
    $sells = $_POST['sells'];
    $expense = $_POST['expense'];
    try{

        $stmt = $db_con->prepare("INSERT INTO tblsales(pname,gname,saledate,quantity,actualprice,sellprice,carryO,sells,expense,profit,stock) 
                                VALUES(:upname,:ugname,:usaledate,:uquantity,:uactualprice,:usellprice,:ucarryO,:usells,:uexpense,:uprofit,:ustock)");
        $stmt->bindParam(":upname", $pname);
        $stmt->bindParam(":ugname", $gname);
        $stmt->bindParam(":usaledate", $saledate);
        $stmt->bindParam(":uquantity", $quantity);
        $stmt->bindParam(":uactualprice", $actualprice);
        $stmt->bindParam(":usellprice", $sellprice);
        $stmt->bindParam(":ucarryO", $carryO);
        $stmt->bindParam(":usells", $sells);
        $stmt->bindParam(":uexpense", $expense);
        $stmt->bindParam(":uprofit", $profit);
        $stmt->bindParam(":ustock", $stock);
        if($stmt->execute())
        {
            echo "Successfully Added";
        }
        else{
            echo "Query Problem";
        }   
    }
    catch(PDOException $e){
        echo $e->getMessage();
    }
}

?>

感谢您在php中的新帮助,如果我可以使用您的代码或只是一个例子,请告诉我

2 个答案:

答案 0 :(得分:1)

更改此部分:

$prod = mysql_query("SELECT * FROM tblproduct WHERE id = ".$_POST['pid']);
echo $prod;
$pname = [$prod['name']];

分为:

$sql = mysql_query("SELECT * FROM tblproduct WHERE id = '".$_POST['pid']."'");
$prod = mysql_fetch_array($sql);
$pname = $prod['name'];

答案 1 :(得分:0)

您可能想尝试一下。

name

如果您只提取一个"SELECT name FROM tblproduct WHERE id = ".$_POST['pid']列,那么请特定于您的查询以获得最快结果。例如$Bash dyld: Library not loaded: /usr/local/opt/readline/lib/libreadline.6.dylib Referenced from: /usr/local/bin/bash Reason: image not found Trace/BPT trap: 5