Awkward white space above outputed Table - php

I outputed the data from my database in a HTML Table and styled it with CSS. Everything is fine, but there is an awkward White space above the outputed Table. (see picture). What's wrong with my code?
Result:
<html>
<head>
<title>Create new user</title>
<style>
table {
border-collapse: collapse;
width: 100%;
}
th, td {
text-align: left;
padding: 8px;
}
tr:nth-child(even){background-color: #f2f2f2}
th {
background-color: #4CAF50;
color: white;
}
</style>
</head>
<body>
<div>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "users";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "select * from employee";
$result = $conn->query($sql);
echo "<table>";
echo "<th>Identifier</th><th>Name</th><th>Lastname</th>";
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>" . $row["id"] . "</td>";
echo "<td>" . $row["vorname"] . "</td>";
echo "<td>" . $row["nachname"] . "</td>";
echo "</tr>";
echo "</br>";
}
}
else {
echo "0 results";
}
echo "</table>";
$conn->close();
?>
</div>
</body>
</html>
Expected result : no white space above the Table
Many Thanks

Why are you adding a <br>
echo "</br>";
in your while loop.
Try this -
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>" . $row["id"] . "</td>";
echo "<td>" . $row["vorname"] . "</td>";
echo "<td>" . $row["nachname"] . "</td>";
echo "</tr>";
}
} else {
echo "0 results";
}
If this does not fix, try inspecting and see what is adding space. Also check for the padding and margins in the computed view in chrome inspect view.
If nothing works, try setting top:0 for the div and may be for table :)

Related

how do I insert php code into a table style?

I'm a super newbie at PHP, I'm writing (or trying to write) some code. I have been trying to insert a php code into a table data cell but obviously I'm making some syntax mistake. Could you tell me what it is?
<tr>
<td><?php echo $row["name"]; ?>
</td>
<td><?php echo $row["translator"]; ?>
</td>
<?php echo "<td style=\"background-image:url(./assets/images/series/.$row['cover'].); background-repeat:no-repeat; background-size:250px 180px; width: 250px; height: 180px;\">"
</td>
</tr>
$row must be known to PHP before. so you need to do this to work:
<?php
$servername = "localhost"; //your db host
$username = "username";
$password = "password";
$dbname = "dbname";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT name, translator, cover FROM translators";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>";
echo $row["name"];
echo "</td>";
echo "<td>";
echo $row["translator"];
echo "</td>";
echo "<td style='background-image:url("./assets/images/series/".$row['cover'].""); background-repeat:no-repeat; background-size:250px 180px; width: 250px; height: 180px;'>"
echo "</td>";
echo "</tr>";
}
} else {
echo "0 results";
}
$conn->close();
?>

Form not posting to delete SQL Object

