2013-02-18 40 views
2

當同時在兩個表中插入值時,我有稱爲用戶和類別的表。 但是在類別表格中,我使用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> 
+0

一些示例代碼將是 – 2013-02-18 13:54:04

+0

能更清楚請有用嗎? – 2013-02-18 13:57:17

+2

@qeremy和Andy Gee我發佈了代碼 – humphrey 2013-02-18 14:02:27

回答

1

我覺得挑戰在這裏吧? Cos,可以插入一隻貓嗎?

$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

+0

你即將給我一個答案是的,我真的明白你理解我100%是什麼正在尋找你的代碼使databse看起來正是我想要的,因爲user_id作爲ref從未改變是好的,但我怎麼能將值轉換爲真正的值,因爲我看到(146,?)在我的分區 – humphrey 2013-02-18 15:10:45

+1

@humphrey ;其實我懷疑我錯過了什麼,並打開了我的電腦。所以再次看到答案,也許修改。感謝接受。 – 2013-02-18 15:29:22

+0

感謝它工作100%請邀請我在gtalk kingmusa5在gmail.com – humphrey 2013-02-18 16:18:47

相關問題