Jump to content

[SOLVED] Need Help to insert dynamic checkboxes values to mysql database


DOUBLEO

Recommended Posts

i can't make this part of the code work I'm a new to the php scene. my checkboxes display just fine but i'm unable to insert the values to db. I'm stuck please help. ???

 

 

<code.>

 

//inserting 62 CHECKBOX VALUES to second table vehicle options

 

$vehicle_id = mysql_insert_id();

 

$sql = "INSERT INTO `vehicle_opt` (`option_id`, `vehicle_id`) VALUES ";

 

$op_ar = array();

 

reset($_POST['chkOpt']);

 

while (list($key, $val)  = each($_POST['chkOpt'])) 

 

{

 

    $op_art[] = " ('$key', '$vehicle_id') ";

 

}

 

$sql .=  implode(", ", $op_ar);

 

echo $sql;

 

 

$result = mysql_query($options);

 

 

 

}

// selecting options from db.

 

mysql_select_db($database_localhost, $localhost);

$options_html = "";

$sql = 'SELECT * FROM `option` ORDER BY `option_txt` ASC'; 

$result = mysql_query($sql);

 

while ($myrow = mysql_fetch_assoc($result))

 

{

 

    $options_html .= "<input type='checkbox' value='1' name='chkOpt[$myrow[option_id]]' />$myrow[option_txt]<br/>     ";

}

 

?>

 

<?php

// this will display all checkboxes w/  values in db.

echo $options_html;

?>

as far as i can see you havent told it what the values are

 

$sql = "INSERT INTO `vehicle_opt` (`option_id`, `vehicle_id`) VALUES ('" . $_POST['name'] . "' , '" . $_POST['name'] . "') ";

 

change name to what ever the field name is that it should get the values from.

the values should be store in this array. right?

 

$op_ar = array();

 

reset($_POST['chkOpt']);

 

while (list($key, $val)  = each($_POST['chkOpt'])) 

 

{

 

    $op_art[] = " ('$key', '$vehicle_id') ";

 

}

 

$sql .=  implode(", ", $op_ar);

 

echo $sql;

 

 

$result = mysql_query($options);

 

 

 

}

do you have a better way to collect the values from here. fyi options are db driven.

 

$options_html .= "<input type='checkbox' value='1' name='chkOpt[$myrow[option_id]]' />$myrow[option_txt]<br/>     ";

 

<?php

// this will display all checkboxes w/  values in db.

echo $options_html;

?>

i read again and i see what you mean

 

what that code is effectivly doing is makeing the query look like

"INSERT INTO `vehicle_opt` (`option_id`, `vehicle_id`) VALUES $key,$vehicle_id";

 

you need to make it surround the values in ( )

 

so try

 

$sql .=  "(" . implode(','$op_ar) . ")";

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.