【发布时间】:2017-02-20 18:58:06
【问题描述】:
我整个星期天都在试图弄清楚这一点,但我不知道如何正确表达。它通过 Cron 作业每小时检查一次是否有新的销售。然后取产品编号和收据编号。它使用产品编号检查信息数据库,并从每一行中收集新销售产品的所有信息。然后对于每个售出的产品,我需要在数组末尾添加收据编号,并将所有这些信息插入到保存所有销售额的第三个数据库中。
我的主要问题是我合并它们,它不会插入到数据库中。我第一次让它工作,但只抓住了第一行。
<?php
//find out current time and 1 hour ago
$current_time = strtotime("now");
$tenmin_ago = strtotime('-10 min');
$hour_ago = strtotime('-1 hour');
//////////////////////////////////////////////////////////////////
/////////////////// Connect to Sales Database ///////////////////
//////////////////////////////////////////////////////////////////
// connect to EMAP sales MySQL server
$server='localhost';
$user='user';
$password='pass';
$database='sales_data';
$con = mysqli_connect($server,$user,$password,$database);
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: SALES " . mysqli_connect_error();
}
//////////////////////////////////////////////////////////////////
///////////////////// Connect to EM Database ////////////////////
//////////////////////////////////////////////////////////////////
//EM connect to DLGuard-EM MySQL server
$em_server='localhost';
$em_user='user';
$em_password='pass';
$em_database='databaseEM';
$em_con = mysqli_connect($em_server,$em_user,$em_password,$em_database);
//EM Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: EM " . mysqli_connect_error();
}
//EM grab store name
$em_dlg_store = "EM";
$em_rows = array();
$em_request = "SELECT * FROM customers WHERE date BETWEEN $hour_ago AND {$current_time}";
$em_result = mysqli_query($em_con, $em_request) or die("ERROR NO SALES EM");
while ($em_row = mysqli_fetch_array($em_result)) {
$em_prod_num = $em_row["prod_num"];
$em_rows[] = $em_row["receipt"];
}
//////////////////////////////////////////////////////////////////
///////////////////// Grab info for EM Sales ////////////////////
//////////////////////////////////////////////////////////////////
$emap_rows = array();
$emap_request = "SELECT * FROM all_products WHERE dlgprod_num='{$em_prod_num}' AND dlg_store='{$em_dlg_store}'";
$emap_result = mysqli_query($con, $emap_request) or die("ERROR dlg prod num EM");
while ($emap_row = mysqli_fetch_array($emap_result)) $emap_rows[] = $emap_row;
$em_emap_rows = array_merge($em_rows, $emap_rows);
/*VALUES ('$emap_sku', '$emap_dlgprod_num', '$emap_book_title', '$emap_dlgprod_price', '$emap_author_name', '$emap_author_email', '$emap_publisher', '$emap_dlg_store', '$em_receipt');";*/
// 1
$em_add_sql = "INSERT INTO all_author_sales (sku, dlgprod_num, dlgprod_nam, dlgprod_price, author_name, author_email, publisher, dlg_store, dlgcustomer_receipt)
VALUES ('$em_emap_rows');";
if ($con->multi_query($em_add_sql) === TRUE) {
} else {
echo "Error: " . $em_add_sql . "<br>" . $con->error;
}
?>
xxxxxxxxxxxxxxxxxxxxxxxxxxx 更新 2/27/17 xxxxxxxxxxxxxxxxxxxxxx
这是一个更新的版本,它获取了信息,但它丢失了在合并或字符串部分某处接收的数组。此外,它使顺序错误。而不是 UPC,产品编号,标题,价格等然后开始第二次销售的下一行,当有两次销售时,它将它们混合在一起,如 upc,UPC,产品编号,产品编号,标题,标题,价格,价格。 这是新的错误。我非常接近解决这个问题,谢谢。我将制作 4 个脚本,让他们每小时交替检查所有 4 家商店的销售情况。
Warning: array_map(): Argument #2 should be an array in /home1/lotscav1/public_html/Sales/scripts/sales-notif.php on line 53
Warning: implode(): Invalid arguments passed in /home1/lotscav1/public_html/Sales/scripts/sales-notif.php on line 53
Error: INSERT INTO all_author_sales (sku, dlgprod_num, dlgprod_nam, dlgprod_price, author_name, author_email, publisher, dlg_store, dlgcustomer_receipt) VALUES ('('EM2200002','EM2200002','1','1','Island Girl','Island Girl','4.95','4.95','Marshall Gibson','Marshall Gibson','jasminerice1993@gmail.com','jasminerice1993@gmail.com','Dan Cuneo','Dan Cuneo','EM','EM'),(),');
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'EM2200002','EM2200002','1','1','Island Girl','Island Girl','4.95','4.95','Marsha' at line 2
Array
这是更新的代码
<?php
//find out current time and 1 hour ago
$current_time = strtotime("now");
$hour_ago = strtotime('-24 hour');
//////////////////////////////////////////////////////////////////
/////////////////// Connect to Sales Database ///////////////////
//////////////////////////////////////////////////////////////////
// connect to EMAP sales MySQL server
$mysqli = new mysqli("localhost", "username", "password", "database");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
//////////////////////////////////////////////////////////////////
///////////////////// Connect to EM Database ////////////////////
//////////////////////////////////////////////////////////////////
$mysqli_em = new mysqli("localhost", "username", "password", "database");
if ($mysqli_em->connect_errno) {
echo "Failed to connect to MySQL_EM: (" . $mysqli_em->connect_errno . ") " . $mysqli_em->connect_error;
}
//EM grab store name
$em_dlg_store = "EM";
$em_rows = array();
$em_request = "SELECT * FROM customers WHERE date BETWEEN '$hour_ago' AND '$current_time'";
$em_result = mysqli_query($mysqli_em, $em_request) or die("Error No Sales EM");
while ($em_row = mysqli_fetch_array($em_result)) {
$em_prod_num = $em_row["prod_num"];
$em_rows[] = $em_row["receipt"];
}
//////////////////////////////////////////////////////////////////
///////////////////// Grab info for EM Sales ////////////////////
//////////////////////////////////////////////////////////////////
$emap_rows = array();
$emap_request = "SELECT * FROM all_products WHERE dlgprod_num='$em_prod_num' AND dlg_store='$em_dlg_store'";
$emap_result = mysqli_query($mysqli, $emap_request) or die("Error dlg prod num EM");
while ($emap_row = mysqli_fetch_array($emap_result)) $emap_rows[] = $emap_row;
$em_emap_rows = array_merge($emap_rows, $em_rows);
/*VALUES ('$emap_sku', '$emap_dlgprod_num', '$emap_book_title', '$emap_dlgprod_price', '$emap_author_name', '$emap_author_email', '$emap_publisher', '$emap_dlg_store', '$em_receipt');";*/
// 1
$string = "";
foreach ($em_emap_rows as $key => $innerArr) {
$result = implode( array_map('quoteItems', $innerArr), ",");
$string .= "(" . $result . ")";
if( $key != count($array) - 1 ){
$string .= ",";
}
}
function quoteItems($item){
return "'" . $item . "'";
}
//$em_rowss = implode(",", $em_rows); this turns the receipt array into a string
$em_add_sql = "INSERT INTO all_author_sales (sku, dlgprod_num, dlgprod_nam, dlgprod_price, author_name, author_email, publisher, dlg_store, dlgcustomer_receipt)
VALUES ('$string');";
if ($mysqli->multi_query($em_add_sql) === TRUE) {
} else {
echo "Error: " . $em_add_sql . "<br>" . $mysqli->error . "<br>" . $em_rows;
}
?>
【问题讨论】:
-
顺便非常感谢。我知道这很容易。我似乎无法得到它,经过 13 个小时的尝试,我发现我不知所措。
-
您有任何错误吗?为什么你评价 $current_time 而不是 $hour_ago?最好与您的选择保持一致
-
您正在尝试将插入和数组作为字符串。
VALUES ('$em_emap_rows');";-- $em_emap_rows 是一个数组而不是字符串。除非我错过了什么。
标签: php mysql mysql-insert-id