Displaying details from list of persons using php mysqli - php

I am trying to display a list of persons. On clicking the list , more details on the person must be shown.
members.php: -a page to list all members
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<h1>Our members are</h1>
<?php
include 'config.php';
session_start();
$sql = "SELECT name from users";
$result = $conn->query($sql);
echo '<table>';
if($result->num_rows > 0)
{
while($row = $result->fetch_assoc())
{
echo '<tr>';
$_SESSION['selection'] = $row['name'];
echo '<td>'.$row['name'].'</td>';
echo '</tr>';
}
}
echo '</table>';
$conn->close();
?>
</body>
view.php -the page to show more details:
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<?php
include 'config.php';
session_start();
$pkey = $_SESSION['selection'];
$new = "SELECT * FROM users where name = '$pkey'";
$display = $conn->query($new);
$result = $display->fetch_assoc();
echo 'Name:'.$result['name'];
echo 'Rollno:'.$result['rool_no'];
echo 'Description:'.$result['description'];
$conn->close();
?>
</body>
on using session , always the details of last person in the list is shown.
How to display the corresonding details??

finally you are saving last name in the session variable $_SESSION['selection'] = $row['name']; so you getting the last name .while loop end with last name and you saving it in session.you are not using session as array know that.
my advice is not use session. session is not required here.
while($row = $result->fetch_assoc())
{
echo '<tr>';
$_SESSION['selection'] = $row['name'];
echo '<td>'.$row['name'].'</td>';
echo '</tr>';
}
change these lines in members.php
while($row = $result->fetch_assoc())
{
echo '<tr>';
echo '<td>'.$row['name'].'</td>';
echo '</tr>';
}
and change these line in view.php
<body>
<?php
include 'config.php';
session_start();
$pkey = $_REQUEST['name'];//change this line alone
$new = "SELECT * FROM users where name = '$pkey'";
$display = $conn->query($new);
$result = $display->fetch_assoc();
echo 'Name:'.$result['name'];
echo 'Rollno:'.$result['rool_no'];
echo 'Description:'.$result['description'];
$conn->close();
?>

Related

How can I change the id of an SQL query automatically via PHP?

I'm creating a news website, and when I create a new article, the title/text are registered on the database. The articles are in a dynamic PHP page with header/footer, with the text itself being called from the database . On the index there is a list with links to every article, and I want those links to change the id automatically.
index.php
<?php include "header.php"; session_start();?>
<?php include "slideshow.php"; ?>
<?php include "db.php"; ?>
<br>
<table class="table_index">
<?php
while ($line = mysqli_fetch_array($query)) {
echo '<tr><td class="td_tit"><a href="example_article.php" >'.$line["title_article"].'</a></td></tr>';
echo '<tr><td class="td_subt"><a href="example_article.php" >'.$line["subtitle_article"].'</a></td></tr>';
$_SESSION['id_article'] = $line["id_tb_article"];
}
?>
</table>
<?php include "footer.php"; ?>
example_article.php
<?php
session_start();
include "header.php";
$id_article = $_SESSION['id_article'];
$query = "SELECT title_article, subtitle_article, content_article FROM tb_article WHERE id_tb_article = $id_article";
$conn = mysqli_connect('127.0.0.1:3307', 'root', '', 'article') or die("error");
$result = mysqli_query($conn, $query);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "<div class='titlediv'><h1 class='title'>" . $row["title_article"]. "</h1></div><div class='titlediv'><h3 class='title'>". $row["subtitle_article"]. "</h3></div><div class='textdiv'><p class='text'>" . $row["content_article"]. "</p></div><br>";
}
} else {
echo "0 results";
}
include "footer.php";
?>
You shouldn't use session parameters for this task. Instead use get parameters (or post) here is a little example. Also your code is vulnerable to SQL-Injection, but thats your problem :D
Index.php rewrite your while loop like this
while ($line = mysqli_fetch_array($query)) {
echo '<tr><td class="td_tit">'.$line["title_article"].'</td></tr>';
echo '<tr><td class="td_subt">'.$line["subtitle_article"].'</td></tr>';
}
then fetch the id in example_article.php like this:
$id_article = $_GET['id_article'];

How to show a database row with my search button?

