简体   繁体   English

从数组中获取ID并发布到JOIN表

[英]GET ID from an array and POST to JOIN table

I have 3 tables which are linked with a join so the idea is I have an array as part of my form which lists out all my categories form my cat_list table: 我有3个与联接链接的表,所以我的想法是将一个数组作为表单的一部分,列出了cat_list表中的所有类别:

  <input type="checkbox" value="<?php echo $cat["cat_id"]; ?>" name="cat_no[$cat]"><?php echo $cat["cat_title"]; ?></a><br>

So I am looping through my cat_list and showing them, if I do a var_dump on these I get this: 因此,我遍历了cat_list并显示了它们,如果我对它们执行了var_dump,则会得到以下信息:

array(2) { ["cat_id"]=> string(1) "1" ["cat_title"]=> string(11) "Consumables" }
array(2) { ["cat_id"]=> string(1) "3" ["cat_title"]=> string(12) "Service Desk" }
array(2) { ["cat_id"]=> string(1) "4" ["cat_title"]=> string(14) "Solutions Team" }
array(2) { ["cat_id"]=> string(1) "5" ["cat_title"]=> string(7) "@Remote" }
array(2) { ["cat_id"]=> string(1) "6" ["cat_title"]=> string(12) "new test cat" }

this is great, but what I need to do is grab the ID's of whichever ones I have selected and post them in to my join table which looks like this: 这很好,但是我需要做的就是获取我选择的ID并将其发布到如下所示的连接表中:

    `cat_doc_link_table` (
    `id` int(11) NOT NULL,
      `link_cat_id` int(11) NOT NULL,
      `link_doc_id` int(11) NOT NULL
    ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=

8 ;

I fetchAll the cat_list to create the loop like this: 我fetchAll cat_list创建这样的循环:

<?php 


try{
    // Selecting entire row from cat_list table
    $results = $dbh->query("SELECT cat_id, cat_title FROM cat_list");

}catch(Exception $e) {
    echo $e->getMessage();
    die();
}

$cat = $results->fetchAll(PDO::FETCH_ASSOC);


?>

And my action script to post looks like this: 我要发布的动作脚本如下所示:

<?php

include 'db_con.php';

try {

$dbh = new PDO("mysql:host=$hostname;dbname=dashboardr",$username,$password);

$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // <== add this line

$sql = "UPDATE doc_list SET doc_title = :doc_title, doc_content = :doc_content, doc_updated=CURRENT_TIMESTAMP WHERE doc_id = :doc_id";
$query = $dbh->prepare($sql);
$query->execute(array(":doc_title"=>$_POST["doc_title"],":doc_content"=>$_POST["doc_content"], ":doc_id"=> $_POST["doc_id"]));

if ($query) {
    header ('Location: ../docList.php');
}
else{
}

$dbh = null;
}
catch(PDOException $e)
{
echo $e->getMessage();
}

}
?>
$SQL = "INSERT INTO `cat_doc_link_table`(`link_cat_id`, `link_doc_id`) VALUES";
$values = "";
$params = [];
$docId = $_POST["doc_id"];
foreach($cat_list as $cat)
{
    $values.= "(?, ?), ";
    $params[] = $cat["cat_id"];
    $params[] = $docId;
}
$values = substr($values, 0, -2);
$SQL.= $values;
$query = $dbh->prepare($SQL);
$query->execute($params);

You first loop through your categories there, and create a VALUES string ( $values.= ). 您首先要在其中遍历类别,然后创建一个VALUES字符串( $values.= )。 Then, we bind our parameters to the ? 然后,我们将参数绑定到?。 marks ( $params[] = $cat["cat_id"]; ). 标记( $params[] = $cat["cat_id"]; )。 Finally, we execute the query. 最后,我们执行查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM