Forum Moderators: coopster

Message Too Old, No Replies

Cause array to list as separate rows in MySQL

Cause array to list as separate rows in MySQL

         

m2d4yh

5:37 am on Mar 23, 2007 (gmt 0)

10+ Year Member



Hi guys, I am really up against the wall with this, I've tried everything but I just can't do it...
I want this array to post as separate rows in MySQL instead of one row with the array.... here is my query and I really hope you can help (thanks either way)
<input type=\"checkbox\" name=\"box".$boxId."[]\" value=\"".$fields['boxvalue']."\" ".((in_array($fields['boxvalue'],$selectedValue))?"checked":"").">

$getFields = mysql_query("SELECT DISTINCT boxid, boxname, boxtype, active FROM opfields WHERE (categoryid='".$cat."' or categoryid='0') AND (productid='$row[product]' or productid='0')") or die(mysql_error());
$isFields = mysql_num_rows($getFields);
if ($isFields) {
while ($fields=mysql_fetch_array($getFields)) {
$box_value = "";
if ($fields['boxtype']=="checkbox") {
for ($i=0; $i<count($_POST['box'.$fields['boxid']]); $i++)
$box_value .= $_POST['box'.$fields['boxid']][$i]."+";
} else {
$box_value = $_POST['box'.$fields['boxid']];//for radio
}
if ($fields['boxtype']=="checkbox") {
if ($box_value!=='') {
$addFieldData = mysql_query("INSERT INTO opfields_data
(prodid, custid, boxid, boxname, boxvalue, monetary, boxtype) VALUES
('".$_GET[product]."','".$ses."','".$fields['boxid']."','".$fields['boxname']."', '".remSpecialChars($box_value)."','".remSpecialChars($box_value)."','".$fields['boxtype']."')") or die(mysql_error());

}
}else if ($fields['boxtype']=="radio") {

[edited by: dreamcatcher at 8:21 am (utc) on Mar. 23, 2007]
[edit reason] Fixed side scroll. [/edit]

mcibor

10:36 am on Mar 23, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I am not sure if I understand...

You want to insert one row for radio and N rows for checkbox, eg. when sb checked 4 checkboxes you want to insert 4 times?

then:

$getFields = mysql_query("SELECT DISTINCT boxid, boxname, boxtype, active
FROM opfields
WHERE (categoryid='".$cat."' OR categoryid='0')
AND (productid='$row[product]' OR productid='0')") or die(mysql_error());
$isFields = mysql_num_rows($getFields);

if ($isFields) {
while ($fields=mysql_fetch_array($getFields)) {
if ($fields['boxtype']=="checkbox") {
for ($i=0; $i<count($_POST['box'.$fields['boxid']]); $i++)

$addFieldData = mysql_query("INSERT INTO opfields_data
(prodid, custid, boxid, boxname, boxvalue, monetary, boxtype) VALUES
('".$_GET[product]."','".$ses."','".$fields['boxid']."','".$fields['boxname']."', '".remSpecialChars($_POST['box'.$fields['boxid']][$i])."','".remSpecialChars($_POST['box'.$fields['boxid']][$i])."','".$fields['boxtype']."')") or die(mysql_error());

} else {

$addFieldData = mysql_query("INSERT INTO opfields_data
(prodid, custid, boxid, boxname, boxvalue, monetary, boxtype) VALUES
('".$_GET[product]."','".$ses."','".$fields['boxid']."','".$fields['boxname']."', '".remSpecialChars($_POST['box'.$fields['boxid']])."','".remSpecialChars($_POST['box'.$fields['boxid']])."','".$fields['boxtype']."')") or die(mysql_error());

}
}
}

Hope this is what you want.
Michal