douweibiao8471 2013-09-25 08:11
浏览 48

使用Dropdown值更新MySql DB

My update statement isn't working correctly, I am attempting to pull the data from the database, populate a dropdown with either "Y" or "N" inside it, on submit the values are entered into the database and the page refreshes.

So far I have my list of items, each with correctly populated dropdown, it is now my submit that is failing to work.

<?php
    $updatedFeatProd = $_POST['featuredProduct'];
    var_dump($updatedFeatProd);

    if ($_POST) {
        foreach ($_POST['featuredProduct'] as $key => $val) {
            $query = 'UPDATE tblProducts SET featuredProduct = ' . $updatedFeatProd . '
                    WHERE fldID = ' . $val;
            $sql = dbQuery($query);
        }
    }
    $sql = dbQuery('SELECT fldId, fldName, featuredProduct FROM tblProducts');
?>

<form method="post" action="#" name="featuredProd">
    <table>
    <tr><td><p>Product Name</p></td><td><p>Is a featured product?</p></td></tr>

<?php
    $products = dbFetchAll($sql);
    foreach ($products as $product) { 
        //var_dump($product['fldName']);
?>
    <tr>
        <td>
            <p><?php echo $product['fldName']; ?></p>
        </td>
        <td>
            <select name="featuredDropdown">;
<?php
        if ($product['featuredProduct'] == 'Y') {
?>
                <option  value="<?php $product['fldId'] ?>"><?php echo $product['featuredProduct'] ?></option>
                <option value="<?php $product['fldId'] ?>">N</option>
<?php 
        } else {
?>
                <option value="<?php $product['fldId'] ?>"><?php echo $product['featuredProduct'] ?></option>
                <option value="<?php $product['fldId'] ?>">Y</option>
<?php 
        }
?>
            </select>
        </td>
    </tr>

<?php 
    }
?>
  • 写回答

1条回答 默认 最新

  • doude4201 2013-09-25 08:39
    关注

    The presentaton here does not make much sence. You have a dropdown with the ProductName in one slot and a 'N' in another.

    Once the user has selected 'N' for a product they have no idea what they have said NO to, as they can no longer see the product name that they have selected NO for.

    It would make more sence to provide a <label> containing the product name and a YES/NO dropdown beside it for them to select from.

    However the reason your update code is not working is that you have called the dropdown featuredDropdown

    <Select name="featuredDropdown">
    

    and you are trying to process a field called featuredProduct in the update code

    foreach ($_POST['featuredProduct'] as $key => $val) {
    

    Your next problem will probably be that you are oututting more than one <Select name="featuredDropdown"> so you need to make that into an array as well like this:

    <Select name="featuredDropdown[]">
    

    Then you will have an array of featuredDropdown in the $_POST array. $_POST['featuredDropdown'][]

    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)