【问题标题】:how to insert multiple choices accessing the same ref Id on each choice of checkbox如何在复选框的每个选择上插入访问相同参考 ID 的多个选择
【发布时间】:2013-02-18 13:51:17
【问题描述】:

同时在两个表中插入值时,我有名为 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>

【问题讨论】:

  • 一些示例代码会很有用
  • @qeremy 和 Andy Gee 我已经发布了代码
  • 所以只需设置 id (如果不是主要的,自动递增等)。在代码中,您只设置了 user)id 和 category_name。查询应该是:INSERT INTO tish_category(RefId, user_id,category_name) VALUES($last_id,:category_name)。不确定这是您要查找的内容。
  • 您需要一种方法来保存每个用户的复选框,因此您应该有一个所有类别的表 - cate_id(唯一,汽车公司等),categ_name;然后用于关联 user_categ - 具有 used_id、categ_id 和 refid 的表(我不会使用 refid);现在每次添加时,每个categ_id 和相同的user_id 都会有1 个条目;当您编辑时 - 只会获取特定用户的所有 categ_id,删除它们并插入新的检查。
  • 不确定我有没有收到你的问题。您想在“链接”/关联表中使用 user_id 吗?当然可以 - 即表格的目的只是保存“链接”user_id、categ_id 和您需要的任何其他信息,但我只会保留 id、所有 cols 不为空、名称和其他信息 - 只需通过 id 获取。 1个user_id可以有多个categ_id:user_id =1,categ_id =1将是(1,1)、(1,2)、(1,15)等;当您获得帐户类别时 - 只需按 user_id (acct) 查询

标签: php mysql pdo prepared-statement


【解决方案1】:

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

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

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

$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

【讨论】:

  • 你即将给我一个答案是的,我真的明白你理解我 100% 这就是正在寻找你的代码使数据库看起来正是我想要的,因为 user_id 作为参考从来没有改变是好的,但我怎样才能将值转换为真实值,因为我在我的数据库中看到(146,?)
  • @humphrey;实际上我怀疑我错过了什么并打开了我的电脑。所以再次查看答案,也许是修订。感谢您的接受。
  • 感谢它 100% 有效,请在 gmail.com 的 gtalk kingmusa5 上邀请我
猜你喜欢
  • 1970-01-01
  • 2015-06-27
  • 2011-11-09
  • 1970-01-01
  • 1970-01-01
  • 2013-07-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多