PHP show records from mysql - php

Im blocked at following part of code...
i have config.php page, with following code inside:
<?php
// Start the session (pretty important!)
session_start();
// Establish a link to the database
$dbLink = mysql_connect('localhost', 'USER', 'PASS');
if (!$dbLink) die('Can\'t establish a connection to the database: ' . mysql_error());
$dbSelected = mysql_select_db('DATABASE', $dbLink);
if (!$dbSelected) die ('We\'re connected, but can\'t use the table: ' . mysql_error());
$isUserLoggedIn = false;
$query = 'SELECT * FROM users WHERE session_id = "' . session_id() . '" LIMIT 1';
$userResult = mysql_query($query);
if(mysql_num_rows($userResult) == 1){
$_SESSION['user'] = mysql_fetch_assoc($userResult);
$isUserLoggedIn = true;
}else{
if(basename($_SERVER['PHP_SELF']) != 'index.php'){
header('Location: index.php');
exit;
}
}
?>
Now i have another page, with following code inside:
<?php include_once('config.php'); ?>
<?php foreach($_SESSION['user'] as $key => $value){ ?>
<li><?php echo $key; ?> <strong><?php echo $value; ?></strong></li>
<?php } ?>
That code show me all informations stored in database, one by one..
I want to show informations, individualy in my page, something like:
<?php echo $email; ?>
etcetera..
Can someone explain me how to do that?
Thank you

You should output it like
<?php echo $_SESSION['user']['email'] ?>
assuming that 'email' is the column name in the users table.

