简体   繁体   English

从选择查询向MySQL插入值

[英]Insert values to mysql from select query

I have a php results page which gets values from submited php form like 我有一个php结果页面,该页面从提交的php表单获取值,例如

$sales = mysqli_real_escape_string($link, (int)$_POST['sales']);

I have an insert query 我有一个插入查询

$sql = "INSERT INTO daily (date, sales) VALUES (CURRENT_TIMESTAMP, '$sales')";
if(mysqli_query($link, $sql)){
    "Records added successfully.";
} else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}

Now i want to add an extra field to db which will be based on a new select query 现在我想向数据库添加一个额外的字段,它将基于新的选择查询

$query = "SELECT SUM(sales) FROM daily WHERE date BETWEEN '2017-01-01' AND '2017-01-31'";

I've tried to add it to insert sql with no result 我试图添加它以插入sql而没有结果

$sql = "INSERT INTO daily (date, sales, total_sales) VALUES (CURRENT_TIMESTAMP, '$sales', '$query')";
if(mysqli_query($link, $sql)){
    "Records added successfully.";
} else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}

You could use a insert/select 您可以使用插入/选择

$sql = "INSERT INTO daily (date, sales, total_sales) 
        SELECT 
            CURRENT_TIMESTAMP,
             '$sales',
            SUM(sales) 
       FROM daily 
       WHERE date BETWEEN '2017-01-01' AND '2017-01-31'";

in two step you could execute your, query get the value and assign to insert ... eg: 在两步中,您可以执行查询,获取值并分配给insert ...例如:

$query = "SELECT SUM(sales) as tot FROM daily WHERE date BETWEEN '2017-01-01' AND '2017-01-31'";
mysqli_query($link, $query) ; 
$row = mysql_fetch_array($result, MYSQL_NUM);
$myTotal  = $row[0]
$sql = "INSERT INTO daily (date, sales, total_sales) VALUES (CURRENT_TIMESTAMP, '$sales', '$myTotal')";

You should run that query first, save the results in an array, cycle through the array and add the result in the correct field of your database. 您应该首先运行该查询,将结果保存在数组中,在数组中循环,然后将结果添加到数据库的正确字段中。

what you are doing now is to add the text of your query in the total_sales column of your daily table. 您现在要做的是将查询文本添加到daily表格的total_sales列中。

You can refer to this answer to inspire yourself: Get sum of MySQL column in PHP 您可以参考以下答案来激发自己的灵感: 在PHP中获取MySQL列的总和

Also this page explains very well what you have to do, scroll down until you reach the example titled "Inserting the result of a query in another table with group by". 此外, 页面还很好地解释了您必须执行的操作,向下滚动直到出现标题为“在具有group by的另一个表中插入查询结果”的示例。

Try this way: 尝试这种方式:

$sales = mysqli_real_escape_string($link, (int)$_POST['sales']);
$query = "SELECT SUM(sales) FROM daily WHERE date BETWEEN '2017-01-01' AND '2017-01-31'"

$sql = "INSERT INTO `daily` SET `date` = CURRENT_TIMESTAMP, `sales` = ' ".$sales." ', `total_sales` = ($query)";
if(mysqli_query($link, $sql)){
    "Records added successfully.";
} else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}

The code that worked was simple: 起作用的代码很简单:

$result= mysqli_query($link, "SELECT SUM(sales) as valuesum FROM daily);
$row = mysqli_fetch_assoc($result); 
$total_sales = $row['valuesum'];

I couldn't manage it to work because there was other errors which i found from phpfpm-error.log 我无法管理它,因为我从phpfpm-error.log中发现了其他错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM