在一次提交时在两个表中插入值

时间:2014-02-14 06:43:20

标签: php mysql database

在我的项目中,我想将相同的值存储在两个不同的表中。当我单击提交按钮时,该值应存储在两个表中。 但是这里的值存储在该表的第二个对象表函数中。

这是我的班级代码:

<?php
include("XXXXX.php");
$obj=new add;
$obj2=new add;
if(isset($_POST['submit']))
{
        $Date=$_POST['date'];
        $Amount=$_POST['amount'];
        $user=$_POST['user'];
        $detail=$_POST['detail'];
        $dw=$_POST['deposite'];

        $TotalAmount=$Amount+$last;

    $oiu=$obj->addAmount($Date,$Amount,$user,$a);
    $z=$obj2->addReport($Date,$user,$Amount,$dw,$TotalAmount);
//  $oiu2=$obj->addRs($Date,$Amount);
}
?>

我用于在数据库中存储数据的函数如下所示。

function addAmount($Date,$Amount,$user,$detail)
    {
        $addCash=$this->conn->prepare("INSERT INTO `amountcash`(`date`,`amount`,`user`,`detail`)
                                    VALUES(?,?,?,?)") ; 
        $addCash->bind_param("siss",$Date,$Amount,$user,$detail);    
        $addCash->execute();
//  echo "Successful";  
    }

    function addRs($Date,$Amount)
    {
        $addD=$this->conn->prepare("INSERT INTO `totalamount`(`date`,`amount`) values (?,?,)");
        $addD->bind_param("ss",$Date,$Amount);
        $addD->execute();
    }

    function addReport($Date,$user,$Amount,$dw,$TotalAmount)    
    {
        $addData=$this->conn->prepare('INSERT INTO `amount_flow` (`date`,`user`,`amount`,`detail`,total)values (?,?,?,?,?)');
        $addData->bind_param('ssisi',$Date,$user,$Amount,$dw,$TotalAmount);
        $addData->execute();
    }

1 个答案:

答案 0 :(得分:0)

我的建议是,在名为table的数据插入函数中添加另一个参数。例如

 function addAmount($Date,$Amount,$user,$detail, $table)
 {
   $addCash=$this->conn->prepare("INSERT INTO $table`(`date`,`amount`,`user`,`detail`)
                            VALUES(?,?,?,?)") ; 
   $addCash->bind_param("siss",$Date,$Amount,$user,$detail);    
   $addCash->execute();
   echo "Successful";
  }

这样你可以做两次

 $oiu=$obj->addAmount($Date,$Amount,$user,$a, "table_name"); //*2