I am trying to display MySQL Records in a HTML Table. I have gone through the process and in my php document it displays the tables name, description etc. but the data from my database doesn't display.
Here is my code:
<?php
//establishing connection
mysql_connect('localhost', 'root', 'root');
//selecting a database
mysql_select_db('festival');
$sql="SELECT * FROM deejay";
$records=mysql_query($sql);
?>
<html>
<head>
<title>PHP Test</title>
</head>
<body>
<h2>Festival Diagram</h2>
<img src="diagram.jpg" alt="diagram">
<h1>Deejay Data</h1>
<table width="300" border="1" cellpadding="10" cellspacing="1">
<tr>
<th>dj_id</th>
<th>name</th>
<th>country</th>
<th>info</th>
</tr>
<?php
while($deejay=mysql_fetch_assoc($records)){
echo "<tr>";
echo "<td>" .$records['dj_id']."</td>";
echo "<td>" .$records['name']."</td>";
echo "<td>" .$records['country']."</td>";
echo "<td>" .$records['info']."</td>";
echo "</tr>";
}
?>
</table>
</body>
</html>
When you do run code here: $deejay=mysql_fetch_assoc($records) you are assigning $deejay to be the current row item (the next row in the table that you got from $records). In other-words $records is still the entire retrieved data and $deejay is the current row you are on. So you need to say: $deejay['...'] instead of $record['...'].
echo "<td>".$deejay['dj_id']."</td>";
echo "<td>".$deejay['name']."</td>";
echo "<td>".$deejay['country']."</td>";
echo "<td>".$deejay['info']."</td>";
try this:
while($deejay=mysql_fetch_assoc($records)){
echo "<tr>";
echo "<td>" .$deejay['dj_id']."</td>";
echo "<td>" .$deejay['name']."</td>";
echo "<td>" .$deejay['country']."</td>";
echo "<td>" .$deejay['info']."</td>";
echo "</tr>";
}
Related
order.php
require_once(conn.php);
session_start();
$itemId=$_SESSION['itemId'];
$tnumber=$_SESSION['tnumber'];
$custno=$_SESSION['custno'];
$sql="select itemId,subtitle,price,quantity from cart where tnumber='$tnumber'" ;
$res=mysqli_query($dbhandle,$sql);
$total=0;
?>
<html>
<head>
<title>Home</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<link rel="stylesheet" type='text/css' href='cartbox.css'/>
</head>
<body>
<h1></h1>
<script src="home.js"></script>
<div id="shopping-cart"> </div>
<?php
echo "<table id='t1' border='1'>
<th>Subtitle</th>
<th>Quantity</th>
<th>Price</th>
<th>Amount</th>
</tr>";
while ($row=mysqli_fetch_array($res))
{
$amount=$row['price']*$row['quantity'];
echo "<tr>";
echo "<td>" .$row['subtitle'] ."</td>";
echo "<td>" .$row['quantity'] ."</td>";
echo "<td>" .$row['price'] ."</td>";
echo "<td>" . $amount . "</td>";
echo' <form id="addform" method="post" action="cartdelete.php"> ';
echo "<td><input type='submit' value='x' name='submit'></td>";
echo "<td><input type='text' name='itemId' value= ".$row['itemId']."></td>";
$total = $total+ $amount;
}
echo"</form>";
echo "</table>";
?>
<?php echo $total ?>
<input type="button" name="continue" value="Continue Order" style="position: absolute;top:200px;" onclick="location.href='customerdicecream.php'">
</body>
</html>
cartdelete.php
<?php
require_once(conn.php);
$itemId=(filter_input(\INPUT_POST,'itemId'));
$sql1="select itemId from cart";
$res1=mysqli_query($dbhandle,$sql1);
$row=mysqli_fetch_array($res1);
$itemId1=$row['itemId'];
if(!empty(\filter_input(INPUT_POST,'submit')))
{
$sql="delete from cart where itemId='$itemId1'";
$res=mysqli_query($dbhandle,$sql) or die(\mysqli_error($dbhandle));
}
echo"deleted successfully";
$mysqli_close = \mysqli_close($dbhandle);
i have retrieved the value from cart table and displayed in the html table,each row havhaving a button to delete.But whenever i try to delete irrespective of the button,only the first row of the table gets deleted.I guess the sql query in the deletecart.php is wrong.Please correct the code and the query..
Depends, if you're trying to delete one row then this is fine:
$sql="delete from cart where itemId='$itemId1'";
However to delete all it's simply:
$sql="DELETE * from cart";
The problem is not in the cart delete, it is in order.php. Two things I see here.
First up, you are opening each form inside the while loop but you don't close the form until outside the loop (and you never closed the <tr>s). This results in:
<form id="addform" method="post" action="cartdelete.php">
....
<form id="addform" method="post" action="cartdelete.php">
....
<form id="addform" method="post" action="cartdelete.php">
....
</form>
Only the outer form is closed. That corresponds to the first row on the table. No matter which submit button you click in the table, they all map back to the first one.
Second, I suggest you give each form a unique id.
echo "<form id=\"addform" . echo $row['itemId'] . "\" method="post" action="cartdelete.php"> ';
I have had instances of multiple forms on a page with the same id giving different results in different browsers.
$total = 0;
while ($row=mysqli_fetch_array($res))
{
$amount=$row['price']*$row['quantity'];
echo "<tr>";
echo "<td>" .$row['subtitle'] ."</td>";
echo "<td>" .$row['quantity'] ."</td>";
echo "<td>" .$row['price'] ."</td>";
echo "<td>" . $amount . "</td>";
echo "<form id='addform{$row['itemId']}' method='post' action='cartdelete.php'> ";
echo "<td><input type='submit' value='x' name='submit'></td>";
echo "<td><input type='text' name='itemId' value= '{$row['itemId']}'></td>";
echo"</form>";
echo "</tr>";
$total += $amount;
}
echo "</table>";
There is also a problem in the cartdelete.php. This line:
$sql1="select itemId from cart";
will only find the very first item in the cart. No matter what, it will never find the second or third item. It might not even be needed. Since I don't know the table structure, try this:
$itemId=(filter_input(\INPUT_POST,'itemId'));
if(!empty(\filter_input(INPUT_POST,'submit')))
{
$sql="delete from cart where itemId='$itemId'";
$res=mysqli_query($dbhandle,$sql) or die(\mysqli_error($dbhandle));
}
echo"deleted successfully";
$mysqli_close = \mysqli_close($dbhandle);
I've a php page with embedded HTML and I'm displaying data from a MySQL database. PHP is echoing the html inside the php page. All of the data is being returned; however, the data table is being displayed with an extra column and the data that should be in the last column is displayed in the extra column (e.g. my last name is 'Last Name,' but there is an extra column after 'Last Name' with the 'last name' data).
What am I doing wrong here?
Thanks.
get_records.php
//make connection
$conn = mysql_connect('localhost', 'root', '');
//select db
mysql_select_db('kis');
if (!$conn) {
die("Can not connect: " . mysql_error());
}
//select db and run query
mysql_select_db('kis');
$sql = "SELECT * FROM users";
$records = mysql_query($sql);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" type="text/css" href="css/TableCSSCode.css" media="all"/>
<title>Volunteer Data</title>
</head>
<body>
<div class="CSSTableGenerator">
<h1>Volunteer Records</h1>
<table>
<tr>
<th>First Name</th>
<th>Middle Name</th>
<th>Last Name</th>
</tr>
<tr>
<?php
//loop through the records and display in page
while ($users = mysql_fetch_assoc($records)) {
echo "<tr>";
echo "<td>" . $users['firstname'] . "</td>";
echo "<td>" . $users['middlename'] . "<td>";
echo "<td>" . $users['lastname'] . "<td>";
echo "</tr>";
}//end while
?>
</tr>
</table>
</div>
<!--end #dr_container-->
</body>
</html>
You need to close the td's
echo "<td>" . $users['middlename'] . "</td>";
echo "<td>" . $users['lastname'] . "</td>";
You have not properly closed your td tags:
echo "<td>" . $users['middlename'] . "<td>";
echo "<td>" . $users['lastname'] . "<td>";
It should be </td> at the end.
You're echoing the row tags (<tr>). So, don't add additional ones in the plain html (just around your PHP code).
Close "td" tag.
Remove "tr" tags before and after where php code start because there are already tr tags inside the php code.
Following is the updated HTML of table:
<table>
<tr>
<th>First Name</th>
<th>Middle Name</th>
<th>Last Name</th>
</tr>
<?php
//loop through the records and display in page
while ($users = mysql_fetch_assoc($records)) {
echo "<tr>";
echo "<td>" . $users['firstname'] . "</td>";
echo "<td>" . $users['middlename'] . "</td>";
echo "<td>" . $users['lastname'] . "</td>";
echo "</tr>";
}//end while
?>
I am trying to make a table in PHP.
All things are fine but all the rows are printed in same line.
How to print new line after each row in table? But in html there is no need to write extra code for new line why it is showing not a new line with PHP?
Here is that part of code:
<div class="contest-table" id="contest-table">
<table class="contest-details" id="contest-details">
<tr>
<th>CODE</th>
<th>NAME</th>
<th>START</th>
<th>END</th>
</tr>
<?php
//Show contest detials -> Contest Code|Contest Name |Start Date | End Date
$con=mysqli_connect("localhost","root","chandan","judge");
$result=mysqli_query($con,"SELECT * FROM judge_contest ");
echo "<tr>";
while($row = mysqli_fetch_array($result))
{
$contest_code=$row['contest_code'];
$contest_name=$row['contest_name'];
$contest_start_date=$row['start_date'];
$contest_end_date=$row['end_date'];
echo "<td>";
echo " $contest_code ";
echo "</td>";
echo "<td>";
echo " $contest_name ";
echo "</td>";
echo "<td>";
echo $contest_start_date;
echo "</td>";
echo "<td>";
echo $contest_end_date;
echo "</td>";
}
echo "</tr>";
?>
</table>
</div>
The problem is obvious, because you have put the statement echo "<tr>" and echo "</tr>" outside the while loop. You should put these two statement into the while loop.
echo '<tr>'
and
echo '</tr>'
should be inside the wile loop
HI, All
I have created (copied) a website that can view the information inside my database. But the code i have, displays every single bot as well as members.
I am connecting it to local host and the database is phpbb3.
I need a sting to get rid of the bots which have a group_id of 6.
Here is my code
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>View Records</title>
</head>
<body>
<?php
/*
VIEW.PHP
Displays all data from 'phpbb_users' table
*/
// connect to the database
include('connect-db.php');
// get results from database
$result = mysql_query("SELECT * FROM phpbb_users")
or die(mysql_error());
// display data in table
echo "<p><b>View All</b> | <a href='view-paginated.php?page=1'>View Paginated</a></p>";
echo "<table border='1' cellpadding='10'>";
echo "<tr> <th>ID</th> <th>Username</th> <th>Email</th> <th></th> <th></th></tr>";
// loop through results of database query, displaying them in the table
while($row = mysql_fetch_array( $result )) {
// echo out the contents of each row into a table
echo "<tr>";
echo '<td>' . $row['user_id'] . '</td>';
echo '<td>' . $row['username'] . '</td>';
echo '<td>' . $row['user_email'] . '</td>';
echo '<td>Edit</td>';
echo '<td>Delete</td>';
echo "</tr>";
}
// close table>
echo "</table>";
?>
<p>Add a new record</p>
</body>
</html>
Thank you,
In advanced.
Change this:
$result = mysql_query("SELECT * FROM phpbb_users")
To
$result = mysql_query("SELECT * FROM phpbb_users WHERE group_id <> 6")
I am new to web dev, PHP, CSS and html. I want to put a CSS in my table displaying the data on my database, but it doesn't work.
Here is my code:
My CSS file is named "table.css" ...
<html>
<head>
<title>WEW</title>
<head>
<link href="table.css" rel="stylesheet" type="text/css" />
</head>
<body>
<?php
$con = mysql_connect("localhost","abc123","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("database_ME", $con);
$result = mysql_query("SELECT * FROM id");
$data->set_css_class("table");
echo "<table class="table">
<tr>
<th>id</th>
<th>password</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr onmouseover="this.style.backgroundColor='#ffff66';" onmouseout="this.style.backgroundColor='#d4e3e5';">";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['password'] . "</td>";
echo "</tr>";
}
echo "</table>";
echo "</div>";
mysql_close($con);
?>
</body>
</html>
I'm assuming the CSS file is well written, and that it uses the .table selector.
There's several syntax errors in there, all because you need to escape the inner " like so:
echo "A 'string with several \"nesting\" levels' needs escaping.";
echo "<table class="table">
change to
echo "<table class='table'>
AND
echo "<tr onmouseover="this.style.backgroundColor='#ffff66';" onmouseout="this.style.backgroundColor='#d4e3e5';">";
change to
echo "<tr onmouseover=\"this.style.backgroundColor='#ffff66';\" onmouseout=\"this.style.backgroundColor='#d4e3e5';\">";