0

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();

?>

3
  • 1
    Please do not solicit people to reach out to you off of StackOverflow. I went ahead and removed that from your post :) Commented Oct 13, 2018 at 5:03
  • what is this echo "Select a Department"; that doesn't look like valid HTML ie <select>Select a Department<option .... ></select> you can't just insert text wily-nilly. Try echo '<option value="" >Select a Department</option>'; Commented Oct 13, 2018 at 5:11
  • ` <label>Department</label> <select> <option>Select</option> <?php mysql_connect('localhost', 'root', ''); mysql_select_db('library_system'); $sql = "SELECT member_type_id FROM member_type"; $result = mysqli_query($sql); echo "<select member_type_id='sub1'>"; while ($row =mysql_fetch_array($result)) { echo "<option value='" . $row['member_type_id'] ."'>" . $row['member_type_id'] ."</option>"; } ?> </select> ` here's my new code Commented Oct 13, 2018 at 5:22

4 Answers 4

1

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!

Sign up to request clarification or add additional context in comments.

11 Comments

<?php $host = "localhost"; $username = "root"; $password = ""; $dbname = "library_system"; $conn = new mysqli ($host, $username, $password, $dbname); if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } ?> here is my conn.php
put it in the question, or in backtics ` Such as this code
updated, but it still shows this pretty confusing mix codes <?php $host = "localhost"; $username = "root"; $password = ""; $dbname = "library_system"; $conn = new mysqli ($host, $username, $password, $dbname); if ($conn->connect_error) { die("Connection failed: " . $con>connect_error); } ?>
mysqli_fetch_assoc not mysql_fetch_array
updated <label>Department</label> <select> <option>Select</option> <?php mysql_connect('localhost', 'root', ''); mysql_select_db('library_system'); $sql = "SELECT member_type_id FROM member_type"; $result = mysqli_query($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>
|
0

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>

3 Comments

Yes this mysqli_fetch_assoc line
updated code here <label>Department</label> <select> <option>Select</option> <?php mysql_connect('localhost', 'root', ''); mysql_select_db('library_system'); $sql = "SELECT member_type_id FROM member_type"; $result = mysqli_query($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>
I have updated my answer with updated connection string please check.
0

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>

5 Comments

it did'nt work, but i change the =mysql_fetch_array to mysqli_fetch_array thank you very much ^_^
where do i put the name? where the databse is going to fetch it from
include 'php/connect.php'; in this file I suppose
I still having problem, it wont insert the data, I have a PHP method post
ohh just check for the mysqli
0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.