I have a page to manage the users and it retrieves users fine but when I try to remove a user nothing happens.
I'm running on php 7.2 with apache2. I've checked logs and nothing breaks or gives a warning/info level. I've tried switching the query and variables and the location however it doesn't seem to take effect.
<!DOCTYPE html>
<html lang="en">
<?php
$con=mysqli_connect("localhost:3306","user","pass","database");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$query = "SELECT `id`,`username`,`email`,`created_at`,`is_admin` FROM users ";
$result = mysqli_query($con,$query);
//if ($_SERVER["REQUEST_METHOD"] == "POST") {
if(isset($_POST['submit'])){
if (empty($_POST["id"])) {
$error = "Make a Selection";
} else {
$delete_id = test_input($_POST["id"]);
$sql = mysqli_query($con,"DELETE FROM users WHERE id='$delete_id'");
echo "<p>" . var_dump($sql) . "</p>";
if($sql){
echo "Deleted";
}
}
}
?>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css">
<style type="text/css">
body{
font: 14px sans-serif;
}
.wrapper{
width: 350px;
vertical-align: middle;
padding: 20px;
}
</style>
</head>
<body>
<form method="post">
<table border='1'><tr><th></th><th>username</th><th>email</th><th>date joined</th><th>Permissions</th></tr>
<?php
while($row = mysqli_fetch_assoc($result))
{
$id = $row['id'];
if ($row['is_admin'] == 1) {
$permissions ="Admin";
}
else {
$permissions = "User";
}
echo "<tr>";
echo '<td><input type="checkbox" name="ID" value="' . $id . '"></td>';
echo "<td>" . $row['username'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "<td>" . $row['created_at'] . "</td>";
echo "<td>" . $permissions . "</td>";
echo "</tr>";
}
echo "</table>";
echo "<BR>";
echo "<BR>";
echo "<BR>";
echo '<input type="submit" name="submit" class="btn btn-primary" value="Delete"> &nbsp &nbsp &nbsp &nbsp';
echo '<input type="submit" class="btn btn-primary" value="Reset PW"><BR/><BR />';
echo "</form>";
mysqli_close($con);
?>
</body>
</html>
I expect the user selected to be deleted but the deletion doesn't happen
Try the following:
replace:
if ($_SERVER["REQUEST_METHOD"] == "POST") {
with:
if(isset($_POST['submit'])){
and add name="submit" to your submit button

PHP, trying to make row ID's work

So I basically been coding this quick stat shower for my game server, but I want it to ID numbers.
<html>
<head>
<meta charset="utf-8">
<title>ExileMod Stats</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<?php
$con=mysqli_connect("******","******","******","******"); //server address, username, password, dbname
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
//check order ascending or descending
if (isset($_GET["order"])) {
$sort = $_GET["order"];
if ($_GET["order"] == "asc"){
$sort = "desc";
$order = "asc";
}
else{
$sort = "asc";
$order = "desc";
}
}
//check filter
if (isset($_GET["name"])) {
$name = $_GET["name"];
}
else {
$name = "name";
}
$list=array('name', 'money', 'score', 'kills', 'deaths', 'uniform', 'vest', 'last_updated_at');
if (in_array($name,$list))
{
//variable ok
}
else
{
$name = "name";
}
$query = mysqli_query($con,"SELECT * FROM account a INNER JOIN player p ON a.uid = p.account_uid ORDER BY a.$name $order");
?><!--//echo "<table border='1'>-->
<style>
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}
td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
tr:nth-child(even) {
background-color: #dddddd;
}
</style>
<table class="table">
<tr>
<?php echo "<th>Player Name</th>";?>
<?php echo "<th>Money</th>";?>
<?php echo "<th>Score</th>";?>
<?php echo "<th>Kills</th>";?>
<?php echo "<th>Deaths</th>";?>
<?php echo "<th>Uniform</th>";?>
<?php echo "<th>Vest</th>";?>
<?php echo "<th>Last Updated</th>";?>
</tr>
<!--//";-->
<?php
while($row = mysqli_fetch_array($query))
{
?><tr class="danger"><?php
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['money'] . "</td>";
echo "<td>" . $row['score'] . "</td>";
echo "<td>" . $row['kills'] . "</td>";
echo "<td>" . $row['deaths'] . "</td>";
echo "<td>" . $row['uniform'] . "</td>";
echo "<td>" . $row['vest'] . "</td>";
echo "<td>" . $row['last_updated_at'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
© HeroesOfGaming 2016 - 2017
</div>
</body>
</html>
So what I am trying to do is put numbers so it counts 1 2 3 4 5 6 7 to however many players are selected from the database? Hopefully this makes sense.
You can do like this
<?php
$i = 0; // <--- Added this
while($row = mysqli_fetch_array($query))
{
$i++; // <--- Added this
?><tr class="danger"><?php
echo "<td>". $i . "</td>"; // <--- Added this
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['money'] . "</td>";
echo "<td>" . $row['score'] . "</td>";
echo "<td>" . $row['kills'] . "</td>";
echo "<td>" . $row['deaths'] . "</td>";
echo "<td>" . $row['uniform'] . "</td>";
echo "<td>" . $row['vest'] . "</td>";
echo "<td>" . $row['last_updated_at'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>

PHP, MySQL and Associative Array Problems...?

I'm following along with the example from w3schools to extract some rows from a mysql db and return the results in a table format, but I'm only getting blank rows (although the correct number of them, oddly enough).
Here is what I have in my php script:
<!DOCTYPE html>
<html>
<head>
<style>
table
{
width: 100%;
border-collapse: collapse;
}
table, td, th
{
border: 1px solid black;
padding: 5px;
}
th
{
text-align: left;
}
</style>
</head>
<body>
<?php
$q = $_GET['q'];
$con = mysqli_connect('localhost', 'root', '123', 'drugsatfda');
if (!$con) {die('Could not connect: ' . mysqli_error($con));}
mysqli_select_db($con, "drugsatfda");
$sql="SELECT * FROM products WHERE DrugName LIKE '"."%".$q."%"."'";
$result = mysqli_query($con,$sql);
$count = 0;
echo "<table>
<tr>
<th>Application Number</th>
<th>Product Number</th>
<th>Form</th>
<th>Strength</th>
<th>Reference Drug</th>
<th>Drug Name</th>
<th>Active Ingredient</th>
</tr>";
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC) && $count < 10) {
echo "<tr>";
echo "<td>" . $row['ApplNo'] . "</td>";
echo "<td>" . $row['ProductNo'] . "</td>";
echo "<td>" . $row['Form'] . "</td>";
echo "<td>" . $row['Strength'] . "</td>";
echo "<td>" . $row['ReferenceDrug'] . "</td>";
echo "<td>" . $row['DrugName'] . "</td>";
echo "<td>" . $row['ActiveIngredient'] . "</td>";
echo "</tr>";
$count++;
}
echo "</table>";
mysqli_close($con);
?>
</body>
</html>
Can someone please help me see where I'm going wrong?
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC) && $count < 10) {
When doing this, you get two conditions, so $row = mysqli_fetch_array($result, MYSQLI_ASSOC) just becomes an expression returning a boolean true, so while $count is less than 10, you get while (true && true).
The solution is to break; when the loop reaches 10 instead.
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
if ($count >= 10) // If we reach 10 iterations, break the loop
break;
echo "<tr>";
echo "<td>" . $row['ApplNo'] . "</td>";
/* and so on */
echo "</tr>";
$count++;
}
You can verify this by doing
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC) && $count < 10) {
var_dump($row);
}
Which will output bool(true); for each iteration, until there are no more rows to fetch (when mysqli_fetch_array() returns null), or when $count is 10 or greater - whichever comes first.
The easier approach
An alternative, is simply just to fetch 10 rows. You can add a LIMIT clause to your SQL, like
$sql="SELECT * FROM products WHERE DrugName LIKE '%".$q."%' LIMIT 10";
This will fetch only 10 rows, meaning that you can just loop through it normally, without having to count
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
echo "<tr>";
echo "<td>" . $row['ApplNo'] . "</td>";
/* and so on */
echo "</tr>";
}
It should also be noted that your code is currently wide open to SQL injections, and you should use prepared statements to guard yourself against this.
References
http://php.net/manual/en/mysqli.prepare.php
How can I prevent SQL injection in PHP?
Please try again...
<!DOCTYPE html>
<html>
<head>
<style>
table
{
width: 100%;
border-collapse: collapse;
}
table, td, th
{
border: 1px solid black;
padding: 5px;
}
th
{
text-align: left;
}
</style>
</head>
<body>
<?php
$q = $_GET['q'];
$con = mysqli_connect('localhost', 'root', '123', 'drugsatfda');
if (!$con) {die('Could not connect: ' . mysqli_error($con));}
mysqli_select_db($con, "drugsatfda");
$sql="SELECT * FROM products WHERE DrugName LIKE '"."%".$q."%"."'";
$result = mysqli_query($con,$sql);
$count = 0;
echo "<table>
<tr>
<th>Application Number</th>
<th>Product Number</th>
<th>Form</th>
<th>Strength</th>
<th>Reference Drug</th>
<th>Drug Name</th>
<th>Active Ingredient</th>
</tr>";
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
if($count >=10)
break;
echo "<tr>";
echo "<td>" . $row['ApplNo'] . "</td>";
echo "<td>" . $row['ProductNo'] . "</td>";
echo "<td>" . $row['Form'] . "</td>";
echo "<td>" . $row['Strength'] . "</td>";
echo "<td>" . $row['ReferenceDrug'] . "</td>";
echo "<td>" . $row['DrugName'] . "</td>";
echo "<td>" . $row['ActiveIngredient'] . "</td>";
echo "</tr>";
$count++;
}
echo "</table>";
mysqli_close($con);
?>
</body>
</html>

