Click here to Skip to main content
15,905,504 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am currently learning PHP by doing a course and building my own stuff as I learn. I like to understand one part before I move onto the next and consqeuntly I have been on this biot for a few days now. I am trying to display the rows from my database (established connection and checked with "Create Entry" button) in tags. Here's my code (forgive the design, it's purely for my own practice).

reg-prac.php

<pre><?php include "db-prac.php"?>
<?php include "func-prac.php"?>
<?php
if(isset($_POST['create'])){
CreateEntry();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Register</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
</head>
<body style="background-color: grey;"><br>
<div class="container" style="height: 30%; width: 40%; background-color:lightblue; padding-top:20px;">
<div class="form-group">
<form method="post" action="reg-prac.php">
<label for="name" style="color: white;">Name</label>
<input type="text" class="form-control" name ="name">
<label for="password" style="color: white;">Surname</label>
<input type="text" class="form-control" name="sname">
<br>
<div clas="container" style="background-color: #e3b3e8; padding-top: 20px; padding-bottom:20px;border-radius: 25px;">
<center>
<input type="submit" value="Update" name="update">
<input type="submit" value="Delete" name="delete">
<input type="submit" value="READ" name="read">
<input type="submit" value="Create" name="create">
<select name="id" value="">
<?php
DisplayRows();
?>
</select>
</center>

</div>

</form>

</body>
</html>

func-prac.php:

<pre><?php

// Function to create entry

function CreateEntry(){
global $connection;
$name = $_POST['name'];
$sName = $_POST['sname'];
$query = "INSERT INTO users (username, password) VALUES ('$name','$sName');";
$result = mysqli_query($connection, $query);
if(!$result){
die("Error" . mysqli_error($result));
}
else{
echo "Added to the database";
}


}

// Function to display rows

function DisplayRows() {
global $connection;
global $id;
$query= "SELECT * users";
$result=mysqli_query($connection, $query);
if(!$result2){
die("FAILED" . mysqli_error($result));
}
else{
while ($row = mysqli_fetch_assoc($result)){
$id=$row['id'];
echo "<option id='$id'>$id</option>";
}
}



}
?>

db-prac.php:

<pre><?php
$connection = mysqli_connect('localhost', 'root', '', 'prac');
if(!$connection){
die("no connection could be made." . mysqli_error($connection));
}
else{
echo "CONNECTED";
}

?>


Any help would be greatly appreciated. Many thanks in advance.

What I have tried:

Passing various parameters to-and-fro and made $connection and $id global.

I've also tried staring at it for hours.
Posted
Updated 10-Jul-19 5:17am

Consider breaking it into bite-size testable pieces.
1 - check the return data from database. Is it what you want
2 - are you requesting the data correctly (i.e, can you just ECHO it?)
3 - Try to force-feed simple data into your dropdown - no SQL. Does it show?

If 1, 2, and 3 are work as expected, then you sort of narrowed your problem down to how you transform your SQL into text strings for display in the dropdown.

Simplify - when it works, then add other features until it stops working. Fix hose features.

Also, the value for a select is based on the select list - does not belong as attribute
<select name="id" value="">
<?php
DisplayRows();
?>
 
Share this answer
 
I forgot to add that I had also tried to just echo it out as plain text to test it and nothing. I took your advice and switched out my tag for tags and and it gave me a MySqli error, which pointed me in the direction my my $query variable which I then found used incorrect syntax.

I missed the word "FROM". Thanks a lot for your help.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900