如何在复选框的每个选项上插入访问相同ref Id的多个选项



当在两个表中同时插入值时,我有名为user和category的表。但在类别表中,我使用6个不同选择的复选框插入不同的类别,这些复选框被访问为购买一个名称。但问题是我插入的选项,我希望每个选项都能从用户的表中获得相同的引用Id,这样我就可以很容易地跟踪该用户的选择。到目前为止,当我插入时,它为每个选项提供了一个不同的参考id,并且它只在第一个选项上使用原始id。请帮助我解决这个问题。下面是代码,但我删除了一些,所以我们只关注这个问题。

  <?php

?>
<div class="form">
    <h1>Client informatinon <?php  echo $_SESSION['username']."   ";?></h1>
    <form action ="form.php" method = "post" id="postform">
<table><tr><td>
<tr><td>
<input type="checkbox" name="category_name[]" id="inlineCheckbox1" value="Architecture"> Architecture
</td></td><td>
<input type="checkbox" name="category_name[]" id="inlineCheckbox2" value="townplanning">Town Planning 
</td></tr><tr><td>
<input type="checkbox" name="category_name[]" id="inlineCheckbox3" value="civilengineering">Civil Engineering 
                                </td><td>
<input type="checkbox" name="category_name[]" id="inlineCheckbox3" value="buildingandrenovation"> Building & Renovation
                                 </td></tr><tr><td>
                                 <input type="checkbox" name="category_name []" id="inlineCheckbox3" value="other"> Other 
        </td><td>                   
<input type="checkbox" name="category_name[]" id="inlineCheckbox3" value="interiorgaphicdesign"> Interior graphic design 
                                </td></tr>

    </form>
    </table>
</div>
<?php

if(isset($_POST['category_name'])){
foreach($_POST['category_name'] as  $value){
?>
<?php
try{
$query="INSERT INTO tish_user(username,Password,Previllage,date_created)
VALUES(:username,:Password,:Previllage,:date_created)";
$insert = $con->prepare($query);
$insert->execute(array(
':username'=>$username,
':Password'=>(md5($Password)),
':Previllage'=>$Previllage,
':date_created'=>$date_created));
#end of first table
################################################
#You select the first Id and put it in a variable then 
$id_last = ("SELECT LAST_INSERT_ID()");
$result =$con->prepare($id_last);
$result->execute();
$last_id = $result->fetchColumn();
############################## Last Id query Ends here
#insert into  clientinfo table 
$clientinfor="INSERT INTO tish_clientinfo
(title, firstname, lastname, nickname, idnumber, client_code, 
 company, country, city, province, address, cell, 
tel, webaddress, satifiedstatus, email, job_approval, cash_with_vat, 
cash_paid, date_registered,user_id)
VALUES(:title,:firstname,:lastname,:nickname,:idnumber,:client_code,
:company,:country,:city,:province,:address,
:cell,:tel,:webaddress,:satifiedstatus, :email, :job_approval,
:cash_with_vat,:cash_paid, :date_registered,$last_id)";
$clientinfor_insert = $con->prepare($clientinfor);
$clientinfor_insert->execute(array(
':title'=>$title,
':firstname'=>$firstname,
':lastname'=>$lastname,
':nickname'=>$nickname,
':idnumber'=>$idnumber,
':client_code'=>$client_code,
':company'=>$company,
':country'=>$country,
':city'=>$city,
':province'=>$province,
':address'=>$address,
':cell'=>$cell,
':tel'=>$tel,
':webaddress'=>$webaddress,
':satifiedstatus'=>$satifiedstatus,
':email'=>$email,
':job_approval'=>$job_approval,
':cash_with_vat'=>$cash_with_vat,
':cash_paid'=>$cash_paid,
':date_registered'=>$date_registered
));
#end of clien infor 
################################################
$security="INSERT INTO tish_security(ip_address,user_id)
VALUES(:ip_address,$last_id)";
$security_insert = $con->prepare($security);
$security_insert->execute(array(
':ip_address'=>$ip_address));
##########################end of security 
############ images 
$images ="INSERT INTO tish_images(user_id,image_name,date_registered)
VALUES($last_id,:image_name,:date_registered)";
$images_insert = $con->prepare($images);
$images_insert->execute(array(
':image_name'=>$rename,
':date_registered'=>$date_created));
##############################category 
$catigory="INSERT INTO tish_catigory(user_id,category_name)
VALUES($last_id,:category_name)";
$catigory_insert = $con->prepare($catigory);
$catigory_insert->execute(array(
':category_name'=>$value));
############# property table##########################################################
/*$property ="INSERT INTO tish_propertyinfo(user_id,date_registered)
VALUES($last_id,:date_registered)";
$property_insert = $con->prepare($images);
$property_insert->execute(array(':date_registered'=>$date_created));
*/}catch(PDOException $e){
echo $e->getMessage();
}
#3 fo the 
}
}
var_dump($value);

?>

</body>

我认为挑战就在这里,对吧?因为,可以只插入一只猫吗?

$catigory="INSERT INTO tish_catigory(user_id,category_name)
    VALUES($last_id,:category_name)";

好吧,也许这不是一个真正的答案,但假设您正在尝试插入多个具有相同last_id的cat;

$cats = $vals = array();
foreach ((array) $_POST['category_name'] as $cat) {
    if ('' !== ($cat = trim($cat))) {
        $cats[] = $cat;
        $vals[] = "({$last_id}, ?)";
    }
}
if (!empty($cats)) {
    $sql = 'INSERT INTO tish_catigory (user_id, category_name) VALUES'. join(',', $vals);
    print($sql); // INSERT INTO tish_catigory (user_id, category_name) VALUES(111, ?),(111, ?)
    $sth = $con->prepare($sql);
    foreach ($cats as $i => $cat) {
        $sth->bindValue($i+1, $cat, PDO::PARAM_STR);
    }
    $sth->execute();
    ...
}

点击此处查看更多详细信息:http://php.net/manual/en/pdostatement.bindvalue.php

最新更新