Trying to list the data from mysql to a html table using php in main html file. I've been through all of the other questions on here and I'm sure I have mostly the same methods and code as them.
For some reason (which I suspect has something to do with mysql and not the code) the only result is a blank table with one row and five columns. Each time I try to implement the other codes they just seem to print text onto the site.
I'm very confused as I think I've done the right thing. I've even been able to list the data from mysql through php echo so I know it's there and that I can access it. Really would appreciate some help on this. Thank you.
<table border="1">
<tbody>
<?php
$connect = mysqli_connect("localhost", "daemon", "xampp", "test");
if (!$connect) {
die(mysqli_error());
}
$results = mysqli_query("SELECT title,url,details,file,submission_date FROM input");
while($row = mysqli_fetch_array($results)) {
?>
<td><?php echo $row['title']?></td>
<td><?php echo $row['url']?></td>
<td><?php echo $row['details']?></td>
<td><?php echo $row['file']?></td>
<td><?php echo $row['submission_date']?></td>
<?php
}
?>
</tbody>
</table>
You say this code is in your mail html file? And it is printing out onto the screen? Try changing your file to .php not .html! Php code won't run in a .html file, and will likely output your code directly onto the page.
Mysqli_query expect connection link as first parameter.
$results = mysqli_query($connect, "SELECT title,url,details,file,submission_date FROM input");
Just a quick not so related improvement. You can avoid inserting most of the php opening and closing clauses:
...
while($row = mysqli_fetch_array($results)){
echo "<td>".$row['title']."/td>";
echo "<td>".$row['url']"</td>";
...
}
?>
Use <tr> because table must have atleast one row(<tr>) and one column(<td>)
<table border="1">
<tbody>
<tr>
<?php
$connect = mysqli_connect("localhost", "daemon", "xampp", "test");
if (!$connect) {
die(mysqli_error());
}
$results = mysqli_query("SELECT title,url,details,file,submission_date FROM input");
while($row = mysqli_fetch_array($results)) {
?>
<td><?php echo $row['title']?></td>
<td><?php echo $row['url']?></td>
<td><?php echo $row['details']?></td>
<td><?php echo $row['file']?></td>
<td><?php echo $row['submission_date']?></td>
<?php
}
?>
</tr>
</tbody>
<table border="1">
<tbody>
<?php
$connect = mysqli_connect("localhost", "daemon", "xampp", "test");
if (!$connect) {
die(mysqli_error());
}
$results = mysqli_query($connect, "SELECT title,url,details,file,submission_date FROM input");
if (!$results) {
mysqli_error($results);
die();
}
while ($row = mysqli_fetch_array($results)) {
?>
<tr>
<td><?php echo $row['title'] ?></td>
<td><?php echo $row['url'] ?></td>
<td><?php echo $row['details'] ?></td>
<td><?php echo $row['file'] ?></td>
<td><?php echo $row['submission_date'] ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
Related
I have a database of contributors and I want to delete some of them from my database using php. I wrote the code but it gives errors. I think I linked php and html in a wrong way. once I delete I want to stay in the same page. Also I would like to know if I can make my code more secure. However, this page is accessed by one user who has a username and password and should enter an OTP.
Here is the code :
<?php
/* Delete button */
if(isset($_POST['delete']))
{
$query = "DELETE FROM contributors WHERE id='".$_GET['id']."' ";
$search_result = filterTable($query);
}
function filterTable($query)
{
$connect = mysqli_connect("localhost", "root", "", "volunteedbzlfqf");
$filter_Result = mysqli_query($connect, $query);
return $filter_Result;
}
?>
<!DOCTYPE html>
<html>
<table>
<?php while($row = mysqli_fetch_array($search_result)):?>
<tr>
<td><?php echo $row['id'];?></td>
<td><?php echo $row['password'];?></td>
<td><?php echo $row['fname'];?></td>
<td><?php echo $row['lname'];?></td>
<td><?php echo $row['gender'];?></td>
<td><?php echo $row['phone'];?></td>
<td><?php echo $row['email'];?></td>
<td><?php echo $row['DOB'];?></td>
<td><?php echo $row['city'];?></td>
<td><?php echo $row['degree'];?></td>
<td><?php echo $row['major'];?></td>
<td><?php echo $row['service'];?></td>
<td><?php echo $row['hours'];?></td>
<td><?php echo $row['hrPrice'];?></td>
<td><button name="modify" id="<?php.$row['id'].?>">#</button></td>
<td><button name="delete" id="<?php.$row['id'].?>">X</button></td>
</tr>
<?php endwhile;?>
</table>
</html>
When go inside delete:
if(isset($_POST['delete']))
{
$query = "DELETE FROM contributors WHERE id='".$_GET['id']."' ";
$search_result = filterTable($query);
}
U can check if user is logged or not (the mistake is i know the URL and put like url/table/randomIdToDelete in postman and can delete your database.
if(isset($_POST['delete']))
{
if(!isset($_SESSION['user']){
return;
}
$query = "DELETE FROM contributors WHERE id='".$_GET['id']."' ";
$search_result = filterTable($query);
}
You can use ajax for this in wordpress. See this example link
I explicitly inserted, inside a table row <tr>, 4 columns, but I am seeing an extra that is messing up with my table design. Added a image here, must say I am fetching data from a database, first time at this. This would be the php/html markup:
<form action="test.php" method="GET">
<?php
$query = "SELECT data0, data1, data2, data3 FROM table_name";
$result = mysqli_query($conn, $query);
echo "<table>";
echo "<tr>
<th>Info-1</th>
<th>Info-2</th>
<th>Info-3<th>
<th>Info-4</th>
</tr>";
while($row = mysqli_fetch_assoc($result)) {
echo "<tr>
<td>{$row['data0']}</td>
<td>{$row['data1']}</td>
<td>{$row['data2']}</td>
<td>{$row['data3']}</td>
</tr>";
} ?>
Use PHP codes inside the PHP tags. Don't mess up with HTML tags with PHP. if you follow like this you can avoid such like mistakes
<?php
$query = "SELECT data0, data1, data2, data3 FROM table_name";
$result = mysqli_query($conn, $query); ?>
<table>
<tr>
<th>Info-1</th>
<th>Info-2</th>
<th>Info-3</th> // you are missing close tag here.
<th>Info-4</th>
</tr>
<?php while($row = mysqli_fetch_assoc($result)) { ?>
<tr>
<td><?php echo $row['data0']; ?></td>
<td><?php echo $row['data1']; ?></td>
<td><?php echo $row['data2']; ?></td>
<td><?php echo $row['data3']; ?></td>
</tr>
<?php } ?>
I'm in need of some help. I'm working in dreamweaver and I'm trying to insert values from my MySQL database into a table on my HTML page.
Dreamweaver generated these variables for me from the server behaviours
mysql_select_db($database_connection, $connection);
$query_mwAcc = "SELECT * FROM accounts";
$mwAcc = mysql_query($query_mwAcc, $connection) or die(mysql_error());
$row_mwAcc = mysql_fetch_assoc($mwAcc);
$totalRows_mwAcc = mysql_num_rows($mwAcc);
Now what I need help with is what to put into the while loop for my PHP script, this is what I have so far
<table class="table table-bordered">
<?php while (): ?>
<tr>
<td><?php echo $row['id'] ?></td>
</tr>
<?php endwhile; ?>
</table>
You need to write your fetch command within the while loop itself.
$query_mwAcc = "SELECT * FROM accounts";
$mwAcc = mysql_query($query_mwAcc, $connection) or die(mysql_error());
while($row_mwAcc = mysql_fetch_assoc($mwAcc)) {
?>
<tr>
<td><?php echo $row_mwAcc['id'] ?></td>
</tr>
<?php } ?>
</table>
With an update to use PDO you can change the while loop into a foreach on the query result. PDO should be used over mysql_ interface methods, due to deprecation: Why shouldn't I use mysql_* functions in PHP?
<?php
$dbh = new PDO('mysql:host=...;dbname=...', $user, $pass);
$results = $dbh->query('SELECT * FROM accounts');
?>
<table class="table table-bordered">
<?php foreach ($results as $row): ?>
<tr>
<td><?php echo $row['id'] ?></td>
</tr>
<?php endforeach; ?>
</table>
underneath is a small script which shows if someone has a birthday today.
The only thing what i miss is a line under the name with the text "Happy birthday" It should only appear, ofcourse is someone has a birthday. how can i achieve this?
<html>
<head>
<title>Vandaag Jarig:</title>
</head>
<body>
<table>
<thead>
</thead>
<tbody>
<?php
$connect = mysql_connect("localhost","root", "****");
if (!$connect) {
die(mysql_error());
}
mysql_select_db("my_site_db");
$results = mysql_query
("SELECT * FROM aevinew2_verjaardagen WHERE DAY(geboortedatum) = DAY (CURDATE ()) AND MONTH(geboortedatum) = MONTH(CURDATE())");
if(mysql_num_rows($results) > 0){
while($row = mysql_fetch_array($results)) {
?>
<tr>
<td><?php echo $row['Naam']?></td>
<td><?php echo $row['Afdeling']?></td>
</tr>
<?php
}
}else{
echo "Helaas geen taart vandaag, er is niemand jarig";
}
?>
</tbody>
</table>
</body>
</html>
Something like this? You had syntax error in your code aswell. Missing ; delimiter at end of echo command.
<td><?php echo $row['Naam'].'<br/>Happy Birthday!'; ?></td>
<td><?php echo $row['Afdeling']; ?></td>
Replace line 22 ,23 with these lines. You are missing ';' after echo statement.
<td><?php echo $row['Naam'].'<br/>Happy Birthday!'; ?></td>
<td><?php echo $row['Afdeling']; ?></td>
well, the script is working (it was already), but the only thing i need is an echo under the current echo which is just showing some text.
If i do that it letterly puts that kind of text on top include the text Echo
I am receiving a "Fatal error" once the function fetch_array(mysqli_both) is invoked. The file below on the while instance is exactly where the error is coming from.
<?php
include 'connect.php';
$id = $_SESSION['user_id'];
$res = mysqli_query($mysqli, "SELECT a.name,j.link,j.points, a.submit_time from journal j, article a where a.journal_id = j.id and a.professor_id = $id");
?>
<table>
<tr>
<th>Article</th>
<th>Journal</th>
<th>Points</th>
<th>Time</th>
</tr>
<?php
while ($row = $res->fetch_array(MYSQLI_BOTH)) {
?>
<tr>
<td><?php echo $row[0] ?></td>
<td><?php echo $row[1] ?></td>
<td><?php echo $row[2] ?></td>
<td><?php $date = date_create($row[3]);
echo date_format($date, 'Y-m-d H:i:s'); ?></td>
<tr>
<?php
}
?>
</table>
When i try it on my wampserver i don't have this problem as i activated couple of PHP extensions. Online, i don't think it's easy to activate such extensions. Do you think this error is related to the fact that i didn't invoked the $query as a variable with $mysqli in line 4?
First of all you should use require_once('connect.php'); because it is a needed file for this code.
Also you have to check your $mysqli variable if it's a correct connection to your database.
For more help you might have to post the full Error-Message
you should check result before fetch:
if ($res) {
while ($row = $res->fetch_array(MYSQLI_BOTH)) {
...
...
}
} else {
echo printf("Errormessage: %s\n", mysqli_error($mysqli));
}