how to view different tables in a table interface using php and sqli

I trying to add 3 tables for to fetch data from 3 table individually. I did mine and seem to have errors, kindly need some help, posting my code i have done but seem to be having errors been trying to have three table interface in one page to fetch 3 different tables
<!DOCTYPE html>
<html>
<head>
<style>
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}
td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
tr:nth-child(even) {
background-color: #dddddd;
}
</style>
</head>
<body>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "ecommerce";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
enter code here
$sql = "SELECT id, firstname, lastname, username, email FROM tbl_member";
$results = $conn->query($sql);
if ($results->num_rows > 0) {
echo "<table><tr><th>ID</th><th>Name</th></tr>";
// output data of each row
while($row = $results->fetch_assoc()) {
echo "<tr><td>" . $row["id"]. "</td><td>" . $row["firstname"]. " " . $row["lastname"]. "</td><td>" . $row["username"]. "</td><td>" . $row["email"]. "</td> </tr>";
}
echo "</table>";
} else {
echo "0 results";
}
$conn->close();
?>
</body>
</html>
<html>
<head>
<style>
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}
td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
tr:nth-child(even) {
background-color: #dddddd;
}
</style>
</head>
<body>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "ecommerce";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// $sql = "SELECT id, firstname, lastname, username, email FROM tbl_member";
// $result = $conn->query($sql);
// if ($result->num_rows > 0) {
// echo "<table><tr><th>ID</th><th>Name</th></tr>";
// output data of each row
//while($row = $result->fetch_assoc()) {
// echo "<tr><td>" . $row["id"]. "</td><td>" . $row["firstname"]. " " . $row["lastname"]. "</td><td>" . $row["username"]. "</td><td>" . $row["email"]. "</td> </tr>";
// }
// echo "</table>";
// } else {
//echo "0 results";
$sql = "SELECT memberid, plan, start_date, end_date, FROM tbl_orders";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table><tr><th>ID</th><th>Name</th></tr>";
// output data of each row
while($row= $result->fetch_assoc()) {
echo "<tr><td>" . $row["memberid"]. "</td><td>" . $row["plan"]. "</td><td>" . $row["start_date"]. "</td><td>" . $row["end_date"]. "</td> </tr>";
}
echo "</table>";
} else {
echo "0 results";
}
$conn->close();
?>
</body>
</html>

Categories