php - Inserting an array into a relational table -
i have 3 tables:
- user_login
- doc_list
- user_cat_link_table
so creating new user , apart of form shows array of available categories pulled cat_list table.
i struggling along other form data update user_cat_link_table.
the script take id of new user , take id of category selected checkboxes array:
here input form:
<form action="actions/add_emp.php" method="post"> <input type="text" name="user" placeholder="username" required="required" /><br/> <input type="text" name="pass" type="password" placeholder="password"/></label><br/> <input type="text" name="firstname" id="name" required="required" placeholder="firstname"/><br /> <input type="text" name="lastname" id="email" required="required" placeholder="lastname"/><br/> <input type="email" name="email" id="city" required="required" placeholder="email address"/><br/> <input type="text" name="extension" id="extension" required="required" placeholder="extension number"/><br/> <select name="title"> <option selected disabled>please select job title...</option> <option disabled></option> <option disabled>helpesk:</option> <option value="technical support advisor">technical support advisor</option> <option value="deputy team leade">deputy team leader</option> <option value="team leader">team leader</option> <option value="incident resolution advisor">incident resolution advisor </option> <option disabled></option> <option disabled>call centre:</option> <option value="technical support advisor">technical support advisor</option> <option value="">deputy team leader</option> <option value="">team leader</option> <option disabled></option> </select> <div id="checkboxlist" > <?php foreach($category $cat){ ?> <input type="checkbox" value="<?php echo $cat["cat_id"]; ?>" name="cat_no[]" id="box1"> <?php echo $cat["cat_title"]; ?></a><br> <?php } ?> </div> <input type="submit" value="add user" name="submit"/><br /> </form>
i can see if print_r() or var_dump() getting results expect:
array ( [action] => id [user] => test23wer3e4weret4essd [pass] => test [firstname] => test [lastname] => test [email] => tes@test.com [extension] => 1234 [cat_no] => array ( [0] => 69 [1] => 70 [2] => 71 ) [submit] => add user )
here form acts script insert data both user_login table (which works) part @ bottom trying insert join table having trouble with:
<? session_start(); session_regenerate_id(); if(!ini_get('date.timezone')) { date_default_timezone_set('gmt'); } if(isset($_post["action"])){ if(isset($_post['submit'])){ include_once'../../config.php'; $dbh = new pdo("mysql:host=$hostname;dbname=dashboardr",$username,$password); $dbh->setattribute(pdo::attr_errmode, pdo::errmode_exception); if(isset($_post['user']) && isset($_post['pass'])){ $password=$_post['pass']; $sql=$dbh->prepare("select count(*) `user_login` `username`=?"); $sql->execute(array($_post['user'])); if($sql->fetchcolumn()!=0){ die("user exists"); }else{ function rand_string($length) { $str=""; $chars = "subinsblogabcdefghijklmanopqrstuvwxyz abcdefghijklmnopqrstuvwxyz0123456789"; $size = strlen($chars); for($i = 0;$i < $length;$i++) { $str .= $chars[rand(0,$size-1)]; } return $str; } $p_salt = rand_string(20); $site_salt="subinsblogsalt"; $salted_hash = hash('sha256', $password.$site_salt.$p_salt); $sql=$dbh->prepare("insert `user_login` (`id`, `username`, `password`, `psalt`, `firstname`, `lastname`, `email`, `extension`) values (null, ?, ?, ?, ?, ?, ?, ?)"); $sql->execute( array($_post['user'], $salted_hash, $p_salt, $_post["firstname"], $_post["lastname"], $_post["email"], $_post["extension"])); print_r($_post); die(); $docid = $dbh->lastinsertid(); $sql = "insert `user_cat_link_table`(`userid`, `cat_id`) values"; $values = ""; $params = []; foreach($_post["cat_no"] $cat) { $values.= "(?, ?), "; $params[] = $cat; // correct here $params[] = $docid; } $values = substr($values, 0, -2); $sql.= $values; $query = $dbh->prepare($sql); $query->execute($params); if ($dbh->query($sql)) { }else{} $dbh = null; } //catch(pdoexception $e) header ('location: ../list_doc.php?success=1'); } } } ?>
Comments
Post a Comment