I am a newbie in programming. I want to to fetch logged user id from database and store in SESSION and insert that session id in another table. I think I made my day but unfortunately there is an error which is driving me nuts. Any help would be appreciated!
Database btrs
has table booking
which have coulmn name
booking_id,customer_id,route_id
dbConfig.php
<?php
$dbhost = "localhost";
$dbname = "btrs";
$dbuser = "root";
$dbpass = "";
$conn =mysql_connect($dbhost, $dbuser, $dbpass) or die("MySQL Error: " . mysql_error());
mysql_select_db($dbname) or die("MySQL Error: " . mysql_error());
?>
Logincheck.php
<?php
session_start();
include('dbConfig.php');
$error = ''; // Variable To Store Error Message
if (isset($_POST['submit'])) {
if (empty($_POST['email']) || empty($_POST['password'])) {
$error = "email or Password is invalid";
}
else {
$email = $_POST['email'];
$password = $_POST['password'];
$email = stripslashes($email);
$password = stripslashes($password);
$email = mysql_real_escape_string($email);
$password = mysql_real_escape_string($password);
$query = mysql_query("select * from member where password='$password' AND email='$email'");
$count = mysql_num_rows($query);
if ($count > 0 ) {
$row = mysql_fetch_array(mysql_query("select * from member where password='$password' AND email='$email'"));
$id = $row['id'];
// echo $id;
$_SESSION['id']=$id;
header("location: profile.php");
} else {
$error = "Username or Password is invalid";
}
}
}
?>
booking.php
<?php
include"dbConfig.php";
include "logincheck.php";
if (isset($_POST['submit'])) {
$from1 = $_POST['from'];
$destination1 = $_POST['destination'];
$query = mysql_query("SELECT route_id FROM route WHERE pick_from='$from1' AND destination='$destination1'");
$row = mysql_fetch_array($query);
$route_id = $row['route_id'];
// echo $route_id;
if ($row!=NULL) {
$query1 = "INSERT INTO booking(customer_id,route_id) VALUES ('".$_SESSION['id']."','$route_id')";
if ($query1==1) {
echo"very goodddd";
}
}
else {
echo "good";
}
}
?>
You need to start session in profile.php
file.
Add session_start();
in profile.php
or booking.php
at top of line, before include dbconfig file.
For every page where you want to create session variables and/or use session variables, you should start the very top of your code like this...
<?php
if (!isset($_SESSION)) {
session_start();
}
Happy Coding !
In PHP you have to start session first.
So Use this code to start session.
session_start();
Visit this url for more reference.