Trying to write an app that allows people to set up options for their products in our system. The submit on the form is sent and runs the following code. The problem right now is that ONLY the first insert works. The rest don't error out, and they don't actually insert. But the create statements and selects all still work the rest of the way through. Code included before. Is this a "duh barbie" and I just missed something obvious? Why does the first insert work, but the rest fail, but then in the very next line go on to successfully create a table?
if ($addOrderOptionsSubmit) {
if (!$conn) { $conn = pg_pconnect("host=localhost user='$dbuser' password='$dbpass' dbname='$dbname'"); }
$resultb=pg_exec($conn,"select attribute_number from attr_rel where product_number='$productid';");
$rowsb = pg_numrows($resultb);
for ($l=0; $l < $rowsb; $l++){
list($attributenum) = pg_fetch_row($resultb,$l);
$resultb2=pg_exec($conn,"select name from attr where id=$attributenum;");
list($attr_name) = pg_fetch_row($resultb2,0);
$check2[$l] = $attr_name;
}
for ($m=0; $m < 3; $m++) {
$attr_name2 = "attr_name_${m}";
$attr_name3 = $$attr_name2;
if ($attr_name3) {
$test4=0;
for ($j=0; $j < $rowsb; $j++) {
if ($check2[$j] == $attr_name3) {
$test4=1;
break;
}
}
if ($test4 == 0) {
$resultbb=pg_exec($conn,"select id from attr where name='$attr_name3';");
$rowsc = pg_numrows($resultbb);
if ($rowsc == 0) {
$resultinserta=pg_exec($conn,"begin; insert into attr (name) values ('$attr_name3'); commit;");
$resultinserta2=pg_exec($conn,"select id from attr where name='$attr_name3';");
list($lastinsert) = pg_fetch_row($resultinserta2,0);
$resultinsertb=pg_exec($conn,"begin; insert into attr_rel (product_number,attribute_number) values ($productid, $lastinsert); commit;");
if (!$resultinsertb) {
$@!#$=pg_errormessage($conn);
echo "$@!#$<p>";
}
$resultcreate1=pg_exec($conn,"create table prod_attr_rel_$attr_name3 (
product_number int4 NOT NULL PRIMARY KEY,
attribute_number int4 NOT NULL,
price_adjustment int4
);");
$resultcreate2=pg_exec($conn,"create table attr_$attr_name3 (
id SERIAL,
name varchar(100)
);");
} else {
list($lastinsert) = pg_fetch_row($resultbb,0);
$resultinsertc=pg_exec($conn,"begin; insert into attr_rel (product_number,attribute_number) values ($productid, $lastinsert); commit;");
}
}
}
}
pg_close($conn);
if (!$conn) { $conn = pg_pconnect("host=localhost user='$dbuser' password='$dbpass' dbname='$dbname'"); }
$result=pg_exec($conn,"select attribute_number from attr_rel where product_number='$productid';");
$rows = pg_numrows($result);
for ($i=0; $i < $rows; $i++){
list($attributenum) = pg_fetch_row($result,$i);
$result2=pg_exec($conn,"select name from attr where id=$attributenum;");
list($attr_name) = pg_fetch_row($result2,0);
$test=0;
for ($o=1;$o <= 3; $o++) {
$attr_name2 = "attr_name${0}";
$attr_name3 = $$attr_name2;
if ($attr_name3) {
if ($attr_name == $attr_name3) {
$test=1;
$result3=pg_exec($conn,"select attribute_number,price_adjustment from prod_attr_rel$attr_name where product_number='$productid'");
$rows2 = pg_numrows($result3);
for ($m=0; $m < $rows2; $m++){
list($attribute_number, $price_adjust) = pg_fetch_row($result3,$m);
$result4=pg_exec($conn,"select name from attr$attr_name where id='$attribute_number'");
list($attribute_name) = pg_fetch_row($result4,0);
$check[$m]=$attribute_name;
$test2=0;
for ($b=0; $b < 10; $b++) {
$option_name="option_name${o}${b}";
$option_name2=$$option_name;
$option_price="option_price${o}${b}";
$option_price2=$$option_price;
if ($option_name2) {
if ($option_name2 == "$attribute_name") {
$resultup=pg_exec($conn,"update product_attr_rel$attr_name set price_adjustment='$option_price2' where attribute_number='$attribute_number' AND product_number='$productid';");
$test2=1;
break;
}
}
}
if ($test2 == 0) {
$resultdel2=pg_exec($conn,"delete from prod_attr_rel$attr_name where attribute_number='$attribute_number' AND product_number='$productid';");
}
}
for ($k=0; $k < 10; $k++) {
$option_name="option_name${o}${k}";
$option_name2=$$option_name;
$option_price="option_price${o}${k}";
$option_price2=$$option_price;
if ($option_name2) {
$test3=0;
for ($j=0; $j < 10; $j++) {
if ($check[$j] == $option_name2) {
$test3=1;
break;
}
}
if ($test3 == 0) {
$result15=pg_exec($conn,"select id from attr$attr_name where name='$option_name2';");
$rowsc = pg_numrows($result15);
if ($rowsc == 0) {
$resultinsert=pg_exec($conn,"insert into attr$attr_name (name) values ('$option_name2');");
$resultinsert2=pg_exec($conn,"select id from attr$attr_name where name='$option_name2';");
list($lastinsert) = pg_fetch_row($resultinsert2,0);
} else {
list($lastinsert) = pg_fetch_row($result15,0);
}
$resultinsert2=pg_exec($conn,"insert into prod_attr_rel_$attr_name values ($lastinsert,$productid,$option_price2);");
}
}
break;
}
}
}
if ($test == 0) {
$resultdel=pg_exec($conn,"delete from attr_rel where product_number='$productid' AND attribute_number=$attributenum;");
}
}
pg_close($conn);
echo"Your options were added to the Database<br>";
}
}