0

我创建了以下程序,用户可以在其中更新列表中的任何特定记录。当用户单击更新按钮时,会打开一个表单,其中包含名称字段、图像字段和用于选择类别的下拉列表。问题是我收到通知:未定义索引:类别错误消息。我试图回应它,但它仍然给我同样的错误信息。我无法理解为什么它没有从下拉列表中获取类别的值。请检查并指导我。

<?php
    include 'connect.php';

    $sub_category_id= $_GET['sub_category_id'];

    $query=         "Select * from sub_categories where sub_category_id =$sub_category_id";

    $query_run=     mysql_query($query);    

    $fetch=         mysql_fetch_array($query_run);

    $name=          $fetch['sub_category_name'];


    $image_query=               "select * from sub_categories_images where sub_category_id=$sub_category_id";

    $image_query_run=           mysql_query($image_query);

    $image_query_fetch=         mysql_fetch_array($image_query_run);

    $image=                     $image_query_fetch['location'];



    echo "
            <form action='update_sub_category.php?sub_category_id=$sub_category_id' 
            method='POST' enctype='multipart/form-data'>
                <table border=1>
                <tr>
                <td>
                    Sub-Category Name: </td><td>    <input type='text' name='sub_category_name' value=$name ></td></tr>
                <tr>
                <td>
                    Image1:</td><td> <input type='file' name= 'file' value=$image></td></tr> 



            </form> </table>    ";




        /*--------------
        Drop Down List
        ---------------*/

            echo "<select name='category'>";


                $select_query=          'Select * from category';
                $select_query_run =     mysql_query($select_query);


            while   ($select_query_array=   mysql_fetch_array($select_query_run) )
            {

                    echo "<option value='".$select_query_array['category_id']."' >"
                    .htmlspecialchars($select_query_array["name"]).     
                    "</option>";                    

                }
                echo "</br>";

         $selectTag= "</br><input type='submit' value='Update Product'  /></select>";

         echo "</div></div>";

         echo $selectTag;

         echo "</form>";

        /*--------------
        Drop Down List
        ---------------*/




        $category_tag=  $_POST['category'];

        /*  
            $query_sub_category=        "update sub_categories set sub_category_name= $name, 
                                         category_id= $category where sub_category_id=$sub_category_id  ";
            */

?>
4

2 回答 2

0

您似乎在选择类别之前关闭表格。所以类别下拉列表不在表单中,因此不在 $_POST 数组中

在 $selectTag 中,您正在关闭表单,但我看不出您从哪里开始。

于 2013-07-27T08:05:51.010 回答
0

您结束了表格并且没有开始它..所以这就是为什么没有价值转移。要克服这个问题,您需要在我//// XXXXXXX ////在代码中 添加此注释的位置添加表格的开头

 <?php
            include 'connect.php';

            $sub_category_id= $_GET['sub_category_id'];

            $query=         "Select * from sub_categories where sub_category_id =$sub_category_id";

            $query_run=     mysql_query($query);    

            $fetch=         mysql_fetch_array($query_run);

            $name=              $fetch['sub_category_name'];


            $image_query=               "select * from sub_categories_images where sub_category_id=$sub_category_id";

            $image_query_run=           mysql_query($image_query);

            $image_query_fetch=     mysql_fetch_array($image_query_run);

            $image=                 $image_query_fetch['location'];



            echo "
                    <form action='update_sub_category.php?sub_category_id=$sub_category_id' 
                    method='POST' enctype='multipart/form-data'>
                        <table border=1>
                        <tr>
                        <td>
                            Sub-Category Name: </td><td>    <input type='text' name='sub_category_name' value=$name ></td></tr>
                        <tr>
                        <td>
                            Image1:</td><td> <input type='file' name= 'file' value=$image></td></tr> 



                    </form> </table>    ";




                /*--------------
                Drop Down List
                ---------------*/
            //// XXXXXXX ////
                    echo "<select name='category'>";


                        $select_query=          'Select * from category';
                        $select_query_run =     mysql_query($select_query);


                    while   ($select_query_array=   mysql_fetch_array($select_query_run) )
                    {

                            echo "<option value='".$select_query_array['category_id']."' >"
                            .htmlspecialchars($select_query_array["name"]).     
                            "</option>";                    

                        }
                        echo "</br>";

                 $selectTag= "</br><input type='submit' value='Update Product'  /></select></form>";

                 echo "</div></div>";

                 echo $selectTag;

                /*--------------
                Drop Down List
                ---------------*/




             $category= $_POST['category'];

                /*  
                    $query_sub_category=        "update sub_categories set sub_category_name= $name, 
                                                 category_id= $category where sub_category_id=$sub_category_id  ";
                    */

        ?>
于 2013-07-27T08:18:40.323 回答