1

I have a Book details form where we can insert the book details.
One of the field in this form is "Category_Id". The value of this field is coming from database.
So I want to display all the category id that is stored in a table name " book_catagory ".
I have tried to write code which I have done it earlier also but here no values are displayed in the form. Please help me to find my mistake. My code snippet is as follow:

<td>Enter Category ID: </td>
<td>
    <?php
        $select_query = "select Category_Id from book_catagory";
        $select_query_run = mysql_query($select_query);

        echo "<select name='category_id' id='category_id'>";

        while ($select_query_array = mysql_fetch_array($select_query_run)) {
            echo "<option value='" . htmlspecialchars($select_query_array['Category_Id']) . "' >" .
 htmlspecialchars($select_query_array["Category_Id"]) . " </option>";
        }

        echo "</select>";
    ?>
</td>
riya
  • 51
  • 1
  • 1
  • 6

2 Answers2

0

I can recommend you to establish the connection via the object orientated approach.This way to connect to database server is since PHP 5.5.0 deprecated.[1] Your query seems to be correct. You parse your SQL results with mysql_fetch_array(), which return a numeric and associated array.[2] If you prefer to parse your database result by associated attribute name only you should use the PHP method mysql_fetch_assoc()[3].

You can alter your code by following to fix your issue:

<td>Enter Category ID: </td>
<td>
    <?php
        $select_query = "select Category_Id from book_catagory";
        $select_query_run = mysql_query($select_query);

        echo "<select name='category_id' id='category_id'>";

        while ($select_query_array = mysql_fetch_array($select_query_run), MYSQL_NUM) {
            echo "<option value='".htmlspecialchars($select_query_array[0])."'>" .htmlspecialchars($select_query_array[0]) . " </option>";
        }

        echo "</select>";
    ?>
</td>



References:
[1] http://php.net/manual/de/mysqli.summary.php
[2] http://php.net/manual/de/function.mysql-fetch-array.php
[3] http://php.net/manual/de/function.mysql-fetch-assoc.php

Kevin
  • 26
  • 4
  • issue remains the same. @Kevin – riya Nov 24 '15 at 08:17
  • Did you have had send the sql statement direct in the database to check which results should been shown? Alternatively you can add an integer value to count while loops: `int i = 0; while() {} echo 'While loops:'.i;` – Kevin Nov 24 '15 at 08:29
  • Changed my post above. Maybe alteration works for you? – Kevin Nov 24 '15 at 08:42
0

You should try like this :

    <td>Enter Category ID: </td>
    <td>
        <?php
            $select_query = "select Category_Id from book_catagory";
            $select_query_run = mysql_query($select_query);

            echo "<select name='category_id' id='category_id'>";

            while ($select_query_array=mysql_fetch_array($select_query_run)) {
     ?>
<option value=' <?php echo htmlspecialchars($select_query_array['Category_Id']) ?>' >
    <?php echo htmlspecialchars($select_query_array['Category_Id']) ?></option>
            }
    <?php
            echo "</select>";
        ?>
    </td>

Also I would say that you should use mysqli. And you can also use mysql_fetch_asssoc if you need an associative array.

Nehal
  • 1,542
  • 4
  • 17
  • 30