所以我有一个允许我创建新用户的表单:
mkdir -p dist/lib/js dist/lib/css
我的动作脚本如下所示:
<?php include 'header.php';
require_once '../../db_con.php';
try{
$results = $dbh->query("SELECT * FROM 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 a 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 as $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>
现在,当我执行<?
session_start();
session_regenerate_id();
/*******************************************************************
** ACTION SCRIPT TO POST A NEW CATEGORY INTO THE 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(*) FROM `user_login` WHERE `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 INTO `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 THE ARRAY DEPENDING ON WHICH WAS CHECKED WITHIN THE FORM **
***********************************************************************/
$sql = "INSERT INTO `user_cat_link_table`(`UserID`, `Cat_ID`) VALUES";
$values = "";
$params = [];
foreach($_POST["cat_no"] as $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');
}
}
}
时,它会回复我期待的结果,但不会发布到联接表中:
var_dump($_POST);
我正在使用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" }
获取即将创建的用户的ID,然后将其链接到所选择的类别ID,并将其发布到koin表中,其中包含用户ID和选择了类别ID。