DOUBLEO Posted December 14, 2008 Share Posted December 14, 2008 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; ?> Link to comment https://forums.phpfreaks.com/topic/136878-solved-need-help-to-insert-dynamic-checkboxes-values-to-mysql-database/ Share on other sites More sharing options...
laPistola Posted December 14, 2008 Share Posted December 14, 2008 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. Link to comment https://forums.phpfreaks.com/topic/136878-solved-need-help-to-insert-dynamic-checkboxes-values-to-mysql-database/#findComment-714900 Share on other sites More sharing options...
DOUBLEO Posted December 14, 2008 Author Share Posted December 14, 2008 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; ?> Link to comment https://forums.phpfreaks.com/topic/136878-solved-need-help-to-insert-dynamic-checkboxes-values-to-mysql-database/#findComment-714910 Share on other sites More sharing options...
laPistola Posted December 14, 2008 Share Posted December 14, 2008 but your not telling the query that! Link to comment https://forums.phpfreaks.com/topic/136878-solved-need-help-to-insert-dynamic-checkboxes-values-to-mysql-database/#findComment-714918 Share on other sites More sharing options...
laPistola Posted December 14, 2008 Share Posted December 14, 2008 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) . ")"; Link to comment https://forums.phpfreaks.com/topic/136878-solved-need-help-to-insert-dynamic-checkboxes-values-to-mysql-database/#findComment-714921 Share on other sites More sharing options...
DOUBLEO Posted December 14, 2008 Author Share Posted December 14, 2008 ok i tried it. and i get Parse error: syntax error, unexpected T_VARIABLE line 68 which is. 68: $sql .= "(" . implode(','$op_ar) . ")"; Link to comment https://forums.phpfreaks.com/topic/136878-solved-need-help-to-insert-dynamic-checkboxes-values-to-mysql-database/#findComment-714928 Share on other sites More sharing options...
DOUBLEO Posted December 14, 2008 Author Share Posted December 14, 2008 found issue typo in { $op_art[] = " ('$key', '$vehicle_id') "; } fix $op_ar[] "t" was the typo, now my script works. yeah.... Link to comment https://forums.phpfreaks.com/topic/136878-solved-need-help-to-insert-dynamic-checkboxes-values-to-mysql-database/#findComment-714987 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.