I have a school web application ..
I want to get all the student name on the log_in.php pages in $_SESSION['allStudent']
for retrieve on further page...
here is my log in .php pages content
include("connect.php");
if(isset($_REQUEST['submit']))
{
$id=$_REQUEST['userName'];
$pass=$_REQUEST['password'];
$sel=mysql_query("select * from login_detail where USERNAME='$id' AND PASSWORD='$pass'")or die(mysql_error());
if($arr=mysql_fetch_array($sel))
{
if(($id==$arr['USERNAME']) && ($pass==$arr['PASSWORD']))
{
session_start();
$_SESSION['id']=$id;
$query = "SELECT * FROM student_personal";
$result = mysql_query($query) or die(mysql_error());
if($result)
{
$_SESSION['allStudent']['']= mysql_fetch_array($result);
}
header("location: viewPages/common/main.php?active=dashboard");
}
}
else
{
echo "<script>alert('please enter the correct id and password');</script>";
}
}
and retrieve into main page
this is my main pages
{
//designed Part
}
<?php
if(isset($_SESION['allStudent']))
{
echo "------------------------------------------<br>";
echo "Student Name--------------------------- DOB<br>";
echo "------------------------------------------<br>";
while($row = mysql_fetch_array($_SESSION['allStudent']))
{
echo $row['STUDENT_NAME']." --------------".$row['DOB']."<br>";
}
}
else
{
echo "No result Found";
}
?>
and this is my table
DB NAME : testssdb
Table Name : student_personal
`SR_NUMBER` int(11) NOT NULL,
`STUDENT_NAME` varchar(30) NOT NULL,
`GENDER` int(11) NOT NULL,
`DOB` varchar(25) NOT NULL,
`RELIGION` varchar(30) NOT NULL,
`MAILING_ADDRESS` text NOT NULL,
`TELEPHONE_NO` varchar(22) default NULL,
`MOBILE_NO` varchar(25) default NULL,
`EMAIL` varchar(30) default NULL,
`PERMANENT_ADDRESS` text,
`MOTHER_TONGUE` varchar(30) default NULL,
`CATEGORY` int(11) default NULL,
`STATUS` int(11) NOT NULL default '1',
`REG_DATE` date NOT NULL,
`FIRST_NAME` varchar(25) NOT NULL,
`LAST_NAME` varchar(25) NOT NULL,
PRIMARY KEY (`SR_NUMBER`)
Here $student = $firstname.$lastName;
So basically i want to store all student records on log in and anyneed of student,i do not want to intrect with the database. only use of session i get the student information
session_start();
$_SESSION['count'] = 1;
$_SESSION['record'][$_SESSION['count']] = array();
$query //retrive ur data here
$result set of ur query
while ($row = mysql_fetch_assoc($result))
{
$_SESSION['record'][$_SESSION['count']]['SR_NUMBER'] = $row["SR_NUMBER"];
$_SESSION['record'][$_SESSION['count']]['STUDENT_NAME'] = $row["STUDENT_NAME"];
$_SESSION['record'][$_SESSION['count']]['GENDER'] = $row["GENDER"];
$_SESSION['record'][$_SESSION['count']]['DOB'] = $row['DOB'];
...// and go on
$_SESSION['count'] = $_SESSION['count'] + 1;
}
foreach($_SESSION['record'] as $key => $value)
{
echo $value['SR_NUMBER'];
echo $value['STUDENT_NAME'];
echo $value['GENDER'];
echo $value['DOB'];
....
}
Related
I am trying to show only categories a user can see by assigning a the category ID inside of the user table.
I am logging in using the following script:
login.php
<?
session_start();
if(isset($_SESSION['user'])){
header("Location:home.php");
exit;
}
$dbh=new PDO('mysql:dbname=dashboardr;host=######', '######', '######');
$email=$_POST['username'];
$password=$_POST['pass'];
if(isset($_POST) && $email!='' && $password!=''){
$sql=$dbh->prepare("SELECT * FROM user_login WHERE username=?");
$sql->execute(array($email));
while($r=$sql->fetch()){
$p=$r['password'];
$p_salt=$r['psalt'];
$id=$r['id'];
$email=$r['username'];
$firstname=$r['firstname'];
$lastname=$r['lastname'];
}
$site_salt="subinsblogsalt";
$salted_hash = hash('sha256',$password.$site_salt.$p_salt);
if($p==$salted_hash){
$_SESSION['user']=$id;
$_SESSION['username']=$email;
$_SESSION['firstname']=$firstname;
$_SESSION['lastname']=$lastname;
header("Location:home.php");
}else{
echo "<h2>Username/Password is Incorrect.</h2>";
}
}
?>
Inside of the home.php file where it shows you are logged in here is where I need to show the categories which are inside of the 'cat_no' column which lised inside of the 'user_login' table.
Here is where I am wanting to GET the users ID and display those categories.
home.php
<?php require_once '../db_con.php';
if(!empty($_GET['user_id'])){
$cat = intval($_GET['user_id']);
try{
$results = $dbh->prepare("SELECT * FROM user_login WHERE FIND_IN_SET(?, cat_no)");
$results->bindParam(1, $cat);
$results->execute();
var_dump($cat);
} catch(Exception $e) {
echo $e->getMessage();
die();
}
$doc = $results->fetchAll(PDO::FETCH_ASSOC);
if($doc == FALSE){
echo '<div class="container">';
echo "<img src='../img/404.jpg' style='margin: 40px auto; display: block;' />";
echo "<h1 style='margin: 40px auto; display: block; text-align: center;' />Oh Crumbs! You upset the bubba!</h1>";
echo 'Get me outta here!';
echo'</div>';
die();
}
}
?>
My tables look like the following:
CREATE TABLE `cat_list` (
`cat_id` int(11) NOT NULL,
`cat_title` varchar(32) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf16 AUTO_INCREMENT=5 ;
CREATE TABLE `user_login` (
`id` int(11) NOT NULL,
`username` text NOT NULL,
`firstname` varchar(30) NOT NULL,
`lastname` varchar(50) NOT NULL,
`password` varchar(64) NOT NULL,
`psalt` text NOT NULL,
`col_no` text NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=12 ;
I am confused on how to GET the logged in users Id and show the categories which are listed out inside of the col_no column in the user_login table.
I'm making a login, with ranks. When you are logged In you receive a welcome message. But that's diffrent for every rank.
my index.php:
<?php
include_once("config.php");
?>
<?php if( !(isset( $_POST['login'] ) ) ) { ?>
<?php
} else {
$usr = new Users;
$usr->storeFormValues( $_POST );
if( $usr->userLogin() ) {
echo "Welcome ";
if ($rank == 1) {
echo "default user";
}
if ($rank == 10) {
echo "developer! right?";
}
else {
echo "error";
}
} else {
echo "Incorrect Username/Password";
}
}
?>
but how do I get the user ranks?
my sql:
CREATE TABLE IF NOT EXISTS `users` (
`userID` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(50) NOT NULL,
`password` varbinary(250) NOT NULL,
`rank` varbinary(250) NOT NULL,
PRIMARY KEY (`userID`,`username`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=8 ;
how do I edit my code so I can get the rank of user?
Thanks!
EDIT AFTER Zsolt Szilagy's ANSWER:
$rank = 'SELECT rank FROM users WHERE userID = "' . mysql_real_escape_string($usr->get_userID()) . '"';
^^doesn't works^^ or am I doing it wrong?
'SELECT rank FROM users WHERE userID = "' . mysql_real_escape_string($usr->get_userID()) . '"';
Depending on your getters, on how the object was loaded, and on your db abstraction.
Basically I want to create a query using a php PDO to check if the table "page" exists in my db "test". I didn't know how to do it and I got some help here.
My code worked perfect ... Until now that I made everything go in classes... and now the var_dump($r2) returns NULL and I don't know what's wrong with the code. I didnt change anything other than putting this into OOP...
Can anyone spot the problem?? because I cant see it.
Thx u
$r1 = $this->db->query('SHOW TABLES LIKE \'page\'');
// Debbug
$r2 = $r1->fetchAll;
var_dump ($r2);
if (count($r1->fetchAll()) > 0 ) {
echo "The table PAGE exists";
}
The full class is the following one
class phase2 {
function __construct () {
$dbFile = 'dbconfig.php';
$this->dbFile = $dbFile;
require_once ("$dbFile");
$step = $_GET["step"];
$username = $DB_USER;
$password = $DB_PASS;
$server = $DB_SERVER;
$dbName = $DB_NAME;
$this->step = $step;
$this->dbFile = $dbFile;
$this->username = $username;
$this->password = $password;
$this->server = $server;
$this->dbName = $dbName;
$db = new PDO ('mysql:host=' .$server.';dbname='.$this->dbName,$this->username,$this->password);
$this->db = $db;
if (empty ($_GET['fot']) ) {
$fOT = 'false';
} elseif ($_GET['true']) { $fOT = 'true'; }
$this->fOT = $fOT;
$this->IDB = $this->handleDatabase( 1 );
$this->IDB2 = $this->handleDatabase( 2 );
$this->IDB3 = $this->handleDatabase( 3 );
}
public function handleDatabase ($num = 1){
// Prepare SQL Statements
$IDB1 = $this->db->prepare(
"CREATE TABLE pages (
id int(11) NOT NULL auto_increment,
subject_id int(11) NOT NULL,
menu_name varchar(30) NOT NULL,
position int(3) NOT NULL,
visible tinyint(1) NOT NULL,
content text NOT NULL,
PRIMARY KEY (id)
)ENGINE=MyISAM AUTO_INCREMENT=7 DEFAULT CHARSET=utf8");
$IDB2 = $this->db->prepare("
CREATE TABLE subjects (
id int(11) NOT NULL auto_increment,
menu_name varchar(30) NOT NULL,
position int(3) NOT NULL,
visible tinyint(1) NOT NULL,
PRIMARY KEY (id)
)ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=utf8");
$IDB3 = $this->db->prepare("
CREATE TABLE users (
id int(11) NOT NULL auto_increment,
username varchar(50) NOT NULL,
hashed_password varchar(40) NOT NULL,
PRIMARY KEY (id)
)ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=utf8");
$name = "IDB".$num;
return isset( $$name)?$$name:false;
}
//Set Option to True or False
function createTablePages ($fOT){
$r1 = $this->db->query('SHOW TABLES LIKE \'page\'');
// Debbug
$r2 = $r1->fetchAll;
var_dump ($r2);
if (count($r1->fetchAll()) > 0) {
echo "The table PAGE exists";
} elseif ($fOT == 'true') {
echo "enteres";
$this->IDB1->execute();
$this->stepFunction (1,false);
}
}
function createTableSubjects ($fOT){
$r2 = $this->db->query('SHOW TABLES LIKE \'subjects\'');
if (count($r2->fetchAll()) > 0 && $fOT == 'false') {
echo "The table SUBJECTS exists ";
} elseif ($fOT == 'true') {
$this->IDB2->execute();
$this->stepFunction (2,false);
}
}
function createTableUsers ($fOT){
$r3 = $this->db->query('SHOW TABLES LIKE \'users\'');
if (count($r3->fetchAll()) > 0 && $fOT == 'false') {
echo "The table USERS exists";
} elseif ($fOT == 'true') {
$this->IDB3->execute();
echo "Would you like to populate all the tables?";
}
}
public function stepFunction ($fOT,$step){
switch ($step) {
case 0:
$this->createTablePages ($fOT);
break;
case 1:
$this->createTableSubjects($fOT);
break;
case 2: $this->createTableUsers ($fOT);
break;
}
}
}
Your query is trying to find a table named page, however, your CREATE TABLE creates a table named pages:
$IDB1 = $this->db->prepare(
"CREATE TABLE pages ("
...
$r1 = $this->db->query('SHOW TABLES LIKE \'page\'');
Unless you actually have both tables, the error lies in one of those two places.
The biggest problem that I can see is you are not creating $db - only inside the construct. Try adding to this section:
class phase2 {
function __construct () {
Adding this statement public $db;:
class phase2 {
public $db;
function __construct () {
Unless I am mistaken, you can't cast a variable from within a method without declaring it first. You'd need to do the same for any other variables you need to access from other methods in that class. Take a read of the basics: http://www.php.net/manual/en/language.oop5.basic.php
Also, I'd suggest turning on error reporting.
You are using require_once() in your constructor. The class will only initialize correctly the first time. After that the config won't load so the variables aren't set.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I put the following code in the browser ?phase=1&step=0&fot=false and I get a black page.
with the PHP error
Undefined variable: fOT in C:\Program Files
(x86)\Zend\Apache2\htdocs\Proj11\1.php on line 219
This is the $this->createTablePages ($fOT); line. If I change it to $this->createTablePages ($this->fOT) I get the following error
Undefined property: phase2::$fOT in C:\Program Files
(x86)\Zend\Apache2\htdocs\Proj11\1.php on line 219
I know that $this->IDB3 = $this->handleDatabase()->$IDB3; in the __constructor is right. How do I do that?
class phase2 {
function __construct () {
$dbFile = 'dbconfig.php';
$this->dbFile = $dbFile;
include_once ("$this->dbFile");
$step = $_GET["step"];
$username = $DB_USER;
$password = $DB_PASS;
$server = $DB_SERVER;
$dbName = $DB_NAME;
$this->step = $step;
$this->dbFile = $dbFile;
$this->username = $username;
$this->password = $password;
$this->server = $server;
$this->dbName = $dbName;
$db = new PDO ('mysql:host=' .$server.';dbname='.$this->dbName,$this->username,$this->password);
$this->db = $db;
if (empty ($_GET['fot']) ) {
$fOT = 'false';
} elseif ($_GET['true']) { $fOT = 'true'; }
$this->IDB = $this->handleDatabase()->$IDB;
$this->IDB2 = $this->handleDatabase()->$IDB2;
$this->IDB3 = $this->handleDatabase()->$IDB3;
}
public function handleDatabase (){
// Prepare SQL Statements
$IDB = $this->db->prepare(
"CREATE TABLE pages (
id int(11) NOT NULL auto_increment,
subject_id int(11) NOT NULL,
menu_name varchar(30) NOT NULL,
position int(3) NOT NULL,
visible tinyint(1) NOT NULL,
content text NOT NULL,
PRIMARY KEY (id)
)ENGINE=MyISAM AUTO_INCREMENT=7 DEFAULT CHARSET=utf8");
$IDB2 = $this->db->prepare("
CREATE TABLE subjects (
id int(11) NOT NULL auto_increment,
menu_name varchar(30) NOT NULL,
position int(3) NOT NULL,
visible tinyint(1) NOT NULL,
PRIMARY KEY (id)
)ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=utf8");
$IDB3 = $this->db->prepare("
CREATE TABLE users (
id int(11) NOT NULL auto_increment,
username varchar(50) NOT NULL,
hashed_password varchar(40) NOT NULL,
PRIMARY KEY (id)
)ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=utf8");
}
//Set Option to True or False
function createTablePages ($fOT){
$r1 = $this->db->query('SHOW TABLES LIKE \'page\'');
if (count($r1->fetchAll()) > 0 && $fOT === 'false') {
echo "The table PAGE exists";
} elseif ($fOT === 'true') {
$this->IDB->execute;
$this->stepFunction (1,false);
}
}
function createTableSubjects ($fOT){
$r2 = $this->db->query('SHOW TABLES LIKE \'subjects\'');
if (count($r2->fetchAll()) > 0 && $fOT === 'false') {
echo "The table SUBJECTS exists ";
} elseif ($fOT === 'true') {
$this->IDB2->execute;
$this->stepFunction (2,false);
}
}
function createTableUsers ($fOT){
$r3 = $this->db->query('SHOW TABLES LIKE \'users\'');
if (count($r3->fetchAll()) > 0 && $fOT === 'false') {
echo "The table USERS exists";
} elseif ($fOT === 'true') {
$this->IDB3->execute;
echo "Would you like to populate all the tables?";
}
}
public function stepFunction ($step, $fOT){
switch ($step) {
case 0:
$this->createTablePages ($fOT);
break;
case 1:
$this->createTableSubjects($fOT);
break;
case 2: $this->createTableUsers ($fOT);
break;
}
}
}
} elseif ($fOT = 'true') {
= is an assignment. You want the comparison operator ==.
if (count($r1->fetchAll()) > 0 && $fOT = 'false') {
You've gotten confused between the assignment operator and the comparison one. You need to change that to:
if (count($r1->fetchAll()) > 0 && $fOT === 'false') {
An assignment operation returns the assigned value, so it's probably not going to give you the result you were expecting, as well as messing up the rest of the logic.
This question already exists:
Closed 10 years ago.
Possible Duplicate:
checklogin condition issue in php
i have this quick question please,
i have this piece of code which isn't working properly, something about the syntax.. could you please help me with it?
i know it may sound stupid enough but i'm trying to understand!
Thanks!
<?php
session_start();
require_once('db.php');
include('functions.php');
if (checkLogin('1 2')) {
echo "hello ".$_SESSION['user_id']." You are now logged in.";
} else if (checkLogin('3')) {
echo "hey tst";
} else {}
?>
function checkLogin($levels)
{
if(!$_SESSION['logged_in'])
{
$access = FALSE;
}
else {
$kt = split(' ', $levels);
$query = mysql_query('SELECT Level_access FROM users WHERE ID = "'.mysql_real_escape_string($_SESSION['user_id']).'"');
$row = mysql_fetch_assoc($query);
$access = FALSE;
while(list($key,$val)=each($kt))
{
if($val==$row['Level_access'])
{//if the user level matches one of the allowed levels
$access = TRUE;
}
}
}
if($access==FALSE)
{
header("Location: login.php");
}
else {
//do nothing: continue
}
}
CREATE TABLE `users` (
`ID` int(11) NOT NULL auto_increment,
`Username` varchar(255) NOT NULL,
`Password` varchar(255) NOT NULL,
`Temp_pass` varchar(55) default NULL,
`Temp_pass_active` tinyint(1) NOT NULL default '0',
`Email` varchar(255) NOT NULL,
`Active` int(11) NOT NULL default '0',
`Level_access` int(11) NOT NULL default '2',
`Random_key` varchar(32) default NULL,
PRIMARY KEY (`ID`),
UNIQUE KEY `Username` (`Username`),
UNIQUE KEY `Email` (`Email`)
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;
Well you could simplify your checkLogin() function
function checkLogin($levels)
{
$access = false;
if (!isset($_SESSION['logged_in']) || !$_SESSION['logged_in'])
return false;
//use mysqli instead mysql
$con = new mysqli("localhost", "username", "password", "database");
$query = $con->query('SELECT Level_access FROM users WHERE ID = "'.$con->real_escape_string($_SESSION['user_id']).'"');
$row = $query->fetch_assoc();
$con->close();
if (in_array($row['Level_access'], explode(" ", $levels))) $access = true;
return $access;
}
This function should return true or false!
After that your code could look like this
session_start();
require_once('db.php');
include('functions.php');
if (checkLogin('1 2')) {
echo "hello ".$_SESSION['user_id']." You are now logged in.";
} else if (checkLogin('3')) {
echo "hey tst";
} else {
header("Location: login.php");
}
Hope this helps you.
Your if statements need parenthesis around them:
if( checkLogin('1 2')) {
^ ^
Try this
<?php
session_start();
require_once('db.php');
include('functions.php');
if (checkLogin('1 2')) {
echo "hello ".$_SESSION['user_id']." You are now logged in.";
} else if (checkLogin('3')) {
echo "hey tst";
} else {}
?>
Run the code in your browser. You'll get an error message. Use that error message to figure out what's wrong. Repeat until you get no error messages, and the program runs as designed.
That's how we debug things in the real world.