So I have a database from which I want to output data in the form of a table.
<table>
<tr>
<th class="tg-031e">Username</th>
<th class="tg-031e">Password</th>
</tr>
..
</table>
For now I get the data like this:
<?php include 'connect.php';
echo "<tr><td class='tg-031e'>";
$SQL = "SELECT `Username` FROM `Users` WHERE ID=1";
$exec = mysql_query($SQL, $connection);
while($row = mysql_fetch_array($exec)){
echo $row['Username'] . "</td>";
} ?>
However, in that case I need to echo out each column independently. How can I make PHP dynamically create an HTML table row with the information from the database whenever new data is present in the MySQL columns?
modify your php code like this:
<?php include 'connect.php';
$SQL = "SELECT `Username` FROM `Users` WHERE ID=1";
$exec = mysql_query($SQL, $connection);
echo "<table>";
echo "<tr>";
echo "<th class="tg-031e">Username</th>";
echo "<th class="tg-031e">Password</th>";
echo "</tr>";
while($row = mysql_fetch_array($exec)){
//add as many fields in record, i.e: username, password... etc.
echo "<tr>";
echo "<td class='tg-031e'>" . $row['Username'] . "</td>";
echo "<td class='tg-031e'>" . $row['Password'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
<?php include 'connect.php';
$SQL = "SELECT `Username`, Password FROM `Users`";
$exec = mysql_query($SQL, $connection);
echo "<table><tr><td class="tg-031e">Username</td><td class="tg-031e">Password</td></tr>";
while($row = mysql_fetch_array($exec))
{
echo "<tr><td class='tg-031e'>";
echo $row['Username'] . "</td>";
echo "<td class='tg-031e'>".$row['Password'] . "</td></tr>";
}
echo "</table>";
?>
Related
We have Table like below :
Once we click on "Submit" button , I am displaying random numbers below column "tracking id" using below query , its working fine:
$sql = $con->query("update orders set tracking_id = '$r' WHERE id ='1'");
$sql = $con->query("update orders set tracking_id = '$r' WHERE id ='2'");
But when i use below query, its not updating....
$sql = $con->query("update orders set tracking_id = '$r' WHERE id ='$id'");
Code :
<?php
$result = mysqli_query($con,"SELECT * FROM orders");
echo "<table border='1'>
<tr>
<th>order</th>
<th>payment</th>
<th>generate</th>
<th>tracking id</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
$id = $row['id'];
echo "<tr>";
echo "<td>" . $row['order_id'] . "</td>";
echo "<td>" . $row['payment_type'] . "</td>";
echo "<td><form method='post' action='new1.php'>
<input type = submit>
</form> </td>";
echo "<td>" . $row['tracking_id'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
new1.php
<?php
$id = $row['id'];
$r = mt_rand(1000,9999);
$sql = $con->query("update orders set tracking_id = '$r' WHERE id ='$id'");
?>
I think that you're not including the details in the form
<?php
$result = mysqli_query($con, "SELECT * FROM orders");
echo "<table border='1'>
<tr>
<th>order</th>
<th>payment</th>
<th>generate</th>
<th>tracking id</th>
</tr>";
while ($row = mysqli_fetch_array($result)) {
$id = $row['id'];
echo "<tr>";
echo "<td>" . $row['order_id'] . "</td>";
echo "<td>" . $row['payment_type'] . "</td>";
echo "<td>";
if (empty($row['tracking_id'])) {
echo "<form method='post' action='new1.php'>";
echo "<input type ='hidden' name='id' value='$id'>
<input type='submit'>
</form>";
}
echo "</td>";
echo "<td>" . $row['tracking_id'] . " </td > ";
echo "</tr>";
}
echo "</table >";
?>
And you also need to modify new1.php
<?php
$id = $_POST['id'];
$r = mt_rand(1000,9999);
$sql = $con->query("update orders set tracking_id = '$r' WHERE id ='$id'");
?>
In my code, I am showing a table within my database called staff in a HTML table and I want to add a delete button to each row in the HTML table that when clicked, it will delete the record its associated with.
Based on searching other solutions, my code looks like this:
staff.php:
require_once('../connection.php');
//delete row on button click
if(isset($_GET["del"])){
$idc = $_GET["del"];
if($VisitorManagement->query("DELETE FROM staff WHERE id=$idc")){
header('Location: delete-thankyou.php');
} else {
echo "Failed to delete staff member.";
}
}
$result = mysqli_query($VisitorManagement, "SELECT * FROM staff ORDER BY fullName");
echo "<table id='staff'>
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th></th>
</tr>
</thead>";
while($row = mysqli_fetch_array($result))
{
echo "<tbody>";
echo "<tr>";
echo "<td>" . $row['fullName'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "<td><a class='button alert' href='staff.php?del=".$row["idc"]."'>Delete</a></td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
connection.php:
$hostname_VisitorManagement = "localhost";
$database_VisitorManagement = "visitor-management";
$username_VisitorManagement = "***";
$password_VisitorManagement = "***";
$VisitorManagement = mysqli_connect($hostname_VisitorManagement, $username_VisitorManagement, $password_VisitorManagement, $database_VisitorManagement);
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
date_default_timezone_set('America/New_York');
Unfortunately, when I go to the click one of the buttons, it fails to delete the record and echoes the error message pre-defined in staff.php. Am I missing something to get this to work?
I was able to fix it by changing all instances of idc to id.
New code is:
require_once('../connection.php');
//delete row on button click
if(isset($_GET["del"])){
$id = $_GET["del"];
if($VisitorManagement->query("DELETE FROM staff WHERE id=$id")){
header('Location: delete-thankyou.php');
} else {
echo "Failed to delete staff member.";
}
}
$result = mysqli_query($VisitorManagement, "SELECT * FROM staff ORDER BY fullName");
echo "<table id='staff'>
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th></th>
</tr>
</thead>";
while($row = mysqli_fetch_array($result))
{
echo "<tbody>";
echo "<tr>";
echo "<td>" . $row['fullName'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "<td><a class='button alert' href='staff.php?del=".$row["id"]."'>Delete</a></td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
Trying to get info from the database into a table. Below is the code I am using but it is not populating the table. The site comes up but no info from the database. Please help, I am very new to this php thing and have no idea what I am doing apart from Google!
<body>
<?php include("header.php"); ?>
<?php
$con=mysqli_connect("localhost","username","password","database");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$query = "SELECT * FROM 'teacher'";
$result = mysqli_query($con, $query);
echo "<div align=\"center\">";
echo "<table width=\"100%\">";
echo "<tr>";
echo "<th>First Name</th>";
echo "<th>Middle Name</th>";
echo "<th>Last Name</th>";
echo "</tr>";
while ($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "</td><td>";
echo $row['first_name'];
echo "</td><td>";
echo $row['middle_name'];
echo "</td><td>";
echo $row['last_name'];
echo "</td></tr>";
}
echo "</table>";
mysqli_free_result($result);
mysqli_close($con);
?>
</div>
</body>
Your query is wrong
$query = "SELECT * FROM 'teacher'";
should be
$query = "SELECT * FROM `teacher`";
"SELECT * FROM teacher"
Just remove your quotation marks.
Try this
$query = "SELECT * FROM teacher";
Ok so I have a simple php select script and I have the code below to define variables.
$result = mysqli_query($con,"SELECT * FROM practice_sheets WHERE student_name='$_SESSION[SESS_FIRST_NAME] $_SESSION[SESS_LAST_NAME]'");
$numrows = mysqli_num_rows($result);
$id = $row['id'];
$total_min = $row['total_min'];
$due_date = $row['due_date'];
I then have:
echo "<td> <a href='account/practiceSheets?id='$id'> <i class='icon-eye-open'> </i> </a> </td>";
and this is supposed to pass the variables from the php select script into the url when the <a> is clicked.
All I end up with is the account/practiceSheets?id= with no actual id. I'm sure this is something stupidly simple and I do apologize as I am new to PHP and also didn't know what to call this to get a useable result in search engines! My full code is below if it helps.
<?php
$con = mysqli_connect("50.63.106.47", "usd309bands", "MacBook1!", "usd309bands");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con, "SELECT * FROM practice_sheets
WHERE student_name='$_SESSION[SESS_FIRST_NAME] $_SESSION[SESS_LAST_NAME]'");
$numrows = mysqli_num_rows($result);
$id = $row['id'];
$total_min = $row['total_min'];
$due_date = $row['due_date'];
if ($numrows == 0) {
echo "<div class='alert alert-danger'>";
echo "No Entries, See your instructor for details.";
echo "</div>";
} else {
echo "<table class='mws-table table-striped table-hover'>";
echo "<thead align='center'>";
echo "<tr>";
echo "<th>Sheet Number</th>";
echo "<th>Total Minutes</th>";
echo "<th>Due Date</th>";
echo "<th>View</th>";
echo "</tr>";
echo "</thead>";
echo "<tbody align='center'>";
while ($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['total_min'] . "</td>";
echo "<td>" . $row['due_date'] . "</td>";
echo "<td> <a href='account/practiceSheets?id='$id'> <i class='icon-eye-open'> </i> </a> </td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
mysqli_close($con);
}
?>
$result = mysqli_query($con,"SELECT * FROM practice_sheets
WHERE student_name='$_SESSION[SESS_FIRST_NAME] $_SESSION[SESS_LAST_NAME]'");
$numrows = mysqli_num_rows($result);
$id = $row['id'];
Can't see the where the $row is coming from.
I think you've forgotten to fetch the MySQL response into an associated array.
(You could use mysqli_fetch_assoc() to acomplish that)
Here's a quick example:
$result = mysqli_query($con,"SELECT * FROM practice_sheets WHERE student_name='$_SESSION[SESS_FIRST_NAME] $_SESSION[SESS_LAST_NAME]'");
$allRows = mysqli_fetch_assoc($result);
foreach($allRows as $row) {
echo $row['id'].'<br/>';
}
This one should list all ID's from the database request.
This sql query is probably not going to return anything unless names are stored in your table like this
firstlast
"SELECT * FROM practice_sheets WHERE student_name='$_SESSION[SESS_FIRST_NAME] $_SESSION[SESS_LAST_NAME]'"
you probably want something like this
$_SESSION[SESS_FIRST_NAME] . " " . $_SESSION[SESS_LAST_NAME]
to return a name like
first last
I changed
echo "<td> <a href='account/practiceSheets?id='$id'> <i class='icon-eye-open'></i></a></td>"
to this:
echo "<td> <a href='account/practiceSheets?id=" . $row["id"] . "'> <i class='icon-eye-open'> </i> </a> </td>";
and got the desired results!
I am accessing my database on PhpMyAdmin, all my names etc are correct, but for some reason UserId is not working. Can someone point me in the right direction?
I have tried printing it but nothing displays.
<?php session_start();
$username = $_GET['username'];
$password = $_GET['password'];
// Create connection
$con=mysqli_connect("localhost","root","","test");
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM user2 where username='$username' and password='$password'");
$row_cnt = mysqli_num_rows($result);
if($row_cnt >0){
while($row = mysqli_fetch_array($result)){
$UserId = $row['UserId'];
}
$sqlQuery2 = "SELECT ProductID, Name, Price, Description FROM product";
echo "Hello ".$username."<br>" .$UserId. "<br> This is a list of products";
$result2 = mysqli_query($con,$sqlQuery2);
echo "<table border='1'>
<tr>
<th>ProductID</th>
<th>Name</th>
<th>Price</th>
<th>Description</th>
<th>View</th>
</tr>";
while($row = mysqli_fetch_array($result2))
{
echo "<tr>";
echo "<td>" . $row['ProductID'] . "</td>";
echo "<td>" . $row['Name'] . "</td>";
echo "<td>" . $row['Price'] . "</td>";
echo "<td>" . $row['Description'] . "</td>";
echo "<td><a href=\"detailview.php?ProductID=".$row['ProductID']."\"'>Detailed View</a></td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
Update My Details
<?php } else{
echo "invalid login "; }
?>
use var_dump($var) to see what is in the variable
1st check what is returned in $result and after check $UserId before using it
after the while check if $UserId is set (if the condition is false, ou var is not set..) you should check $row_count too
you should indent your code
here is your code reindented :
<?php session_start();
$username = $_GET['username'];
$password = $_GET['password'];
// Create connection
$con=mysqli_connect("localhost","root","","test");
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM user2 where username='$username' and password='$password'");
$row_cnt = mysqli_num_rows($result);
if($row_cnt >0){
while($row = mysqli_fetch_array($result)){
$UserId = $row['UserId'];
}
$sqlQuery2 = "SELECT ProductID, Name, Price, Description FROM product";
echo "Hello ".$username."<br>" .$UserId. "<br> This is a list of products";
$result2 = mysqli_query($con,$sqlQuery2);
echo "<table border='1'>
<tr>
<th>ProductID</th>
<th>Name</th>
<th>Price</th>
<th>Description</th>
<th>View</th>
</tr>";
while($row = mysqli_fetch_array($result2))
{
echo "<tr>";
echo "<td>" . $row['ProductID'] . "</td>";
echo "<td>" . $row['Name'] . "</td>";
echo "<td>" . $row['Price'] . "</td>";
echo "<td>" . $row['Description'] . "</td>";
echo "<td><a href=\"detailview.php?ProductID=".$row['ProductID']."\"'>Detailed View</a></td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
Update My Details
<?php
}
else
{
echo "invalid login ";
}
?>