how to display the data from database for multiple images - php

html
<img src="getdesserticecreamimage.php?itemId=oepd1007" alt="image" id="img1"></li>
samcheckdb.php
<?php
$hostname="localhost";
$username="root";
$password="tiger";
$itemId=intval(\filter_input(\INPUT_GET,'itemId'));
* #var $dbhandle type */
$dbhandle = \mysqli_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
/* #var $select type */
$select= \mysqli_select_db($dbhandle,"sample")
or mysqli_error($dbhandle);
$sql="select subtitle,descript from dessert where itemId='oepd1007'";
$result=mysqli_query($dbhandle,$sql);
$row= mysqli_fetch_array($result);
$subtitle=$row['subtitle'];
$descript=$row['descript'];
mysqli_close($dbhandle);
?>
<html>
<head>
<title>Customer menu card</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<link rel="stylesheet" type="text/css" href="dessert.css">
</head>
<body>
<form id='custdisp' name='custdisp' method='post' action="samcheckdb.php" enctype="multipart/form-data">
<div id="d"><?php echo $descript ?></div>
<div id="s"><?php echo $subtitle?></div>
</form>
</body>
</html>
I'm able to retrieve the corresponding data from the database with this code.But im having n number of images,it is not possible to create that many number of pages and i dont think it is optimal.How can i do this for multiple images???

You can put an onclick like
<img src="getimage.php?itemId=oepsv1007" alt="image" id="img1" onclick="get_detail('oepsv1007');">
then do a ajax call to get the detail using this id.
javascript function
function get_detail(id) {
// ajax call
}

Related

Select from database and echo on html page

Im trying to echo the information that is inserted to my database. As im pretty new to coding php im not really sure how i could manage to do this. My database name is "nyheter" and the table is "post". So i want to selecte the data from db and echo it on the page.
<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("localhost", "root", "", "nyheter");
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
//get results from database
$result = mysqli_query($connection,"SELECT * FROM post");
$all_property = array(); //declare an array for saving property
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf8" />
<link rel="stylesheet" href="nyheter.css" type="text/css" />
<link rel="stylesheet" href="read.css">
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<script src="jquery.dynamicmaxheight.js"></script>
</head>
<body>
<div class="allt-2">
<div class="content">
<img src="http://cdn4.iconfinder.com/data/icons/socialmediaicons_v120/48/google.png"/ alt="" >
<h3><?php echo $Name?></h3>
</div>​
<section class="section js-dynamic-height" data-maxheight="150" >
<p class="dynamic-height-wrap"> Hej
</p>
<button class="js-dynamic-show-hide button" title="Läs mer" data-replace-text="Läs mindre">Läs mer</button>
</section>
<img class="ny-img" src="http://placehold.it/500x320"/>
</div>
</body>
<script>
$(document).ready(function(){
$('.js-dynamic-height').dynamicMaxHeight();
});
</script>
</body>
</html>
You're selecting everything from the database. So you've to use a loop to display the content you've selected from the database.
Write something similar to this:
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "name: " . $row["name"]."<br>";
}
} else {
echo "0 results";
}
mysqli_close($conn); ?>
Hope this was helpful.

Make dynamic page with database?

Here's what I'm trying to do. I'm trying to echo blog posts stored in my database. Simple enough, but I want them to be redirected to view_post.php to show the full post, when they click on the little preview. Here's my code:
<?php
session_start();
require_once('required/db.php');
$_SESSION['admin'] = false;
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>WillWam - Blog</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="assets/css/style.css" type="text/css">
<link rel="ico" href="assets/favicon.ico">
</head>
<body>
<nav><h2 class="title">The Blog</h2></nav>
<?php
$sql="SELECT id,title,author,body FROM posts ORDER BY id DESC";
if ($result=mysqli_query($con,$sql))
{
// Fetch one and one row
while ($row=mysqli_fetch_row($result))
{
printf('<div class="row"><div class="row-inner"><p><strong>%s</strong> | %s |</p></div></div>', $row[0],$row[1],$row[2]);
}
// Free result set
mysqli_free_result($result);
}
mysqli_close($con);
?>
<div class="top-margin wrapper"><div class="container"><p>Administrator? Click here.</p></div></div>
</body>
</html>
How would I go about making the preview row a link dynamically (such as view_post.php?id=1)? What would I put in view_post.php?
Assumming on $row the id is contained you can create the links like this
printf('<a href="view_post.php?id='.$row['id'].'"> <----just put the id there
And on view_port use the value
<div class="row"><div class="row-inner">
<p><strong>%s</strong> | %s |</p></div></div></a>', $row[0],$row[1],$row[2]);

Draw a link from the database and add it to the HTML

I want to make a page where the element will redirect the user to a random page from the database.
I made the HTML code, but I can not deal with PHP and MySQL - I was able to connect to the database, but any attempt to connect the MySQL code with the tag ended in a loss.
Can I count on help writing a PHP script and linking code with ?
<!DOCTYPE html>
<html>
<head>
<link href="https://fonts.googleapis.com/css?family=Montserrat+Subrayada" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Kaushan+Script" rel="stylesheet">
<title>Random it</title>
<meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<header></header>
<div class="random">
Random
</div>
<?php
require_once "connect.php";
$conn = #new mysqli($host, $db_user, $db_password, $db_name);
$sql = "SELECT link FROM randomit ORDER BY RAND()";
?>
<footer></footer>
</body>
</html>
Let's try this:
Put this PHP part at the top of the page;
<?php
// set a default link in case something goes wrong
$DEFAULT_LINK = "default.html";
// connect to DB
require_once "connect.php";
$conn = new mysqli($host, $db_user, $db_password, $db_name);
// set query -> get 1 result only with LIMIT 1
$sql = "SELECT link FROM randomit ORDER BY RAND() LIMIT 1";
$result = mysqli_query($conn, $sql);
if ($result !== false)
$row = mysqli_fetch_assoc($result);
else
$row = false;
// if we have a valid result => use it
// else => use the default
if ($row && isset($row["link"]))
$RANDOM_LINK = $row["link"];
else
$RANDOM_LINK = $DEFAULT_LINK;
?>
And now the HTML part:
<!DOCTYPE html>
<html>
<!-- head part... -->
<body>
<header></header>
<div class="random">
Random
</div>
<footer></footer>
<!-- etc... -->

How to show user the URL once image uploaded

I am looking for a way where when a user uploads a image in PHP it shows them the URL to the file.
My current upload.php is the following:
<?php
if(!empty($_FILES)){
//database configuration
$dbHost = 'localhost';
$dbUsername = 'root';
$dbPassword = '';
$dbName = 'codexworld';
//connect with the database
$conn = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);
if($mysqli->connect_errno){
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
$targetDir = "http://wotm8.net/";
$fileName = $_FILES['file']['name'];
$targetFile = $targetDir.$fileName;
if(move_uploaded_file($_FILES['file']['tmp_name'],$targetFile)){
//insert file information into db table
$conn->query("INSERT INTO files (file_name, uploaded) VALUES('".$fileName."','".date("Y-m-d H:i:s")."')");
}
}
?>
Ignore the database. I don't use that
if you need the index file or anything else then ask.
HTML PAGE
<!DOCTYPE html> <html> <head><meta http-equiv="Content-Type" content="text/html; charset=gb18030">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="google-site-verification" content="_wLD3KO_vuO5czv-n-j9YrXxO3OtQuGc6C51vOaRHMU" />
<title>wotm8.net 路 File Hosting</title>
<link rel="stylesheet" type="text/css" href="css/dropzone.css" />
<script type="text/javascript" src="js/dropzone.js"></script>
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon">
<link rel="stylesheet" href="build.css">
</head>
<body>
<div class="container">
<div class="jumbotron">
<h1 id="logo">wotm8.net</h1>
<p class="lead">Max upload size is 75MiB, read the FAQ</p>
<noscript>
<p class="alert alert-error"><strong>Enable JavaScript</strong> you autist neckbeard, it's not gonna hurt you</p>
</noscript>
<p id="no-file-api" class="alert alert-error"><strong>Your browser is shit.</strong> Install the latestFirefox or Botnet and come back <3</p>
<div class="fileUpload btn btn-primary">
<form action="upload.php" class="dropzone" data-max-size="75MiB">
</div>
</form>
<ul id="upload-filelist"></ul>
<audio id="sickerMeme" src="meme.mp3" />
<script>
var i = 0, text;
var str = "Dude, that's a sick meme.";
function start(){
// document.getElementById("sickerMeme").play();type();
}
/* Credits: https://jsfiddle.net/creed88/VG8MJ/1/ */
function type(){
text = str.slice(0, ++i);
if (text === str) return;
document.getElementById('logo').innerHTML = text;
setTimeout(type, 80);
}
</script>
</div>
<!-- Leo5gg lol -->
<nav>
<ul>
<li>Index</li>
<li>FAQ</li>
<li>Contact</li>
</ul>
</nav>
<script src="main.js"></script>
</div>
</body>
</html>
I think your upload directory is $targetFile
You can echo it out or return it in any way you think is safe
Like after insert into database
echo $targetFile;
EDIT
Your target dir should be a absolute path, not a website link, should be something like /var/www/folder/
I doubt your upload is working in the example you posted.
after you manage to make the upload successful, just echo your domain + relative folder ( if any) + filename.

$_SESSION in PHP is always returning 1

I'm trying to develop a login system in PHP which has faculty_login.php which displays faculty_login_option.inc.php which has a login form and if $_SESSION['f_id'] is set it redirects to faculty_upload_option.php. where faculty details are fetched from faculty_table using $f_id = $_SESSION['f_id'] as the primary key.but $_SESSION['f_id'] is always returning 1 and user is logging in as the user whose f_id is 1.
<?php
//faculty login page.faculty_login.php
//if logged in show upload option/show login option.
require_once 'resources/core.inc.php';//session is set here
require_once 'resources/connect.inc.php';//init db connection
if(isset($_SESSION['f_id'])&&!empty($_SESSION['f_id'])){
require_once 'faculty_upload_option.inc.php';
}
else{
require_once 'faculty_login_option.inc.php';
}
?>
<?php
/* faculty_login_db.php
* Check if the faculty can login or the credentials are wrong.
*/
require_once 'resources/core.inc.php';
require_once 'resources/connect.inc.php';
if(isset($_POST['f_username'])&&isset($_POST['f_password'])){
if(!empty($_POST['f_username'])&&!empty($_POST['f_password'])){
$username = stripcslashes($_POST['f_username']);
$password = stripcslashes($_POST['f_password']);
$result = $conn->prepare("SELECT f_id FROM faculty_table WHERE f_username= :hjhjhjh AND f_password= :asas");
$result->bindParam(':hjhjhjh', $username);
$result->bindParam(':asas', $password);
$result->execute();
$rows = $result->fetch(PDO::FETCH_NUM);
if($result->rowCount() == 1) {
$_SESSION['f_id'] = $rows ;
$_SESSION['f_username'] = $username;
header('Location:faculty_login.php');
}
else{
header('Location:faculty_login.php?username='.$username);
}
}
else{
header('Location:faculty_login.php');
}
}
else{
header('Location:faculty_login.php');
}
?>
<?php
/* faculty_login_option.php
* faculty login page. check if user exists/ use faculty_login_db.php
*/
?>
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<head>
<title>Home</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0 , maximum-scale=1">
<link rel="stylesheet" href="resources/loginstyle.css">
</head>
<body>
<?php
//<img alt="full screen background image" src="images/orange.jpg" id="full-screen-background-image" />
?>
<div id="back">Home</div>
<div id="header">
<h3>FACULTY LOGIN</h3><br>
</div>
<hr>
<div id="container">
<center>
<form action="faculty_login_db.php" method="post">
<input type="text" onFocus="if(this.value=='Username'){this.value=''}" name="f_username" class="buttons" value="<?php
if(isset($_GET['username']))
{
echo $_GET['username'];
}else{echo 'Username';}
?>"><br>
<input type="password" onFocus="if(this.value=='Password'){this.value='';}" name="f_password" class="buttons" value="Password"><br>
<input type="submit" value="Login" class="lbutton">
</form>
</center>
<?php
if(isset($_GET['username'])){
?>
<div id="errormsg">Username or password is invalid.</div>
<?php
}
?>
</body>
<?php
/* this is faculty_upload_option.inc.php
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
require_once 'resources/core.inc.php';
require_once 'resources/connect.inc.php';
if(isset($_SESSION['f_id'])&&isset($_SESSION['f_username'])&&!empty($_SESSION['f_id'])){
$f_id=trim(isset($_SESSION['f_id']));
if(!empty($f_id)){
$result = $conn->prepare("SELECT * FROM faculty_table WHERE f_id=:id");
$result->bindparam(':id', $f_id);
$result->execute();
$rows = $result->fetchAll();
foreach($rows as $db_rows){
$f_username = $db_rows['f_username'];
$category = $db_rows['category'];
$branch = $db_rows['branch'];
}
//page which should be displayed if user logs in.?>
<html>
<head><title><?php echo $f_username; ?></title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0 , maximum-scale=1">
<link href='http://fonts.googleapis.com/css?family=Indie+Flower|Yanone+Kaffeesatz' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="resources/upload_style.css">
</head>
<body><div id="parent">
<div id="header">
<img src="images/no-profile-image.png" width="30%" id="noimg">
<span id="addfont"><h1><?php echo $f_username;?></h1></span>
<h2><?php echo $category;?></h2>
<p><?php echo $branch;?></p>
<center><div class="buttons" id="left">Home</div><div class="buttons" id="right">Logout</div></center>
</div>
Are you destroying session correctly on logout? Are you setting session id before hitting the condition somewhere? What is the value before hitting the comparion condition?

Categories