2013-08-31 88 views
-1

在我的數據庫中,我有一個名爲user_items的表,其中存儲了每個用戶名以及他們每個項目的數量。每個用戶都可以購買1列,併爲他們輸入用戶名。現在我也有一個HTML表單,並希望從HTML選擇中進行PDO選擇。

<?php 
if (isset($_POST['Detach'])) { 

    // Here we get the info about the pokemon 
    $teamget = $db->prepare("select * from user_items where username = :name"); 
    $teamget->execute(array(':name' => $_SESSION['username'])); 
    $getteam = $teamget->fetch(); // Use fetchAll() if you want all results, or just iterate over the statement, since it implements Iterator 

    echo $getteam -> $_POST['item']; 

    echo "working"; 
} 
?> 

<form method="post" action=""> 

    <select name="item" id="item" style="width:150px;padding-left:5px;"> 
     <option value=""></option>  
     <option>poke_ball</option> 
     <option>great_ball</option> 
     <option>ultra_ball</option> 
     <option> aster_ball</option> 
     <option>potion</option> 
     <option>super_potion</option> 
     <option>hyper_potion</option> 
     <option>burn_heal</option> 
     <option>parlyz_heal</option> 
     <option>antidote</option> 
     <option>awakening</option> 
     <option>ice_heal</option> 
     <option>dawn_stone</option> 
     <option>dusk_stone</option> 
     <option>fire_stone</option> 
     <option>leaf_stone</option> 
     <option>moon_stone</option> 
     <option>oval_stone</option> 
     <option>shiny_stone</option> 
     <option>sun_stone</option> 
     <option>thunder_stone</option> 
     <option>water_stone</option> 
     <option>exp_share</option> 
    </select> 

    <select name="Detach" id="Detach" style="width:150px;padding-left:5px;"> 
     <option value=""></option>  
     <option>Attach To Pokemon 1</option> 
     <option> Attach To Pokemon 2</option> 
     <option> Attach To Pokemon 3</option> 
     <option> Attach To Pokemon 4</option> 
     <option> Attach To Pokemon 5</option> 
     <option> Attach To Pokemon 6</option> 

?> 
    </select> 
    <br/> 
    <br/> 

    <br/> 
    <br/> 
    <button name="submit" type="submit" id="Submit_Butt">Detach Item</button> 
    <p>&nbsp;</p> 
    <p>&nbsp;</p> 
</form> 

第一個select是每列的名稱。我想要做的是選擇列的值,其中$_POST =列

這個工作嗎?

$teamget = $db->prepare("select '".$_POST['item']."' from user_items where username = :name"); 
$teamget->execute(array(':name' => $_SESSION['username'])); 
$getteam = $teamget->fetch(); // Use fetchAll() if you want all results, or just iterate over the statement, since it implements Iterator 

echo $getteam ; 
+0

'session_start();'無疑在那裏,對嗎? –

+0

是的,$ db connect – user2734610

+1

知道了。所以新手們總是會留下這些信息。 –

回答

0

您的表格組織錯誤。它必須是:

username item quantity 

,現在你可以查詢它這樣

$sql ="select quantity from user_items where username = ? and item = ?"; 
$stmt = $db->prepare($sql); 
$stmt->execute(array($_SESSION['username'], $_POST['item'])); 
$item = $stmt->fetchColumn(); 

echo $item; 

它也應該是在此表中,而不是實際名稱user_iditem_id,但它已經是太複雜對你來說

+0

仍然無法正常工作....也是你從數量? – user2734610

+1

**您的表格組織錯誤。您需要另一個表結構來存儲您的數據** –

+0

所以我有項目名稱的列名稱,所以我想做一個簡單的選擇表單,該項目匹配的列。所以我可以有一個名爲item1的列,所以如果我想獲得第1項的金額,他們有我想要做的選擇是用戶名=用戶名和列= item1這是存儲在帖子 – user2734610