this is my search.php code. i am trying to search wherein the user will input the subscriber id and it will show the FirstName, LastName and address of the accepted SubscriberID
<!doctype html>
<html>
<body>
<?php
$conn=mysqli_connect("localhost","root","kuyahajji","steve");
if (isset($POST['submit-SubscriberID'])) {
$choice = mysqli_real_escape_string($conn,$_POST['$SubscriberID']);
$sql = "SELECT * FROM property WHERE SubscriberID='$choice'";
$result = mysqli_query($conn,$sql);
$queryResult = mysqli_num-rows($result);
if ($queryResult > 0) {
while($row = mysqli_fetch_assoc($result))
{
echo $result;
}
}
else {
echo "0 results";
}
}
?>
</body>
</html>
$row is array. To output required key use [] notation:
echo $row['FirstName'] . $row['LastName'];
Also correct name for a function is mysqli_num_rows (all underscores).

PHP while command missing first entry

I am trying to use the while loop to print multiple entries from a MySQL database. Everywhere I do this it is missing the first item on the list and only manages to print from the second one on.
<?php
require 'db.php';
$udate = $mysqli->escape_string($_POST['date']);
$result = $mysqli->query("SELECT * FROM videos WHERE date='$udate'");
$user = $result->fetch_assoc();
$_SESSION['file_name'] = $user['file_name'];
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>View</title>
<link rel="stylesheet" type="text/css" href="css/nominatorstyle.css">
</head>
<body>
<div class="header">
<h2>Results.</h2>
<form action="watch.php" method='POST'><table>
<?php
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
$vid = $row['vid'];
$source = "uploads/".$row['file_name'];
echo "<tr><td>".$row['vid']."</td>
<td>".$row['file_name']."</td>";
echo "<td><a href=watch.php?
source=$source>Play</a></td></tr>";
}
}else{
echo "0 results";
}
?>
</table></form>
<button class="button-block">Home</button>
</div>
</body>
I'd like it to print all matching criteria.
I'm getting only the entries after the first one.
require 'db.php';
$udate = $mysqli->escape_string($_POST['date']);
$result = $mysqli->query("SELECT * FROM videos WHERE date='$udate'");
/*
This fetch_assoc will get your first row #0 of the query
next fetch_assoc with your loop on the $result
will start as you said from row #1
*/
$user = $result->fetch_assoc(); //your missing row #0
$_SESSION['file_name'] = $user['file_name'];
You missed a "}" at the end of your while loop, that might be a problem.. I also changed your >=1 of your num_rows conditions to > 0. Try this:
if($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$vid = $row['vid'];
$source = "uploads/" . $row['file_name'];
$htmlRow = "<tr>\n";
$htmlRow .= "<td>" . $row['vid'] . "</td>\n";
$htmlRow .= "<td>" . $row['file_name'] . "</td>\n";
$htmlRow .= "<td>" . $row['vid'] . "</td>\n";
$htmlRow .= "<td>Play</td>\n";
$htmlRow .= "</tr>\n";
echo $htmlRow;
}
}

View data to another page in PHP

