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" />
Related
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? :/
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>
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);
On my website i have an admin page where i want to be able to update information in the database, using a form.
This is the code im using to enter information and update what is in my database:
adminform.php
<html>
<head>
<link rel="stylesheet" href="assets/css/main.css" />
</head>
<body>
<header id="header">
<h1>SafeTNet</h1>
<nav id="nav">
<ul>
<li>Admin Page Only</li>
<li></li>
<li>Logout </li>
</ul>
</nav>
</header>
<h1> Select a member </h1>
<br />
<select name="members" onchange="showUser(this.value)">
<option value="">Select a member email</option>
<?php
$query = "SELECT * FROM members";
$mysqli = new mysqli('localhost','root','root','SafeTNetD');
$result = $mysqli->query($query);
while($row = $result->fetch_assoc())
echo '<option value="'.$row["email"].'">'.$row["email"].'</option>';
?>
</select>
<div id="signup">
<h2>Update Your Member Information</h2>
<form method="post" action="admin1.php">
<table>
<tr>
<td>Email</td>
<td><input type="text" name="email" required="required"></td>
</tr>
<tr>
</tr>
<tr>
<td>City </td>
<td><input type="text" name="city"></td>
</tr>
<tr>
</tr>
<tr>
</table>
<br><br>
<div id="buttons">
<input type="submit">
</div>
</body>
</html>
admin1.php
<html>
<head>
<title>Admin</title>
<link rel="stylesheet" href="assets/css/main.css" />
</head>
<body>
<header id="header">
<h1>SafeTNet</h1>
<nav id="nav">
<ul>
<li>Admin Page Only</li>
<li></li>
<li>Logout</li>
</ul>
</nav>
</header>
<br />
<?php
$query = "SELECT * FROM members";
$mysqli = new mysqli('localhost','root','root','SafeTNetD');
$result = $mysqli->query($query);
while($row = $result->fetch_assoc())
echo '<option value="'.$row["email"].'">'.$row["email"].'</option>';
?>
</select>
<br />
<?php
$q=$row["email"];
$mysqli = new mysqli('localhost','root','root','members');
$sql = "SELECT * FROM members WHERE email='".$q."'";
if(array_key_exists('_submit_check', $_POST))
{
$email = $_POST['email'];
$city = $_POST['city'];
$sql = "UPDATE members SET city = '$city' WHERE email = '$q'";
if($mysqli->query($sql) === TRUE)
{
echo 'Record updated successfully<br />';
}
else
{
echo $sql.'<br />' . $mysqli->error;
}
$mysqli->close();
}
?>
<br><br><br>
<footer id="footer">
<img src="logo.jpg" height="50px">
<ul class="copyright">
<li>© SafeTNet. All rights reserved.</li><li> 2016</li>
</ul>
</footer>
</body>
</html>
I can get the form to run but cant get the information to change in the database or echo to the screen.
Thank you in advance.
if(array_key_exists('_submit_check', $_POST))
{
$email = $_POST['email'];
$city = $_POST['city'];
$sql = "UPDATE members SET city = '$city' WHERE email = '$q'";
if($mysqli->query($sql) === TRUE)
{
echo 'Record updated successfully<br />';
}
else
{
echo $sql.'<br />' . $mysqli->error;
}
$mysqli->close();
}
There is no element called '_submit_check' in your form. I guess you forgot the name attribute of your submit-button.
Your script is very vulnerable to SQL-Injection. You really should not simply throw the userinput into your query. You can use mysqli_real_escape_string() or Prepared Statements to protect your application.
To improve the readability of your code you could change the structure a little. In your admin1.php you should do the business logic before outputting any html. So you would first check if the form has been sent, then you do the database operation. The result of the check or the success/error-message of the database operation can be written into a variable until you output the content of your site.
This way everybody who starts reading the code immediately knows 'alright, this script is the target of some form and accesses the database for some write-operation'.
I am new to php and been taking a php programing class this summer semester and i was wondering if someone can help me figure what i need to do in order for things to work.
I am working on a final project and we had to build our on little database on the subject that we wanted. So I choose to do a movie database. And I had used some code already from my_guitar_shop from murach's php book. which is the Murach's php and mysql book. And for some reason when I go to index.php page, I can not get my information to show up in my table on my page.
I will show you the code for my index page
<?php
require_once('database.php');
if (isset($_POST['deleteThis'])) {
$deleteThis = $_POST['deleteThis'];
$sqlDel="DELETE FROM categories WHERE categoryID = $deleteThis";
$temp=$db->exec($sqlDel);
}
// Get all categories
$query = 'SELECT * FROM categories
ORDER BY categoryID';
$categories = $db->query($query);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html
xmlns="http://www.w3.org/1999/xhtml">
<!-- the head section -->
<head>
<title>My Movie Store</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<!-- the body section -->
<body>
<center>
<div id="heading">My Movie Store</div>
</center>
<div id="page">
<div id="header">
<h1>Top Movies</h1>
</div>
<div id="main">
<h1>Movie List</h1>
<table class="content">
<tr>
<th>Title</th>
<th>Date</th>
<th> </th>
</tr>
<?php foreach($categories as $cat) {
?>
<tr>
<td><?php echo $cat['categoryName'];?>
</td>
<form method="post" action="category_list.php">
<input type="hidden" name="deleteThis"
value="<?php echo $cat['categoryID']; ?> " />
<td></td>
<td><input type="submit" value="Delete" />
</td>
</form>
<?php
}
?>
</table>
<br />
<h2>Add a Movie</h2>
<!-- add code for the form here -->
<p>
Add Movie
</p>
<br />
<p>
Movie List
</p>
</div>
<!-- end main -->
<div id="footer">
<p>
©
<?php echo date("2012"); ?>
My Movie Store, created by Kara Holey
</p>
</div>
</div>
<!-- end page -->
</body>
</html>
My database name is called moviedatabase that i had created in localhost/phpmyadmin and my 2 tables underneath the database are called categories and movies. and for some reason thsi is the error that i get on this page.
Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\1306\finalp\index.php on line 82
If anyone can help me that would be great.
If you're using PDO, this probably means your query failed and the output of $db->query() is false.
To avoid this problem, I would suggest enabling exception handling:
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
You can add this to database.php. Doing this will produce a very obvious and hard-to-miss exception if a query fails with details as to why it failed.
$categories = $db->query($query) // this is your problem
I think your foreach() is not receiving a valid array. Try troubleshooting with a static array you create!!
example make
$categories = array("A","B");
foreach($categories as $value){
echo $value."<br/>";
}
Your variable $categories in foreach loop isn't a valid collection that's why php gives you this error. You need to see what $categories really is.
I doubt it might be a resource returned from your query or could be a null.