-1

I search for answers here but haven't found a solution. I have also added picture of the error. I want the data to go to the first drop-down list ( above the error) I think the method I try to perform is also create drop-down list, am I correct?

           <form name="message" action="" method="post" onsubmit="" accept-charset="utf-8">
               <div class="form-group">
                   <label id="senderName">שם השולח:</label>



               </div>
            <div class="form-group">

                <label for="to_user">מען:</label>
                <select name="to_user" class="form-control">
                    <option value="pick">בחר מהרשימה</option>
                        <?php

                        $sql = \mysqli_query("SELECT name From users");
                        $row = mysqli_num_rows($sql);


                            echo "<select name='to_user'>";
                            while ($row = mysqli_fetch_array($sql)){
                                echo "<option value='". $row['name'] ."'>" .$row['name'] ."</option>" ;
                            }
                            echo "</select>" ;

                        ?>

                </select>


            </div>

picture of the error

Ani Menon
  • 27,209
  • 16
  • 105
  • 126

3 Answers3

4

In MySQLi, the first parameter of a query needs to be the database connection. Also, there's no need to add a \ before the statement.

$sql = \mysqli_query("SELECT name From users"); should be $sql = mysqli_query($con, "SELECT name From users");

Note: replace $con with your database connection variable!

As you mentioned that you wanted the result from the database to go inside the select form, simply adjust your code to look like this:

<select name="to_user" class="form-control">
<option value="pick">בחר מהרשימה</option>
<?php
$sql = mysqli_query($con, "SELECT name From users");
$row = mysqli_num_rows($sql);
while ($row = mysqli_fetch_array($sql)){
echo "<option value='". $row['name'] ."'>" .$row['name'] ."</option>" ;
}
?>
</select>
The Codesee
  • 3,714
  • 5
  • 38
  • 78
  • $con , solve the issue, but how can i add it to the drop-down list instead of print it? –  May 06 '16 at 16:41
  • @user3473379 Can you elaborate? – The Codesee May 06 '16 at 16:43
  • instead of put the sql answer inside the first drop-down list, it echo it under.http://rotter.name/User_files/nor/572cca733f0ad351.jpg >> this is a print screen to demonstrate . thanks in advance. –  May 06 '16 at 16:44
  • @user3473379 If you want to create a new drop-down list, move the closing `` tag from the bottom of the script to under ``. – The Codesee May 06 '16 at 16:48
  • maybe it is not possible , but i want to fetch data to this list not below. what do you think? want to say thank you for your quick response , much appreciation. –  May 06 '16 at 16:50
  • @user3473379 Are you saying you only want ONE drop-down? – The Codesee May 06 '16 at 16:53
  • you are the best :) . thank you very much . WOW thank you:!! –  May 06 '16 at 17:07
  • @user3473379 No problem! – The Codesee May 06 '16 at 17:09
  • it is possible to communicate with you via email ( job offer) ?@The Codesee –  May 07 '16 at 09:32
0
<div class="row form-group">
<div class="col col-md-3">
<label for="email-input" class=" form-control-label"> Vehicle</label>
</div>
<div class="col-12 col-md-9">

<select name="car_id" id="car_id" class="form-control-label" >
<?php
$list = mysqli_query($conn,"SELECT * FROM `vehicle_registration` where `status`='0' ");
while ($row_ah = mysqli_fetch_assoc($list)) {
?>
<option value="<?php echo $row_ah['id']; ?>"><?php echo $row_ah['car_no']; ?></option>
<?php } ?>
</select>

</div>
</div>
0
<label><b>Select Steam: </b></label>
<select id="study">
    <option value="" selected="selected" disabled="">---Selected---</option>
    <?php
    $query = "SELECT study FROM details";
    $query_run = mysqli_query($con, $query);

    while ($row = mysqli_fetch_array($query_run)) {
        echo "<option value='".$row['study']."'>".$row['study']."</option>";
    }

?>
</select>