0

我想将表单中的数据存储到二维数组中,但将数据插入数组似乎有问题。如果我要回显 $orderArray[0][$count2] 它似乎可以工作,但如果我要回显 $orderArray[1][$count2] 会有错误

$dateArray = array();
$orderArray = array(array());
$amountArray = array(array());
$count = 0;
$count2 = 0;
foreach ($_POST['export'] as $date){ 
    $dateArray[$count] =  $date;
    include "/storescript/connect_to_mysql.php"; 
    $sql = mysql_query("SELECT * FROM ordera WHERE orderDate = '$date' ORDER BY     orderid ASC");
    $productCount = mysql_num_rows($sql); // count the output amount
        if ($productCount > 0) {
            while($row = mysql_fetch_array($sql)){ 
                $orderArray[$count][$count2] = $row["orderAmount"];
                $amountArray[$count][$count2] = $row["itemAmount"];
                $count2++;

            }
        }
            $count++;
    }
4

1 回答 1

0

我会将代码简化为:

// connect here
include "/storescript/connect_to_mysql.php"; 

// make date list safe for querying
$dates = join(',', array_map(function($date) {
    return sprintf("'%s'", mysql_real_escape_string($date));
}, $_POST['export']);

// run query    
$sql = "SELECT * FROM ordera WHERE orderDate IN ($dates) ORDER BY orderid";
$res = mysql_query($sql) or die("Error in query");

// collect results
while ($row = mysql_fetch_array($res)) {
    $orders[$date][] = $row['orderAmount'];
    $amounts[$date][] = $row['itemAmount'];
}

// do stuff with results
foreach ($orders as $date => $orderAmounts) {
    print_r($orderAmounts);
    print_r($amounts[$date]);
}

另外,请了解PDOor mysqli; 旧mysql_功能已弃用。

于 2013-04-16T06:39:17.450 回答