Help with variable refs and concat - simple?
Posted: Fri May 02, 2003 7:00 pm
I am trying to do a mysql insert/update using a loop for a large set of form-submitted options (33+). My logic (flawed?) was to make 33 fields with names as numbers so that I could simply (hmm) reference the field name using a loop var and churn through them
I guess there are several issues.
First, how do I reference a variable that is a number in the loop, for instance the var would be $1 (with a string value) and the loop var would be $i (which would=1 on the first pass, see code).
Second, if that works, can I use the same concept in the UPDATE statement to ref the field name? First field name is 1, then I say UPDATE $i (or $i++ as I don't have a 0 field). -I've never used a var to ref a field and couldn't find where that was not doable in the docs.
Third, maybe someone will tell me that naming fields with numbers is inane and then passing variables as numbers is equally so?
The above gives me a "expected T_Variable" error
Thanks for any pointers.
I guess there are several issues.
First, how do I reference a variable that is a number in the loop, for instance the var would be $1 (with a string value) and the loop var would be $i (which would=1 on the first pass, see code).
Second, if that works, can I use the same concept in the UPDATE statement to ref the field name? First field name is 1, then I say UPDATE $i (or $i++ as I don't have a 0 field). -I've never used a var to ref a field and couldn't find where that was not doable in the docs.
Third, maybe someone will tell me that naming fields with numbers is inane and then passing variables as numbers is equally so?
Code: Select all
<?php require_once('../Connections/cateye2.php');
mysql_select_db($database_cateye2, $cateye2);
?>
<?php
//get the product name from product table using the passed id
$query_rsProdID = "Select products_name from products WHERE products_id = $prod_id";
$rsProdID = mysql_query($query_rsProdID, $cateye2);
$row_rsProdID = mysql_fetch_assoc($rsProdID);
$ProdName = $row_rsProdIDї'products_name'];
//insert the easy part first
$sql = "INSERT INTO specs_com (prod_id, prod_name) VALUES ('$prod_id','$ProdName')";
$spec_id = mysql_insert_id(); //grab the id key for the value insert below
//create arrays for the set of specs 1, 1a, 2, 2a, etc.
for ($i=1; $i< 34;$i++){
if (isset($.$i){ //form variable should start with $1, $2, $3, etc
$specarrayї]= $.$i; //main spec string
$specarrayVї]= $.$i.a; // value for the above, if set
}
}
//Loop through the array and update the table for each
for ($i=0;$i< count($specarray);$i++){
$sqlS="UPDATE specs_com SET $i++ = $specarrayї$i] WHERE id = $spec_id";
$sqlV="UPDATE specs_com SET $i++.a = $specarrayVї$i] WHERE id = $spec_id";
mysql_query($sqlS, $cateye2);
mysql_query($sqlV, $cateye2);
}
if (isset($acc1)){
$sqlAcc1="UPDATE specs_cam SET acc1 = $acc1 WHERE id = $spec_id";
mysql_query($sqlACC1, $cateye2);
}
if (isset($acc12)){
$sqlAcc2="UPDATE specs_cam SET acc2 = $acc2 WHERE id = $spec_id";
mysql_query($sqlACC2, $cateye2);
}
?>Thanks for any pointers.