Just address the relevant field in the associative array:
<?php echo $value['email';?>

Use this in the config file:
$_SESSION['user']['email'] = mysql_fetch_assoc($userResult);
instead of this
$_SESSION['user'] = mysql_fetch_assoc($userResult);
in your code. You can pass as many columns you want to pass from your table.
And in another page use
foreach($_SESSION['user']['email'] as $key => $value)
instead of
foreach($_SESSION['user'] as $key => $value)
in your code.
Remember number of columns you pass in config file, all those columns should be called in this page.

Related

How to Block a user in PHP this is my code

Here is the code which I am trying to block a user from my website. I am using MySQL database and I insert a Banned column in my user table and set their value 1 by default so tell me what I have to do. Table name of this section in MySQL database is Lectures So please help me as soon as possible.
<?php
session_start();
include("header.php");
include("conection.php");
//echo "lec name".$_SESSION["lecname"];
?>
<?php
if($banned == "1")
{
header("Location:lectureaccount.php");
}
else
{
header("Location:banned.php");
}?>
<?php
//if(isset($_SESSION["userid"]))
{
$lec_id = $_SESSION["userid"];
?>
you are doing good.
I am assuming you need to know what your SQL should be to get user data from the database.
<?php
session_start();
include("header.php");
include("conection.php");
//echo "lec name".$_SESSION["lecname"];
if(isset($_SESSION["userid"]))
{
$user_id = $_SESSION["userid"];
$con=mysqli_connect("example.com","username","password","database_name");
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT banned FROM Lectures where user_id = ".$user_id);
while($row = mysqli_fetch_array($result)){
$banned= $row['banned'];
}
mysqli_close($con);
if($banned == "1"){
header("Location:lectureaccount.php");
}
else{
header("Location:banned.php");
}
}
?>
<?php
//if(isset($_SESSION["userid"]))
{
$lec_id = $_SESSION["userid"];

headers not working PHP

Page 1 abc.html.. on submit it will jump to this PHP page .
This is page PHP1.php here i am trying to validate user input if name and id in in data he will be forwarded to fill out second part of registration if not it will just give error.
<?php
session_start();
$_SESSION["acb"] = "good";
$_SESSION['team'] = $_POST['team_name'];
$con = mysql_connect("localhost", "user", "password");
if (!$con)
{die('Could not connect: ' . mysql_error());}
mysql_select_db("mydbName");
if(isset($_POST['team_name'],$_POST['id'])){
$team_name = mysql_real_escape_string($_POST['team_name']);
$id = mysql_real_escape_string($_POST['id']);
if (!empty($team_name)) {
$result= mysql_query("SELECT COUNT(`teamname`) FROM `table` WHERE `teamname`='$team_name' AND `id`='$id'");
$team_result = mysql_fetch_row($result);
if ($team_result[0] == '0') { //if does not exist print failed.
echo 'Varification failed';
} else {
header('Location: http://www.abc.com/REGISTERpart2.php');
}} } ?>
RegisterPART2.php is where i am checking my session exist or not (the one i started in last file). if not i want to redirect back to form one and fill that first then come to registration part 2
`<?php
session_start();
$name = $_SESSION['team']; //a value stored in session which i used on this page
if (($_SESSION["abc"] !== 'good')) {
header('Location: http://www.abc.com/page1.html'); //take back to stage 1 coz user did not fill first part.
}
else{
echo $name. 'you have completed register process part one you may continue!';
}
?>
If you're using the new MySQL version (MySQLi), so the first page will become:
<?php
session_start();
$_SESSION["acb"] = "good";
$_SESSION['team'] = $_POST['team_name'];
$con = new mysqli("localhost", "user", "password", "mydbName");
if (!$con) {
die('Could not connect: ' . $con->error());
};
if (isset($_POST['team_name'],$_POST['id'])) {
$team_name = $con->real_escape_string($_POST['team_name']);
$id = $con->real_escape_string($_POST['id']);
if (!empty($team_name)) {
$result = $con->prepare("SELECT COUNT(`teamname`) FROM `table` WHERE `teamname`='$team_name' AND `id`='$id'");
$result->execute();
$result->bind_result($one,$two,$three,$etc);
$result->fetch();
if (empty($one) and empty($two) and empty($three) and empty(etc)) { // may be and/or (pick one)
echo 'Varification failed';
} else {
header('Location: http://www.abc.com/REGISTERpart2.php');
}
}
}
?>
You may use the following alternative to header.
prinf('<script>window.location = "URL HERE"</script>');
It should do the same thing as header does.

Error in retriving data from database

I'm trying to retrive my first name and last name for viewprofile.php but i'm getting resource ID#5 . I am CREATING session in Login page after successful authentication. And I am trying to use it here. I'm trying to use a session which has been created to fetch the data from the database.
<html>
<h1> My Profile</h1>
<?php
session_start();
require "config.php";
$con = mysql_connect("localhost", $db_user, $db_pass);
if(!$con)
{
die('cound not connect: '. mysql_error());
}
mysql_select_db($db_name, $con);
# include 'new.php';
echo $_SESSION['username'];
#$usname = $_SESSION['username'];
#echo 'local var: ', $usname;
$sql1 = "SELECT * FROM register WHERE uname='".$_SESSION['username']."'" ;
#echo $sql1 ;
$result= mysql_query($sql1)or die(mysql_error());
#echo "res: " . $result;
while($row = mysql_fetch_array($result))
{
echo $row['fname']." ".$row['lname'];
echo"</br>";
}
mysql_close($con);
?>
</html>
I'm getting resource ID#5. Searched numerous places no luck. Kindly help
Unless I'm mistaken mysql_fetch_array will give you values that can be expressed as $row[0], $row[1] etc whereas mysql_fetch_assoc is what you're trying to use to get $row['fname'] etc
Change your code to
while($row = mysql_fetch_assoc($result))
In either case a print_r($row) within your while loop will you how it's made up.

Show delete link on comments

I have a page which shows the comments posted by all users. Here I need to show a delete link on the side of the comments posted by that current logged in user and he should be able to delete that comment too (like in Facebook, Orkut or any Blogging site). The sample code which I have tried is:
<?php
$user_id = 1;
$con = mysql_connect("localhost","root","root");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("blog", $con);
$result = mysql_query("SELECT * FROM replies");
while($row = mysql_fetch_array($result))
{
$replies = $row;
if($replies['poster_id' == $user_id]){
$delete = 'Delete';
}
echo $replies['poster_id']?></a> ¦ <?php echo $replies['reply_text']?> ¦ <?php echo $delete?></div>
<?php echo "<br />";
}
mysql_close($con);
?>
Here I have given the user_id which is hardcoded here. What I got is, delete link is displayed on all comments. I need to display delete link for user_id with "1" only. Can anyone suggest me to get the solution...Thanks in Advance...
if($replies['poster_id' == $user_id]){
need to be
$delete ='';
if($replies['poster_id'] == $user_id){
$delete = 'Delete';
}
you need to unset the $delete variable in every loop, if not once its get a value and present it in every cycle of loop

Running a PHP script at a specific place in the code

Ok, So I have a external php script that get data from a DB and displays it in a table. I want to run it in a specific div in my html so the data gets echoed out in the right place?
Any ideas how to do that?
Html div
<div id="statsContent">
<?php include('updatestats.php'); ?>
</div>
Heres the PHP code.
<?php
//Start session
session_start();
//Make sure user is logged in
require_once('auth.php');
//Include database connection details
require_once('config.php');
//Connect to DB
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link) {
die('Failed to connect to server: ' . mysql_error());
}
//Select database
$db = mysql_select_db(DB_DATABASE);
if(!$db) {
die("Unable to select database");
}
//Create Querys
$query = "SELECT * FROM stats WHERE member_id='" . $_SESSION['SESS_MEMBER_ID'] . "' ";
$result = mysql_query($query);
//Gather the whole row into an array
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$money = $row['money'];
$bank_money = $row['bank_money'];
$ap = $row['ap'];
$exp = $row['exp'];
}
//Now create a table to display the data to the user
echo "<table>
<tr>
<td>Money $$money</td>
<td>Action Points $ap</td>
<td>Experience $exp</td>
</tr>";
?>
you can include PHP script in any tag by calling
include("path_to/myscript.php") or require("path_to/myscript.php")
<div>
<?php include("path_to/myscript.php"); ?>
</div>
<div><?php *whatever you want to do inside the div*?></div>
just include it inside your div by using:
<?php include('filename.php'); ?>

Categories