douyun8674 2015-06-04 13:03
浏览 61

将数组插入关系表

I have 3 tables:

  • user_login
  • doc_list
  • user_cat_link_table

So I am creating a new user and apart of the form shows an array of the available categories which are pulled from the cat_list table.

I am struggling to along with the other form data update the user_cat_link_table.

The script to take the ID of the new user and take the ID of the category selected from the checkboxes array:

Here is my 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 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>

I can see if I do a print_r() or var_dump() I am getting the results I 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 is the form which acts as the script to insert the data into both the user_login table (which works) but its the part at the bottom trying to insert into the join table I am 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(*) FROM `user_login` WHERE `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 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"]));
        print_r($_POST);
        die();

        $docId = $dbh->lastInsertId();

$sql = "INSERT INTO `user_cat_link_table`(`UserID`, `Cat_ID`) VALUES";
$values = "";
$params = [];

foreach($_POST["cat_no"] as $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');

    }

    }
    }
?>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 微带串馈天线阵列每个阵元宽度计算
    • ¥15 关于无人驾驶的航向角
    • ¥15 keil的map文件中Image component sizes各项意思
    • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
    • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
    • ¥15 划分vlan后,链路不通了?
    • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
    • ¥15 Vue3 大型图片数据拖动排序
    • ¥15 Centos / PETGEM
    • ¥15 划分vlan后不通了