php - Insert checkboxes into database -
** update **
i have updated code still no joy in getting join table update
i have 3 tables:
docs doc_id doc_name doc_content cat cat_id cat_name cat_color cat_icon cat_doc_join id cat_id doc_id
i have ability create , insert data docs table, inserts doc_name , doc_content table fine. have introduced categories new_doc.php form shows array of categories available can selected via checkbox. stands unsure on how amend insert query take id of doc creating , id of selected checkboxes have selected , go on not insert docs table insert row(s) doc_cat_join table if 1 or more categories had been selected:
here script handles insert:
<?php require ('../../db_con.php'); error_reporting(e_all); ini_set('display_errors', '1'); // form validation & submission if ($_server['request_method'] == 'post') { $errors = array(); // begin errors array // check first name if (empty($_post['doc_name'])) { $errors[] = 'you forgot enter document name.'; } else { $dn = mysqli_real_escape_string($dbc, trim($_post['doc_name'])); } // check last name if (empty($_post['doc_content'])) { $errors[] = 'you forgot enter document content.'; } else { $dc = mysqli_real_escape_string($dbc, trim($_post['doc_content'])); } if (empty($errors)) { // making query $q = "insert docs (doc_name, doc_content, created_date) values ('$dn', '$dc', now() )"; $r = mysqli_query($dbc, $q); if ($r) { $cat_ids=isset($_post['cats'])&&is_array($_post['cats'])?array_keys($_post['cats']):array(); $query='insert cat_doc_join (cat_id,doc_id) values'; $length=count($cat_ids); for($i=0;$i<$length;$i++){ $query.='('.$cat_ids[$i].','.$doc_id.')'; if($i<$length-1) $query.=','; } mysqli_query($dbc, $query); // redirect document list echo("<script>location.href = 'list_doc.php';</script>"); } else { // error checking! echo '<h1>system error!</h1> <p>you not registered because of system error!<br></p>'; // debuggin message echo '<p>' . mysqli_error($dbc) . '<br><br>query: ' . $q . '</p>'; } mysqli_close($dbc); exit(); } else { // report errors echo '<h1>error!</h1><p class="error">the following error(s) have occured:<br>'; foreach ($errors $msg) { echo " - $msg<br>\n"; } echo '</p><p>please try again!.</p><p><br></p>'; } } ?>
and here section of form array of categories:
<?php $q = "select * cats"; $r = mysqli_query ($dbc, $q); // run query. echo '<div class="view_body">'; // fetch , print records while ($row = mysqli_fetch_array($r)) { echo '<br><input type="checkbox" name="cats['.$row['cat_id'].']"> '.$row["cat_name"]. '</label>'; } echo '</div>'; ?>
i guess seeking advice on how checkboxes can include value cat_id , within insert query add in join table insert too.
echo checkboxes this:
while ($row = mysqli_fetch_array($r)) { echo '<br><input type="checkbox" name="cats['.$row['cat_id'].']"> '.$row["cat_name"]. '</label>'; }
and use (i suppose $doc_id
known, doc_id
, cat_id
ints , id
field of cat_doc_join
table autoincremented):
$cat_ids=isset($_post['cats'])&&is_array($_post['cats'])?array_keys($_post['cats']):array(); //please check fine here, ints, no sql injection,etc... $query='insert cat_doc_join (cat_id,doc_id) values'; $length=count($cat_ids); for($i=0;$i<$length;$i++){ $query.='('.$cat_ids[$i].','.$doc_id.')'; if($i<$length-1) $query.=','; } //run query
warning: you'll have sanitize input avoid sql injection , stuffs
Comments
Post a Comment