Get id from URL but database results aren't shown - php

I use the following:
<a href="showlog.php?id=<?php echo $car['car_id']; ?>">
and I get a url that looks like this:
https://example.com/showlog.php?id=AAA1111
So now in the showlog.php file I want select from a table all the lines that have this id value saved as car_id and echo the id (primary key which is different than the car_id), the trn_date, and the kilometers, so I use the following code:
<?php
include 'main.php';
checkLoggedIn($pdo);
$id=$_GET['id'];
$stmt = $pdo->prepare('SELECT id, trn_date, kilometers FROM serv WHERE car_id = '.$id.' ');
$stmt->execute([$_SESSION['id']]);
$cars = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body class="loggedin">
<div class="content">
<h2>Ser</h2>
<div>
<table>
<?php foreach($cars as $car): ?>
<tr>
<td>
<a href="showser.php?id=<?php echo "". $car["id"]. ""; ?>" style="color:DarkCyan; text-decoration: none">
<img src="images/car_service.png" width="40" height="40"> Date: <?="". $car["trn_date"]. ""?>
</a>
</td>
<td>kilometers: <?="". $car["kilometers"]. ""?></td>
</tr>
<tr><th colspan="8"><br/><hr width="100%"></th><tr>
<?php endforeach; ?>
</table>
</div>
</div>
</body>
</html>
The page loads but I get nothing as result. What am I doing wrong? :/

Related

Displaying MySQL output in PHP as rows rather than columns

Good afternoon all :)
I would like to display my MySQL output on a PHP page as rows rather than columns for easier mobile viewing and scrolling (so the user can just scroll down the data instead of across).
I'd read about pivots and transposing but wasn't sure what was the most appropriate way to transform the return data output on the webpage. Please can you advise on what is best to use? It looks like it's easier to do it in PHP rather than MySQL?
I'm using a standard post form, connection PDO, isset, thead, php echo td and th etc.
My current code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta http-equiv="x-ua-compatible" content="ie=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title></title>
<link rel="stylesheet" href="css/style.css" />
</head>
<body>
<h1>Find people</h1>
<form method="post">
<label for="people">Enter person</label>
<input type="text" id="people" name="people">
<input type="submit" name="submit" value="View Results">
</form>
<?php
//error_reporting(-1);
//ini_set('display_errors', 'On');
if (isset($_POST['submit'])) {
try {
require "./config.php";
require "./common.php";
$connection = new PDO($dsn, $username, $password, $options);
$sql = "SELECT *
FROM Basics
WHERE UniqueID = :people";
$people = $_POST['people'];
$statement = $connection->prepare($sql);
$statement->bindParam(':people', $people, PDO::PARAM_STR);
$statement->execute();
$result = $statement->fetchAll();
} catch(PDOException $error) {
echo $sql . "<br>" . $error->getMessage();
}
}
?>
<?php
if (isset($_POST['submit'])) {
if ($result && $statement->rowCount() > 0) { ?>
<h2>Results</h2>
<table>
<?php echo '<table border="1">';?>
<thead>
<tr>
<th>Field 1</th>
<th>Field 2</th>
<th>Field 3</th>
</tr>
</thead>
<tbody>
<?php foreach ($result as $row) { ?>
<tr>
<td><?php echo escape($row["Field 1"]); ?></td>
<td><?php echo escape($row["Field 2"]); ?></td>
<td><?php echo escape($row["Field 3"]); ?></td>
</tr>
<?php } ?>
</tbody>
</table>
<?php } else { ?>
<br>No results found for <?php echo escape($_POST['people']); ?>, as the data has likely not been added yet.
<?php }
} ?>
<!--
<?php if (isset($_POST['submit']) && $statement) { ?>
<?php echo escape($_POST['people']); ?> successfully found.
<?php } ?>
-->
</body>
</html>
My current output:
Current example output:
Output example I would like:
Similar to this example I found (can be in a table or not like below):
Update edit:
Looks like I just needed to use ul and li!
<ul>
<li><b>Name:</b> <?php echo escape($row["Name"]); ?></li>
</ul>
I would use array_walk() to loop over the rows and within that loop, start the tr-tag, loop over the values of the current row, output them as td-elements, exit the td-loop, output the closing tr-tag and exit the tr-loop. Not that fancy solution but simple.
Looks like I just needed to use ul and li!
<ul>
<li><b>Name:</b> <?php echo escape($row["Name"]); ?></li>
</ul>

Inserting and Displaying image from MySQL

