0

フォームから 2D 配列にデータを格納したいのですが、配列へのデータの挿入に問題があるようです。$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]);
}

PDOまた、またはについて学んでくださいmysqli。古いmysql_関数は非推奨です。

于 2013-04-16T06:39:17.450 に答える