Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

filling combobox with data from php database

this is my code

    <select>
        <?php
        $query = "SELECT * FROM 'sections'";
        $result = mysql_query($query);  
            while($row=mysql_fetch_array($result)){                                                 
            echo "<option value='".$row[id]."'>".$row[sectionName]."</option>";
            }
        ?>          
    </select>

nothing happen I don't know why this my page maybe there is wrong some where

<?php
    ob_start();
    session_start();
    include('../includes/connect.php');
    include('../includes/phpCodes.php');        

?>

<!DOCTYPE html>
<html>
    <head>
        <title>لوحة التحكم</title>
        <meta charset="utf-8">

        <link rel="stylesheet" type="text/css" href="../css/mainstyle.css">
        <link rel="stylesheet" type="text/css" href="css/controlstyle.css">
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function(){
                $('#tabs div').hide();
                $('#tabs div:first').show();
                $('#tabs ul li:first').addClass('active');
                $('#tabs ul li a').click(function(){ 
                    $('#tabs ul li').removeClass('active');
                    $(this).parent().addClass('active'); 
                    var currentTab = $(this).attr('href'); 
                    $('#tabs div').hide();
                    $(currentTab).show();
                    return false;
                });
            });
        </script>
    </head>
    <body>
    <div class="wrapper">
        <?php headerCode(); ?>
        <div class="content">
              <div id="tabs">
                <ul>
                    <li><a href="#add">اضافة موضوع</a></li>
                    <li><a href="#remove">حذف موضوع</a></li>
                    <li><a href="#edit">تعديل موضوع</a></li>
                    <li><a href="#edit">التحكم بالاقسام</a></li>
                </ul>
                <div id="add">
                    <form method="POST" action="includes/add.php" dir="rtl" enctype="multipart/from-data">
                        <br>
                        حدد القسم :
                        <select>

                                    <?php
                                    $query = "SELECT * FROM 'sectionsd'";
                                    $result = mysql_query($query);
                                    while($row=mysql_fetch_array($result)){
                                        echo "<option value='".$row[id]."'>".$row[sectionName]."</option>";
                                    }?>         
                        </select><br>
                        عنوان الموضوع :<input type="text" name="title" class="mem-information"/><br>
                        الموضوع : <br /><textarea name="subject" rows="10" cols="50" class="mem-information" style="width: 500px"></textarea><br /><br>
                        الصورة :<input type="file" name="image"><br>
                        <input type="submit" value="إرسال" name="send" class="log" style="color: black">
                    </form>
                </div>
                <div id="remove">
                    <form method="POST" action="includes/remove.php" dir="rtl"><br>
                    حدد القسم :
                    <select name ="sectionsName">
                    <option value="">dd</option>
                    </select>

                        <input type="submit" value="حذف" name="send" class="log" style="color: black">
                    </form>
                </div>
                <div id="edit">

                </div>
                <div id="addDep">

                </div>
            </div>
        </div>
    </div>
    </body>
</html>

sorry for my bad English my table enter image description here

like image 448
Bassam Badr Avatar asked May 29 '26 04:05

Bassam Badr


1 Answers

The problem I see is you are using single quotes on your table name in your query, you should either use back tick or no back ticks for table name. Please try the following code:

<?php
    $query = "SELECT * FROM `sections`";
    $result = mysql_query($query);
    while($row=mysql_fetch_array($result, MYSQL_ASSOC)){                                                 
       echo "<option value='".$row['id']."'>".$row['sectionName']."</option>";
    }
?>

Also start looking into using mysqli (http://php.net/manual/en/book.mysqli.php) or PDO (http://php.net/manual/en/book.pdo.php), as mysql is deprecated.

like image 170
vee Avatar answered May 31 '26 16:05

vee



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!