javascript - trying to post an array in a db without success -
so have form allows me create new user:
<?php include 'header.php'; require_once '../../db_con.php'; try{ $results = $dbh->query("select * cat_list"); }catch(exception $e) { echo $e->getmessage(); die(); } $category = $results->fetchall(pdo::fetch_assoc); ?> <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="hidden" name="action" value="id"/> <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>
my action script looks this:
<? session_start(); session_regenerate_id(); /******************************************************************* ** action script post new category doc_list table ** *******************************************************************/ 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 = "subinsblogabcdefghijklmanopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789"; $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"])); } $userid = $dbh->lastinsertid(); /*********************************************************************** ** inserts array depending on checked within form ** ***********************************************************************/ $sql = "insert `user_cat_link_table`(`userid`, `cat_id`) values"; $values = ""; $params = []; foreach($_post["cat_no"] $cat) var_dump($_post); { $values.= "(?, ?), "; $params[] = $cat; // correct here $params[] = $userid; } $values = substr($values, 0, -2); $sql.= $values; $query = $dbh->prepare($sql); $query->execute($params); if ($dbh->query($sql)) { }else{} $dbh = null; } { header ('location: ../list_emp.php?success=1'); } } }
now when var_dump($_post);
gives me results expecting not posting join table:
array(9) { ["user"]=> string(19) "2342rrwersfsdfdffdf" ["pass"]=> string(3) "424" ["action"]=> string(2) "id" ["firstname"]=> string(5) "23423" ["lastname"]=> string(5) "23424" ["email"]=> string(26) "daniel.barrett@ricoh.co.uk" ["extension"]=> string(6) "312321" ["cat_no"]=> array(1) { [0]=> string(2) "61" } ["submit"]=> string(8) "add user" }
i taking id of user created using $userid = $dbh->lastinsertid();
, linking category id selcted , post koin table takes users id , category id selected.
Comments
Post a Comment