我有两张桌子,即AccPurchase
和PurchaseProjection
。AccPurchase中已经有数据,我希望从AccPurchase获得数据,并在添加一定百分比后插入到PurchaseProjection表中。我有html表单,我从表单发送日期和百分比值。我在选定日期的每一行中添加了一定百分比,并将其插入到PurchaseProjection中。
<?php
$date = $_GET['1'];
$percentage = $_GET['2'];
$con = mysqli_connect("localhost","username","pw","db");
$sql = "Select Date, Amount, Paid, Outstanding from
AccPurchase Where Date='$date'";
$row = mysqli_query($con,$sql);
mysqli_fetch_array($row,MYSQLI_ASSOC);
$date=$row['Date'];
$amount=$row["Amount*('$percentage'/100)"];
$paid=$row['Paid'];
$outstanding=$row['Outstanding'];
$sql1 = "INSERT into PuchaseProjection (`Date`,`Amount`,`Paid`,`Outstanding`) VALUES ('$date','$amount','$paid','$outstanding')";
mysqli_query($con,$sql1);
mysqli_close($con);;
?>
发布于 2018-06-28 01:59:25
我建议您在MySQL操作中使用PDO。对于你的问题,我有解决办法,我也在我的项目中使用。只需将select查询放在insert查询之后,使用相同的列名和列计数,它就会像一个魅力一样工作。
查询: INSERT into PuchaseProjection (Date,Amount,Paid,Outstanding) Select Date, Amount+(Amount * :percentage / 100) as Amount, Paid, Outstanding from AccPurchase Where Date=:date
百分比和:日期表示pdo statement.Visit PDO官方手册中的占位符
发布于 2018-06-28 02:01:53
<?php
$date = $_GET['1'];
$percentage = $_GET['2'];
$con = mysqli_connect("localhost","username","pw","db");
$sql = "Select Date, Amount, Paid, Outstanding from
AccPurchase Where Date='$date'";
$row = mysqli_query($con,$sql);
mysqli_fetch_array($row,MYSQLI_ASSOC);
$date=$row['Date']/*do the operation,if any*/;
$amount=$row["Amount"]*($percentage/100);
$paid=$row['Paid']/*do the operation,if any*/;
$outstanding=$row['Outstanding']/*do the operation,if any*/;
$sql1 = "INSERT into PuchaseProjection
(`Date`,`Amount`,`Paid`,`Outstanding`) VALUES
('$date','$amount','$paid','$outstanding')";
mysqli_query($con,$sql1);
mysqli_close($con);
?>
尝尝这个
https://stackoverflow.com/questions/51079718
复制相似问题