I have a problem for display data from page to another page.
This is index.php:
<?php
include '../php/connect.php';
$query = mysql_query("SELECT * FROM user
ORDER BY user.id_user DESC") or die(mysql_error());
if(mysql_num_rows($query) == 0){
echo '<tr><td colspan="6">Tidak ada data!</td></tr>';
}else{
while($data = mysql_fetch_assoc($query)){
echo '<tr>';
echo '<td>'.$data['id_user'].'</td>';
echo '<td>'.$data['name'].'</td>';
echo '<td>'.$data['email'].'</td>';
echo '<td>View File</td>';
echo '<td>Konfirmasi</td>';
echo '</tr>';
?>
<?php
}}
?>
This is confirm_pembayaran.php
<?php
include '../php/connect.php';
$query = mysql_query("SELECT * FROM user
WHERE id_user=$id_user") or die(mysql_error());
if(mysql_fetch_array($query) == 0){
echo '<tr><td colspan="6">Tidak ada data!</td></tr>';
}else{
while($data = mysql_fetch_assoc($query)){
echo '<tr>';
echo '<td>'.$data['id_user'].'</td>';
echo '<td>'.$data['name'].'</td>';
echo '<td>'.$data['email'].'</td>';
echo '</tr>';
?>
<?php
}}
?>
The problem is in confirm_pembayaran.php for id_user that i was click from index.php not display in confirm_pembayaran.php. What should i do for confirm_pembayaran.php?
On the index page change the link to something like this ( unless you have set your .htaccess file up to accept links as they were generated )
You should, however, not be using the mysql_ family of functions as they have been deprecated. The code, as it is now is vulnerable to sql injection - I merely posted this to show how to pass the user_id parameter from one page to another which was what ( I think ) you wanted
/* index.php */
Konfirmasi
/* confirm_pembayaran.php */
$user_id=isset( $_GET['user_id'] ) ? filter_input( INPUT_GET,'user_id', FILTER_SANITIZE_STRING ) : false;
$query = mysql_query("SELECT * FROM user
WHERE id_user='{$user_id}'") or die(mysql_error());
First you need to pass your user id in query string correctly like below:
index.php
Changes this line
echo '<td>Konfirmasi</td>';
To
echo '<td>Konfirmasi</td>';
Then you need to change confirm_pembayaran.php
<?php
include '../php/connect.php';
$id_user = $_GET['id_user']; // Add this line for get your user id
$query = mysql_query("SELECT * FROM user
WHERE id_user=$id_user") or die(mysql_error());
if(mysql_fetch_array($query) == 0){
echo '<tr><td colspan="6">Tidak ada data!</td></tr>';
}else{
while($data = mysql_fetch_assoc($query)){
echo '<tr>';
echo '<td>'.$data['id_user'].'</td>';
echo '<td>'.$data['name'].'</td>';
echo '<td>'.$data['email'].'</td>';
echo '</tr>';
?>
<?php
}}
?>
Simplest way is:
In index.php
Konfirmasi
in confirm_pembayaran.php
$id_user = intval($_GET['userId']);

Formatting Table with PHP

I can't figure out how to format my table so that there is a header for each section of attributes. I only see one header when there are actually more that I need to see.
For example I'm able to get DROID 4 by MOTOROLA and all of it's attributes go from Full Retail Price to Voice Dialing. After Voice Dialing there should be a second header for another phone. Clearly my logic is messed up and I have been struggling for about 4 hours on it. I also need the two tables to be side by side, not in a long list like this.
This is what I'm trying to achieve:
My code looks like this:
<?
session_start();
if(!session_is_registered(myusername)){
header("location:main_login.php");
}
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="theme.css" />
</head>
<body>
<?php
include_once 'config.php';
$conf = new config();
mysql_connect($conf->getdbServ(), $conf->getdbUser(), $conf->getdbPwd()) or die(mysql_error());
mysql_select_db($conf->getDB()) or die(mysql_error());
$selectedPhones = $_POST['phones'];
$totalSelected = count($selectedPhones);
//echo $totalSelected;
$idList = "";
for($i=0;$i < $totalSelected; $i++){
$idList .= $selectedPhones[$i] . ",";
}
$idList = substr($idList,0,-1);
$query = "Select name from ".$conf->getproductTbl()." WHERE id='$idList'";
$res=mysql_query($query);
//echo $idList;
$data = mysql_query("SELECT ".$conf->getproductTbl().".id, ".$conf->getproductAttr().".* from ".$conf->getproductTbl()."
LEFT JOIN ".$conf->getproductAttr()." ON ".$conf->getproductTbl().".id=".$conf->getproductAttr().".prodFK
Where ".$conf->getproductTbl().".id IN(" . $idList . ");");
echo "<table width = 100% border = '1' cellspacing = '2' cellpadding = '0'>";
while ($result = mysql_fetch_assoc($res))
{
echo "<th colspan='2'>".$result["name"]."</th>";
}
while ($row = mysql_fetch_assoc($data)) {
echo "<tr><td>";
echo $row["Name"];
echo "</td><td>";
echo $row["value"];
echo "</td></tr>";
}
echo "</table>";
?>
</body>
</html>
1 - You might want to look into MYSQL JOIN to combine your two queries. (http://www.tizag.com/mysqlTutorial/mysqljoins.php)
2 - Use CSS to style your result set.
You can try something like this.. I hope that will help you.
<table>
<tr>
<?php
for($i=0;$i < $totalSelected; $i++){
//$idList .= $selectedPhones[$i] . ",";
?>
<td><?php
$data = mysql_query("SELECT ".$conf->getproductTbl().".id, ".$conf->getproductAttr().".* from ".$conf->getproductTbl()."
LEFT JOIN ".$conf->getproductAttr()." ON ".$conf->getproductTbl().".id=".$conf->getproductAttr().".prodFK
Where ".$conf->getproductTbl().".id = ".$selectedPhones[$i]);
echo "<table width = 100% border = '1' cellspacing = '2' cellpadding = '0'>";
while ($result = mysql_fetch_assoc($res))
{
echo "<th colspan='2'>".$result["name"]."</th>";
}
while ($row = mysql_fetch_assoc($data)) {
echo "<tr><td>";
echo $row["Name"];
echo "</td><td>";
echo $row["value"];
echo "</td></tr>";
}
echo "</table>";
?>
</td>
<?php }?>
</tr>
</table>

Categories