I'm trying to display an image which have been stored in MySQL, but haven't been able to get a success just yet. Apparently echoing the table header (img) gives me back something like this
In addition I would like to be able to add the image in the website itself rather than using the phpmyadmin and inserting the image there.
As of now this is the code I have for the standing.php page
<?php
require_once('database.php');
// Get all categories
$query = 'SELECT * FROM categories
ORDER BY categoryID';
$statement = $db->prepare($query);
$statement->execute();
$teams = $statement->fetchAll();
$statement->closeCursor();
?>
<!DOCTYPE html>
<html>
<!-- the head section -->
<head>
<title>NBA</title>
<link rel="stylesheet" type="text/css" href="css/index.css">
<link rel="shortcut icon" type="image/png" href="images/favicon.ico"/>
</head>
<!-- the body section -->
<body>
<main id="standingListMain">
<h1 id="addCategoryh1">Team Standings</h1>
<table id="standingListTable">
<tr>
<th>Team</th>
<th> </th>
</tr>
<?php foreach ($teams as $team) : ?>
<tr>
<td><?php echo $team['categoryID']; ?></td>
<td>
<?php echo $team['categoryName']; ?>
<?php echo $team['img']; ?>
</td>
</tr>
<?php endforeach; ?>
</table>
<br>
</main>
<!-- <footer id="standingListFooter">
<p>© <?php echo date("Y"); ?> NBA</p>
</footer> -->
</body>
</html>
Basically, the user can add or remove a team from the team_list.php page and view it on the standings page
<?php
require_once('../Model/database.php');
// Get all categories
$query = 'SELECT * FROM categories
ORDER BY categoryID';
$statement = $db->prepare($query);
$statement->execute();
$teams = $statement->fetchAll();
$statement->closeCursor();
?>
<!DOCTYPE html>
<html>
<!-- the head section -->
<head>
<title>NBA</title>
<link rel="stylesheet" type="text/css" href="../css/index.css">
<link rel="shortcut icon" type="image/png" href="images/favicon.ico"/>
</head>
<!-- the body section -->
<body>
<main>
<h1 id="addCategoryh1">Teams</h1>
<table id="categoryListTable">
<tr>
<th>Name</th>
<th> </th>
</tr>
<?php foreach ($teams as $team) : ?>
<tr>
<td><?php echo $team['categoryName']; ?></td>
<td>
<form action="delete_team.php" method="post"
id="delete_product_form">
<input type="hidden" name="team_id"
value="<?php echo $team['categoryID']; ?>">
<input id="deleteCategoryList" type="submit" value="Delete">
</form>
</td>
</tr>
<?php endforeach; ?>
</table>
<br>
<h2 id="add_category_h2">Add Team</h2>
<form action="add_team.php" method="post"
id="add_category_form">
<label>Name:</label>
<input type="input" name="name">
<input id="add_category_button" type="submit" value="Add">
</form>
<br>
<p>View Team List</p>
</main>
<footer id="categoryListFooter">
<p>© <?php echo date("Y"); ?> NBA</p>
</footer>
</body>
</html>
Code above is the team_list.php page and below is the code to connect to the database called the add_team.php
<?php
// Get the team data
$name = filter_input(INPUT_POST, 'name');
// Validate inputs
if ($name == null) {
$error = "Invalid team data. Check all fields and try again.";
include('../Error/error.php');
} else {
require_once('../Model/database.php');
// Add the product to the database
$query = 'INSERT INTO categories (categoryName)
VALUES (:team_name)';
$statement = $db->prepare($query);
$statement->bindValue(':team_name', $name);
$statement->execute();
$statement->closeCursor();
// Display the team List page
include('team_list.php');
}
?>
The image above shows the page where u can add or remove a team.
For testing purposes
First you need to know if the Image really exist. Let's assume that in your database you have an image with category Id of 1. Thus create another file, eg "image.php".
(Please ensure that this code runs correctly. I have not tested it but it should work for you).
image.php
<?php
require_once('database.php');
// Get all categories
$query = "SELECT img FROM categories where categoryID=1";
$statement = $db->prepare($query);
$statement->execute();
$num = $statement->rowCount();
if( $num ){
$teams = $statement->fetchAll();
// Ensure to specify header with content type,
// you can do header("Content-type: image/jpg"); for jpg,
// header("Content-type: image/gif"); for gif, etc.
header("Content-type: image/png");
//display the image file
print $teams['img'];
exit;
}else{
//echo no image found with that Category Id.
}
?>
Then in your "standing.php", remove this code:
<?php echo $team['img']; ?>
and replace it with:
<!– "1" is the categoryID id of the image to be displayed –>
<img src="image.php?id=1" />

