Nash
Nash

Reputation: 15

PHP fetch value from database and display it in the inside the HTML

I have a system where I have a problem I have this code where I just follow inside a youtube video. but change some text. here is my code

<label>Department</label>
    <select>
        <?php
        include 'php/connect.php';

        $sql = "SELECT member_type_id FROM member_type";
        $result = mysqli_query($conn, $sql);
        echo "Select a Department";
        while ($row =mysql_fetch_array($result)) {
        echo "<option value='" . $row['member_type_id'] ."'>" . $row['member_type_id'] ."</option>";
        }
        ?>
</select>

The problem is, It doesn't show me the value inside that table, here is my database table

enter image description here

the html file is OK, but when I click subit, I always get member registration failed someone correct my code please

<?php

include 'php/connect.php';

 $Lname = $_POST['Lname'];
 $Fname = $_POST['Fname'];
 $Mname = $_POST['Mname'];
 $Bdate = $_POST['Bdate'];
 $Address = $_POST['Address'];
 $Contact = $_POST['Contact'];
 $member_type_id = $_POST['Dept'];
 $Gender = $_POST['Gender'];



 if($Lname == '' OR $Fname == '' OR  $Mname =='' OR  $Bdate == '' OR $Address =='' OR $Contact =='' OR $Gender  =='' OR $member_type_id =='')
{       
            echo "Fill in all the forms";
}


else {



$sql = "INSERT INTO members (Lname, Fname, Mname, Bdate, Dept, Address, Contact, Gender)

VALUES ('$Lname', '$Fname', '$Mname', '$Bdate', 
'$member_type_id', '$Address', '$Contact', '$Gender')";
if ($conn->query($sql) === TRUE) 
    { 
            echo "A Member has been added successfully";</script>";

    } 
else 
{

echo "Member Registration Failed"; }

    }

$conn->close();

?>

Upvotes: 0

Views: 99

Answers (4)

PHP Geek
PHP Geek

Reputation: 4033

try this.

<?php
include 'php/connect.php';

 $sql = "SELECT member_type_id FROM member_type";
$option = '';
 while($row = mysql_fetch_assoc($sql))
{
  $option .= '<option value = "'.$row['member_type_id'].'">'.$row['member_type_id'].'</option>';
}
?>
<html>
<body>
<form>
 <select> 
<?php echo $option; ?>
</select>
</form>
</body>
</html>

Upvotes: 0

Avdhesh Solanki
Avdhesh Solanki

Reputation: 144

Use the code below, and you will get the desired results-

<label>Department</label>
<select>
        <option value="">Select a Department</option>
        <?php
        include 'php/connect.php';

        $sql = "SELECT member_type_id, member_type FROM member_type";
        $result = mysqli_query($conn, $sql);
        while ($row =mysqli_fetch_array($result)) {
        echo "<option value='" . $row['member_type_id'] ."'>" . $row['member_type'] ."</option>";
        }
        ?>
</select>

Upvotes: 0

Sachin
Sachin

Reputation: 799

Update your connection string

<label>Department</label> 
<select name='memberName'> 
    <option>Select</option> 
    <?php 
        // Create connection
        $conn = mysqli_connect("localhost", "root", "", "library_system");
        // Check connection
        if (!$conn) {
            die("Connection failed: " . mysqli_connect_error());
        }
        $sql = "SELECT member_type_id FROM member_type";
        $result = mysqli_query($conn, $sql);

        echo "<select member_type_id='sub1'>"; 
        while($row = mysqli_fetch_assoc($result)) {
            echo "<option value='" . $row['member_type_id'] ."'>" . $row['member_type_id'] ."</option>"; 
        } 
    ?> 
</select>

Upvotes: 0

ArtisticPhoenix
ArtisticPhoenix

Reputation: 21661

Well to start your HTML is invalid, specifically:

  echo "Select a Department";

This will result in HTML something like this

 <select>
     Select a Department
     <option value="1">Faculty</option>
       ...
 </select>

Instead echo the first thing as an actual option

  echo '<option value="" >Select a Department</option>'

So your HTML will be proper

As far as the Query goes, who knows. Without knowing if you set $conn and have a proper connection.

  $result = mysqli_query($conn, $sql);

Besides that

  $sql = "SELECT member_type_id FROM member_type";

Only gives you the number (no one want to select 1, 2 or 3) how will they know 1=Faculty?

 $sql = "SELECT * FROM member_type";

Will give you everything, like in PHPmyAdmin. And then:

 echo "<option value='" . $row['member_type_id'] ."'>" . $row['member_type'] ."</option>";

Will give you the text, assuming your $conn variable is a proper connection resource.

Lastly this quoting stuff, is ugly. Instead do this ( I cant stand single quotes in HTML)

 echo '<option value="'.$row['member_type_id'].'">'.$row['member_type'].'</option>';

That said if you must use singe quotes, at least use variable interpolation in a sensible way.

 echo "<option value='{$row['member_type_id']}'>{$row['member_type']}</option>";

One other thing (the thing)

    mysql_fetch_array

The mysql_* set of functions are dead as of PHP7 and there is a huge diffrence between mysql_ and mysqli_. But on top of that you want mysqli_fetch_assoc() as in:

 while ($row =mysqli_fetch_assoc($result)) {

UPDATE

Probably the most important thing is turn on display errors and error reporting. Let PHP tell you what is wrong, why struggle with it.

<?php      
  error_reporting(-1);
  ini_set('display_errors','1');

Saying It's not working or posting a huge block of code in a comment, is not helpful. Programing is hard. It's like math, you have to be exact in what you do or it won't do what you expect. There are many hundreds of things that can go wrong and typically only a handful of things that will work.

Thanks!

Upvotes: 1

Related Questions