FOR Loops -> SQL Query. Pain. Agony.
Posted: Thu Apr 14, 2005 6:37 pm
Still trying to generate a SQL query using the code below. I realise this outputs an extra comma at the end of the field list, and I can't seem to grok its destruction. Also, this returns "1" for the $icols and $qcols arrays, even though I think I've exploded the string values correctly and there's more than one value in the $row[5] and $row[3] variables. For the record, it returns the LAST value in each of those variables instead of the first, which I find wierd for some reason.
This is probably extremely poorly written, and easy to solve. Please help end my suffering. Physical death or code solution welcomed. Next-gen multi-table database structure is in the works, but I need a basic working solution first. Thanks for any assistance you can provide.
This is probably extremely poorly written, and easy to solve. Please help end my suffering. Physical death or code solution welcomed. Next-gen multi-table database structure is in the works, but I need a basic working solution first. Thanks for any assistance you can provide.
Code: Select all
$icols = array();
$qcols = array();
$BigBrother = "INSERT INTO orderhistory (datetime, legalname, title, degree, body, email, phone, address, city, state, zip, totalcost, paymethod,";
foreach(explode(',', $row[5]) as $citem) {
$icols[] = $citem; }
foreach(explode(',', $row[3]) as $cqty) {
$qcols[] = $cqty; }
for($n = 1; $n <= count($icols); $n++) {
$BigBrother .= " item" . $n . ","; }
for($q = 1; $q <= count($qcols); $q++) {
$BigBrother .= " qty" . $q . ","; }
$BigBrother .= ") VALUES ('$date', '$name', '$title', '$degree', '$body', '$email', '$phone', '$addr";
if($_POST["addr2"] !== "") {
$BigBrother .= " $addr2', '$city', '$state', '$zip', '$total', '$method'"; }
else {
$BigBrother .= "', '$city', '$state', '$zip', '$total', '$method'"; }
foreach(explode(',', $row[5]) as $iname) {
$BigBrother .= ", '$iname'"; }
foreach(explode(',', $row[3]) as $qty) {
$BigBrother .= ", '$qty'"; }
$BigBrother .= ")";