php file is working on local server but fails when on online server

I did complete a website for online Entrance form for students as my demo project to learn. When I run from my local server it works fine but when I uploaded on webserver and tasted index.php and other files run fine except when user enter his/her symbol no to check if he/she already been registered or not..I have coded a logic
if (exists)>Show admit card
if(don't exist)>show alert box
It works fine in local server but in webserver when I enter value in search box and enter then it shows empty page with no any error.
I have one row in my database. So in case you wanna check here is the symbol no in column =15369-2017-02 . On Entering submit it should show admit card and you can enter any random value other then this .which should show alert box.
Here is my website
https://cmprc.edu.np/condensed/entrance_form_demo/studntreport/main/
This is the code of file which is not responding and showing blank
<html>
<head>
<title>
CDP || Admission Form
</title>
<link href="css/bootstrap.css" rel="stylesheet">
<link href="../style.css" media="screen" rel="stylesheet" type="text/css" />
<body style="background-color: white;">
<?php
include('../connect.php');
$var_value = $_POST['search_value'];
echo $var_value;
$sql="SELECT * FROM entrance WHERE re_value='$var_value'";
$STH = $db->prepare($sql);
$STH->execute(array($var_value));
$User = $STH->fetch();
if (empty($User))
echo "<script>alert('Sorry, you Have not Registered yet');
window.location = 'index.php';</script>";
else
$result = $db->prepare("SELECT * FROM entrance
WHERE re_value = '$var_value' ORDER BY id ASC;");
$result->execute();
for($i=0; $row = $result->fetch(); ){
?>
<link href="../style.css" media="screen" rel="stylesheet" type="text/css" />
<center><h4><i class="icon-edit icon-large"></i> You've Already Registered</h4></center>
<hr>
<center>
<div class="panel panel-default">
<div class="container">
<div class="row">
<div class="col-md-4 seventy">
<img src="img/admit.png"/ >
</div>
<div class="col-md-8 thirty">
<img src="../image/profile/<?php echo $row['pic_value'];?>" class="roundimage2" alt=""/>
</div>
</div>
</div>
<hr>
<table style=" width: 500px;">
<tr>
<td >Roll no: </td>
<td> <?php echo $row['id']; ?></td>
</tr>
<tr>
<td >Name : </td>
<td > <?php echo $row['name_en_value']; ?></td>
<td > Subject: </td>
<td > <?php echo $row['ge_value']; ?> </td>
</tr>
</table>
<br><br>
<h5><i><strong>STUDENTS MUST BRING THIS ADMIT CARD ON THE DAY OF EXAMINATION</strong></i></h5>
<br>
</div>
</center>
<?php
}
?>
</body>
<?php include('footer.php');?>
</html>
Any help? I did almost finished it which I was working continously for 4-5 Days.
It seems that your php is not executed correctly. Maybe try to enable error reporting to get a hint of what's wrong.
Include the following at the beginning of your destination file (my.php)
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);

How to echo value from php to html

how do i echo a value from php to html in a division (div2--a div i created) of a paragraph(p).
for example: i have a value called $greetings and i assign a value of "Welcome" to it
$greetings = "Welcome";
I tried:
`<p>
<?php
$index = "WELCOME";
echo "<div2>$index</div2>";
?>
</p>`
but it doesn't display the value of $greetings--welcome
instead it displays ( $index"; ?> )
HERES MY ENTIRE CODE:
HTML:
<!DOCTYPE html>
<html>
<head>
<title>Tech Planet</title>
<link rel="stylesheet" type="text/css" href="style.css">
<img src="logo.gif" alt="logo" width="150" height="50">
<div class ='seachAndProducts'><form action="header.php" method="GET">
<input id="search" type="text" placeholder="Search Tech Planet...">
<input id="submit" type="submit" value="Search">
</form></div>
<div class = 'navAndFooter'><ul>
<li><b>Home</b></li>
<li><b>Products</b></li>
<li><b>About</b></li>
<li><b>Contact us</b></li>
</ul></div>
<img src="cart.jpg" alt="cart" width="40" height="40">
</head>
<body>
<h3><img src="WelcometoTechPlanet.gif" width="250" height="40"></h3>
<h5><div class ='ourAndFeatProductsHeader'>Our Products</div>
<div class='seachAndProducts'>
<div class='ourProductsList'>Audio Systems</div>
<div class='ourProductsList'>Cameras</div>
<div class='ourProductsList'>Laptops</div>
<div class='ourProductsList'>Memory Cards</div>
<div class='ourProductsList'>Monitors</div>
<div class='ourProductsList'>Phones</div>
<div class='ourProductsList'>Televisions</div>
<div class='ourProductsList'>USBs</div>
<div class='ourProductsList'>All Products</div>
</div><div class ='ourAndFeatProductsHeader'>Featured Products</div>
<div2><img src="8wb50.gif" alt="" width="250" height="200"></div2></h5>
<p><?php
$index = "WELCOME";
echo "<h1>$index</h1>";
?></p>
<!-- used &copy because © displays © -->
<div class = 'navAndFooter'>&copy Tech Planet. All Rights Reserved.</div>
</body>
</html>
Here are a few things to note...
Don't put an h1 tag in the p tag; and you are echoing $index, not $greetings.
Try this
<?php
// Pick the one you want.
$greetings = "Welcome";
$index = "WELCOME";
// Pick the one you want
echo "<h1>$index</h1>";
echo "<h1>$greetings</h1>";
?>
Then, make sure you save it as a .php file, and that you are on a web server with php enabled.

How to retrieve the comment IP

I need a code that displays the IP of the person/guest that commented on my website.
Here's the index page;
<?php
include "core/init.php";
?>
<!DOCTYPE html>
<link rel="stylesheet" type="text/css" href="http://hjalplista.comxa.com/daniel_emelie/style.Css">
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<title>Välkommen till hjälplistan!</title>
<body>
<div id="header">
<div align="right"><img src="http://i.imgur.com/y0aiNIt.jpg"></div>
</div>
<div id="inside">
<center>
<div id="location"><a href='index.php'>Hem</a> - <a href='administrative/adminonlyaccess.php'><i>Admin</i></a><br><br>
</div>
<hr/>
<br>
<div id="text">
<br>
<table border="1" cellpadding="1" cellspacing="1" style="width: 100%;">
<tbody>
<tr>
<td><span style="color:slategray;"><strong><center>Nr</center></strong></span></td>
<td><span style="color:slategray;"><strong><s><center>Fått hjälp</center></s></strong></span></td>
<td><span style="color:slategray;"><strong><center>Namn</center></strong></span></td>
<td><span style="color:slategray;"><strong><center>Uppgift</center></strong></span></td>
<td><span style="color:slategray;"><strong><center>IP-Adress</center></strong></span></td>
</tr>
<?php
include "core/inc/conn.php";
mysql_select_db("comments");
$getquery = mysql_query("SELECT * FROM comments ORDER BY id DESC");
while($rows = mysql_fetch_assoc($getquery))
{
$id = $rows['id'];
$comment_name = $rows['name'];
$comment = $rows['comment'];
echo"<tr><th><font color='lightgray'>$id</font></th>";
echo"<th><font color='red'>Fungerar ej!</font></th>";
echo "<th><u><font color='black'>$comment_name</font></u></th>";
echo"<th><font face='black'>$comment</font></th>";
echo"<th></th></tr>";
}
if(isset($_GET['error']))
{
echo "<p>15 Bokstäver max!";
}
?>
</table>
<br><br>
</body>
<head>
<center>
</div>
<br><br>
<form action="post_comment.php" method="post">
<label> Namn: <input type="text" name="name"></label><br><br>
<label> Uppgift: <input type="text" name="comment"></label><br><br>
<input type="submit" name="post" value="Skicka">
</form>
<br><br>
<br><br>
<?php
echo "<br><br><br>";
echo "<hr/>";
echo "<p> © Wieselgrenskolan. All rights reserved. </p>";
?>
</head>
</html>
<html>
<div align="right">
<!-- START OF HIT COUNTER CODE -->
<br><script language="JavaScript" src="http://www.counter160.com/js.js?img=11"></script><br><img src="http://www.counter160.com/images/11/left.png" alt="Free web hosting" border="0" align="textright"><img alt="Web hosting" src="http://www.counter160.com/images/11/right.png" border="0" align="texttop">
<!-- END OF HIT COUNTER CODE -->
</div>
</html>
And it's supposed to be in the echo "<th></th></tr>";. Do I need a MySQL table for that?
I've created a index in mysql called ''commentip'' but I don't know how to fix the missing parts.
You can get the IP in php in most cases using $_SERVER['REMOTE_ADDR'] However if they are using a proxy or you are using a reverse proxy like cloudflare or incapsula then you can use $_SERVER['HTTP_X_FORWARDED_FOR'].

Categories