pdo从$ _POST中选择列

时间:2013-08-31 00:43:17

标签: php pdo

在我的数据库中,我有一个名为user_items的表,我存储了每个用户名以及他们拥有的每个项目的数量。每个用户都可以购买一列,并为其用户名添加一列。现在我还有一个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>

第一个选择是每列的名称。我要做的是选择$_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 ;

1 个答案:

答案 0 :(得分: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而不是实际名称,但它已经太复杂了