Retrieve current users id from database to use in new tabel - php

I have a profile page where I retrieve users information.
Profile.php
<?php
require 'core/init.php';
if(!$username = Input::get('user')) {
Redirect::to('index.php');
} else {
$user = new User($username);
if(!$user->exists()) {
Redirect::to(404);
} else {
$data = $user->data();
}
?>
<h3><?php echo escape($data->username); ?></h3>
<p>Membership No: <?php echo escape($data->id); ?></p>
<p>Full name: <?php echo escape($data->name); ?></p>
<p>Date of birth: <?php echo escape($data->dob); ?></p>
<p>Location: <?php echo escape($data->location); ?></p>
<p>Join date: <?php echo escape($data->joined); ?></p>
<?php
I want to retrieve the id of my user to insert into another table in my order page, so far I have this
oerder.php
<?php
session_start();
require 'core/init.php';
$Band_id = mysql_real_escape_string($_POST['band']);
$user_id = $_SESSION['id'];
$sql = "INSERT INTO orders (band_id,user_id) VALUES('$Band_id', '$user_id')";
mysql_query ($sql, $linkme)
or die ("could not add to database");
?>
currently $user_id = $_SESSION['id']; is not placing the users id in my table orders.
I tried
<?php echo escape($data->id); ?>
and
$user_id = $_GET['id'];
but it dose not work, dose anyone know how to retrieve the users id so I can insert it into the db?

What you could do is save user data to the session
$_SESSION['user_data'] = $user->data();
you could assign it back to $data once you check $_SESSION['user_data'] is set, else re query the model.
And session_start() should also be at the top of every file you want to hold session for.
So something like:
Profile.php
<?php
session_start();
require 'core/init.php';
if(!$username = Input::get('user')) {
Redirect::to('index.php');
exit;
}
if(!isset($_SESSION['user_data'])){
$user = new User($username);
if(!$user->exists()) {
Redirect::to(404);
exit;
}
$_SESSION['user_data'] = $user->data();
}
?>
<h3><?php echo escape($_SESSION['user_data']->username); ?></h3>
<p>Membership No: <?php echo escape($_SESSION['user_data']->id); ?></p>
<p>Full name: <?php echo escape($_SESSION['user_data']->name); ?></p>
<p>Date of birth: <?php echo escape($_SESSION['user_data']->dob); ?></p>
<p>Location: <?php echo escape($_SESSION['user_data']->location); ?></p>
<p>Join date: <?php echo escape($_SESSION['user_data']->joined); ?></p>
oerder.php
<?php
session_start();
require 'core/init.php';
if(!isset($_SESSION['user_data'])){
Redirect::to('index.php');
exit;
}
$Band_id = mysql_real_escape_string($_POST['band']);
$user_id = $_SESSION['user_data']->id;
$sql = "INSERT INTO orders (band_id,user_id) VALUES('$Band_id', '$user_id')";
mysql_query ($sql, $linkme)
or die ("could not add to database");
?>
also you should move over to PDO or mysqli.

Related

Display state header only once, and still display all addresses in that state

I'm looking for a way to display:
<h1>State1</h1>
<h2>City1</h2>
<p>Address1</p>
***if next address is in the same state:*
<h2>City2</h2>
<p>Address2</p>
<hr>
**if next address is NOT in the same state
<h1>State2</h1>
<h2>City3</h2>
<p>Address3</p>
I'm using:
<?php
$query = "SELECT * FROM places";
$select_all_places = mysqli_query($connection, $query);
while($row = mysqli_fetch_assoc()){
$p_state = $row['p_state'];
$p_city = $row['p_city'];
$p_address = $row['p_address'];
?>
<h1><?php echo $p_state ?></h1>
<h2><?php echo $p_city ?></h2>
<p><?php echo $p_address ?></p>
<?php } ?>
Take a look at the code here. I added some semicolons on your echo statements and added the query variable into the mysqli_fetch_assoc method for you. I added some comments as well to help you understand what I have done here.
<?php
$query = "SELECT * FROM places";
$select_all_places = mysqli_query($connection, $query);
// This will be the state we are currently on. Start it as null.
$current_state = '';
while($row = mysqli_fetch_assoc($select_all_places)){
$p_state = $row['p_state'];
$p_city = $row['p_city'];
$p_address = $row['p_address'];
?>
<!-- Check if variable state is equal to current row's state. -->
<?php if($current_state != $p_state): ?>
<!-- Echo state if they aren't equal -->
<h1><?php echo $p_state; ?></h1>
<!-- Assign new current state -->
<?php $current_state = $p_state; ?>
<?php endif; ?>
<h2><?php echo $p_city; ?></h2>
<p><?php echo $p_address; ?></p>
<?php } ?>

show logout link in navigation bar when logged in php

When user logged in, the login link on nav bar should be gone and logout link should appear how should i do it?
index.html:
<nav>
<p class="menu">Home</p>
<p class="menu">Products </p>
<p class="menu">Login</p>
<p class="menu">Logout</p>
</nav>
Login.php file:
<?php
require 'db.php';
session_start();
$password = $mysqli->escape_string($_POST['Pass']);
$email = $mysqli->escape_string($_POST['EmailAdd']);
$result = $mysqli->query("SELECT * FROM Account WHERE Usermail='$email'");
//check email in db
if ($result->num_rows == 0)
{
$_SESSION['message'] = "Email does not exist";
print '<script type="text/javascript">alert("' . $_SESSION['message'] .
'");
</script>';
header("Location: ../register.html");
}
else
{
//get user array
$user = $result->fetch_assoc();
if ($password == $user['password'])
{
$box = "Login successful";
$_SESSION['email'] = $user['Usermail'];
$_SESSION['logged_in'] = true;
echo ("<SCRIPT LANGUAGE='JavaScript'>
window.alert('Login Successful')
window.location.href='../index.html';
</SCRIPT>");
}
else
{
$_SESSION['message'] = "Wrong password";
header("Location: ../account.html");
echo "failed";
echo '<script language="javascript">';
echo 'alert("Wrong password")';
echo '</script>';
}
}
?>
I've gone through some of the post in stack overflow and apply things like if (!isset($_SESSION['email'])) and else statement on my index.php but its not working and i don't know what's the prob
Ps Previously was using index.php, since its not working so i change it back to index.html
<?php
if(!isset($_SESSION['logged_in'])){?>
<p class="menu">Login</p>
<?php }
else
{?> <p class="menu">Logout</p>
<?php } ?>
try the above code, Hope this helps
Assuming your code is correctly validating the credential and setting the auth state in the session $_SESSION['logged_in'] = true;
You can do something like this:
<nav>
<p class="menu">Home</p>
<p class="menu">Products </p>
<?php if (isset($_SESSION['logged_in']) && $_SESSION['logged_in'] == true): ?>
<p class="menu">Logout</p>
<?php else: ?>
<p class="menu">Login</p>
<?php endif; ?>
</nav>
Try this in your nav:
<?php
if($_SESSION['logged_in'] == "true"){
echo '<p class="menu">Login</p>';
}
else {
echo '<p class="menu">Logout</p>';
}
?>
It is not working because you use the .html extension instead of .php
<nav>
<p class="menu">Home</p>
<p class="menu">Products </p>
<?php if(empty($_SESSION['logged_in'])){ ?>
<p class="menu">Login</p>
<?php } else{ ?>
<p class="menu">Logout</p>
<?php } ?>
</nav>
This should work:
<nav>
<p class="menu">Home</p>
<p class="menu">Products</p>
<p class="menu">
<?php if(isset($_SESSION['logged_in]) && $_SESSION['logged_in]) {?>
Logout
<?php } else { ?>
Login
<?php } ?>
</p>
</nav>

Create a new HTML page and add content using PHP

I am trying to create new page (file) using PHP.
I have opened and created the page and I have add it content from MySQL but I couldn't get id variable from the previous page.
My admin panel is like this:
<?php
if(isset($_POST['sayfaekle'])) {
$baslik = $_POST['baslik'];
$text = $_POST['editor1'];
$sql = "INSERT INTO sayfa (sayfa_baslik , sayfa_text) VALUES ('$baslik' , '$text')";
$db->exec($sql);
$id=$db->lastInsertId();
echo "basarılı";
include "sayfagonder.php";
}
?>
In here, I get baslik and yazi and insert into database successfully. I also get id successfully.
In other page that creates a new PHP page codes are like this:
<?php
$_GET['id'] = $id;
ini_set('display_errors','0');
error_reporting(0);
$pagename = '../'.$baslik. '.php' ;
$myfile = fopen( $pagename , "w");
$text = ' <?php
include "config.php";
$id = $_GET["id"];
echo $id;
$sayfasor=$db->prepare("select * from sayfa where sayfa_id=$id");
$sayfasor->execute(array(0));
$sayfacek=$sayfasor->fetch(PDO::FETCH_ASSOC);
include "header.php";
?>
<div class="content">
<div class="row">
<div class="pageana">
<div class="col-sm-8">
<h1><?php echo $sayfacek["sayfa_baslik"]; ?></h1>
<?php echo $sayfacek["sayfa_text"]; ?>
</div>
</div>
</div>
</div>
</div>
<?php
include "footer.php";
?> ';
fwrite($myfile , $text);
fclose($myfile);
?>
And I get this error:
Notice: Undefined index: id in C:\xampp\htdocs\doktor\deneme56.php on line 5
I can't get the value of id..
You are fetching the value of $id but that value is not persisting. Set the value in a php $_SESSION variable then access it on new page. Something like this -
<?php
session_start();
if(isset($_POST['sayfaekle'])) {
$baslik = $_POST['baslik'];
$text = $_POST['editor1'];
$sql = "INSERT INTO sayfa (sayfa_baslik , sayfa_text) VALUES ('$baslik' , '$text')";
$db->exec($sql);
$id=$db->lastInsertId();
$_SESSION['id'] = $id;
echo "basarılı";
include "sayfagonder.php";
}
?>
Now you can access this value on new page like this -
<?php
session_start();
$_GET['id'] = $_SESSION['id'];
ini_set('display_errors','0');
error_reporting(0);
$pagename = '../'.$baslik. '.php' ;
$myfile = fopen( $pagename , "w");
$text = ' <?php
include "config.php";
$id = $_GET["id"];
echo $id;
$sayfasor=$db->prepare("select * from sayfa where sayfa_id=$id");
$sayfasor->execute(array(0));
$sayfacek=$sayfasor->fetch(PDO::FETCH_ASSOC);
include "header.php";
?>
<div class="content">
<div class="row">
<div class="pageana">
<div class="col-sm-8">
<h1><?php echo $sayfacek["sayfa_baslik"]; ?></h1>
<?php echo $sayfacek["sayfa_text"]; ?>
</div>
</div></div>
</div>
</div>
<?php
include "footer.php";
?> ';
fwrite($myfile , $text);
fclose($myfile);
?>
Now coming to your code. When you already have value in Session variable you don't need to assign in $_GET variable. Access it directly.

PHP user profile page doesn't show tables

I'm working on a website with where a user profile page is part of.
This is my code on top of the page:
<?php
require 'includes/connect.inc.php';
session_start();
$id = $_SESSION['user_id'];
$userResult = mysqli_query("SELECT * FROM users where user_id ='$id'");
while($userRow = mysqli_fetch_array($userResult)) {
$avatar = $userRow['avatar'];
$locatie = $userRow['locatie'];
$info = $userRow['info'];
$email = $userRow['email'];
$username = $userRow['username'];
}
?>
And this is the part where it has to display the rows from the database (the part the user can see when he is on his own profile page)
<?php if(isset($_SESSION['username'])){
?>
<div class="col-lg-6">
<h4>Naam:</h4>
<p><?php echo $username; ?></p>
<h4>Locatie:</h4>
<p><?php echo $locatie; ?></p>
<h4>E-mailadres:</h4>
<p><?php echo $email; ?></p>
<h4>Informatie:</h4>
<p><?php echo $info; ?></p>
Klik hier om uw profiel te bewerken.
</div>
<div class="col-lg-6">
<?php echo "<img class='useravatar' src='/avatar/user" . $id . ".jpg'></img>"; ?>
<?php
} else {
echo "U hebt geen bevoegdheid om deze pagina te bekijken";
}
?>
Why does it not show anything?
this is the verify page from the login form:
$query = mysql_query("SELECT * FROM users WHERE username='$username' AND password='$password'") or die(mysql_error());
$row = mysql_num_rows($query) or die(mysql_error());
if($row == 1){
$queryFetch = mysql_fetch_array($query);
session_start();
$_SESSION['username'] = $queryFetch['username'];
$_SESSION['role'] = 'user';
$_SESSION['email'] = $queryFetch['email'];
$_SESSION['user_id'] = $queryFetch['user_id'];
if(isset($_SESSION['username'])){
header ('location: /usercp.php');
}
}
}
?>

Prbolem in view when refreshing page

I am retrieving a column from my database and storing that values in an array in my controller.
I am passing that array to the view page, and displaying that values to the user.
All things upto this are working fine.
But the problem is when I am refreshing the page its not showing the values obtained by the controller from the model and just showing blank page..\
I also tried to store the value in a session and use that value but that doesn't seem to work for me.. :(
here is my controller code :-
function full_post_view(){
$data= array(
'ticket_id' => $this->input->post('ticket_id')
);
$this->session->set_userdata($data);
$ticket_id = ($this->input->post('ticket_id')) ? $this->input->post('ticket_id') : $this->session->userdata('ticket_id');
// $post_content = $this->session->userdata('post_content');
echo $ticket_id;
$this->load->model('helpdesk_model');
$all_comments = $this->helpdesk_model->fetchComments($ticket_id);
$is_post_closed = $this->helpdesk_model->fetchPostStatus($ticket_id);
if($all_comments->num_rows > 0) {
foreach ($all_comments->result() as $comments_value) {
$comments = $comments_value->comments;
}
$count=1;
Template::set('all_comments',$all_comments);
Template::set_view('helpdesk/full_post_view');
}
else {
$count=0;
Template::set('post_content',$this->input->post('post_content'));
}
Template::set('is_post_close',$is_post_closed);
Template::set('ticket_id',$ticket_id);
Template::set('is_post_closed',$is_post_closed);
Template::set('post_content',$post_content);
Template::set('count',$count);
Template::set('is_post_closed',$is_post_closed);
Template::render();
}
here is my view :-
<?php $post=Template::get('post_content'); ?>
<?php $ticket_id=Template::get('ticket_id'); ?>
<?php $is_close = Template::get('is_close'); ?>
<h4>Your Problem :- </h4>
<?php echo $post; ?>
<hr/>
<?php foreach ($is_post_closed->result() as $value) {
$is_close = $value->is_close;
} ?>
<?php if(Template::get('count') > 0) : ?>
<?php foreach($all_comments->result_array() as $commentsRow) : ?>
<?php echo word_wrap($commentsRow['comments'],15); ?>
<?php echo " by->"; ?>
<?php echo $commentsRow['username']; ?>
<?php echo $commentsRow['role_name']; ?>
<hr/>
<?php endforeach; ?>
<?php else : ?>
<br/>
No Comments Yet
<?php endif; ?>
<?php if($is_close == 0 ) : ?>
<?php echo form_open('helpdesk/newComment'); ?>
<?php echo form_hidden('ticket_id',$ticket_id); ?>
<?php echo form_hidden('post_content', $post); ?>
<?php echo form_textarea('comment_from_user'); ?>
<?php echo form_submit('submit', 'Comment '); ?>
<?php echo form_close(); ?>
<?php endif; ?>
<?php if($is_close==0) : ?>
<?php echo form_open('helpdesk/closePost'); ?>
<?php echo form_hidden('ticket_id', $ticket_id); ?>
<?php echo form_hidden('post_content', $post); ?>
<?php echo form_submit('submit','close post'); ?>
<?php echo form_close(); ?>
<?php else : ?>
<?php echo form_open('helpdesk/reopenPost'); ?>
<?php echo form_hidden('ticket_id', $ticket_id); ?>
<?php echo form_submit('submit','Reopen post'); ?>
<?php echo form_close(); ?>
<?php endif; ?>
Edit :
I want to ask that how to load the database content again to the view when user refreshes the page.
Edited my Controller and view code
When you refresh the page, do you resend the form ? I'm asking this because from your controller code, it looks like $ticket_id is fetched from _POST and you use pass this variable to the model. If you don't resend the form, the $ticket_id is null and nothing is fetched from DB.
Edit:
Replace in your controller:
$ticket_id = $this->input->post('ticket_id');
with:
$ticket_id = ($this->input->post('ticket_id')) ? $this->input->post('ticket_id') : $this->session->userdata('ticket_id');
and make sure on each request ticket_id exists at least in one from the two possibilities (POST and SESSION)
Solved :)
the problem was when I was refreshing the page, ticket_id was set to 0 and that value was passed as a post .
So the session value was the new ticket_id i.e., 0 and so it was displaying nothing..
I did this:-
if(isset($_POST['ticket_id'])) {
$data= array(
'ticket_id' => $this->input->post('ticket_id'),
'post_content' => $this->input->post('post_content')
);
$this->session->set_userdata($data);
}
$ticket_id =$this->session->userdata('ticket_id');
$post_content =$this->session->userdata('post_content');
i.e., I am storing ticket_id in session when post in set i.e., for the first time. When page is refreshed next time ticket_id post is not set and data is taken from session.

Categories