How can I get data from MySQL for each user - javascript

I need help. I crated a MySQL database and I connected database with login and registration form. All it´s working. I tried to create page that show all data about user from database. I created tried this code:
<p>Username: <?php echo $_SESSION['username']; ?></p>
<p>Email: <?php echo $_SESSION['email']; ?></p>
<p>Create profile date and time: <?php echo $_SESSION['create_datetime']; ?></p>
But that showed me only username.
I created this from this page.
Can you help me with this? Very much thanks for response!

Inside login.php page you need to assign email and create_datetime to $_SESSION. It should be like
if ($rows == 1) {
while($row = $result->fetch_assoc()) {
$_SESSION['username'] = $row["username"];
$_SESSION['email'] = $row["email"];
$_SESSION['create_datetime'] = $row["create_datetime"];
}
// Redirect to user dashboard page
header("Location: dashboard.php");
}

Related

User Profile Page - PHP

I have an issue that I made a php code and I made a registration system, now, I have successfully made a user page which is like ( ?id=1 )
But if I view ?id=2 it still shows data for user id=1 (me)
How can i solve that?
And how can i hide the Edit profile button for userid2 if I am not userid2 ?
PHP:
<?
session_start();
include 'db.php';
$iDD = $_SESSION['id'];
?>
edit user Login<br><br>
<?php
$DOO = $_GET['id'];
$SEL= $con->prepare("SELECT * FROM users WHERE id=:id");
$SEL->execute(array(
'id' => $iDD
));
$data = $SEL->fetchall();
foreach ($data as $row){
echo "your username:".$row['user']."<br>";
echo "your user password:".$row['pass']."<br>";
echo "your user id:".$row['id']."<br>";
}
?>
Change as follows;
$SEL->execute(array(
'id' => $DOO
));
You can use an if statement like this:
<?php if ($iDD == 1): ?>
edit user Login<br><br>
<?php endif ?>

How to process data using ajax, if there are thousand entries?

I have 1000 entries of users with their email id. I want to send mail to all of them. But I want to do it with ajax.
userdata.php
$con = mysqli_connect("localhost","namename","123456","namename");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}else{
echo "Successfully Connected";
}
$selectsql = "SELECT * FROM table_name";
$rowcount = mysqli_num_rows(mysqli_query($con,$selectsql));
$result = $con->query($selectsql);
?>
<div>
<div><h1>Send Mail</h1></div>
<div>
<p>Total Subscribers: <?php echo $rowcount; ?></p>
</div>
</div>
<div>
<table>
<?php
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td> Email ID </td>";
echo "<td>".$row["email"]."</td>";
echo "</tr>";
}
}
?>
</table>
</div>
It returns 1000 emails. Now I want to send these emails one by one to another page (Mail.php).
I know we can do this with ajax. But how? will it increase server load?
Please guide me through it. I want to know what logic I should use to send that much data to another page via ajax without increasing too much load on the server.
In a single ajax call, send all the email ids so that you won't have 1000 calls to Mail.php.
Then in Mail.php, parse the request and extract all email ids and send mails by iterating all email idsin loop.

Sql Php Ajax - Check if change in database

I currently have a ajax script to run a notiload.php file which runs every 5 seconds to check if theres a change in the database, if theres a change it then outputs "1 New" for a message. However as I will be introducing a notification sound in that file. It will chime every 5 secconds when the file is loaded. Of course this will piss people off. So I need the script to check if theres a change in the database. I have a time and date stamp of every message, with the message being viewed as 2 and not as 1. So if theres a change in the database I will need it to call that file or play a sound.
here is my ajax script inside the main page
<script type="text/javascript">
$(document).ready(function(){
refreshTable<?= $FriendName->id ?>();
});
function refreshTable<?= $FriendName->id ?>(){
$('#NotiLoad<?=$FriendName->id?>').load('elements/notiload.php?id=<?=$FriendName->id?>', function(){
setTimeout(refreshTable<?= $FriendName->id ?>, 5000);
});
}
</script>
<div id="NotiLoad<?= $FriendName->id ?>" style="display: inline-block;"></div>
And here is the php of notiload.php
<?php
session_start();
// redirect to index page if not superuser
$con = mysqli_connect('localhost','root','','');
if (!$con) {
die('Could not connect to Socialnetwk DB: ' . mysqli_error($con));
}
mysqli_select_db($con,"ajax_demo");
?>
<?php
$uid = $_GET['id'];
$mid = $_SESSION['user']['id'];
?>
<?php $usrload = $con->query("SELECT * FROM message WHERE sender = $uid AND recipient = $mid AND seen = '2' OR sender = $mid AND recipient = $uid AND seen = '2'"); ?>
<?php while($FriendName = $usrload->fetch_object()): ?>
<h6 style="display:inline-block;font-size: 10px;color:#37BC9B">1 New</h6>
<?php endwhile;?>

How to manage Php sessions in same page in php

I have a Index page in Php and a login link. When user login to the site then i want to redirect to the same page (index.php). At the place of login link I want to show the username of the user or client. I have a php login code like this
$error = '';
if (isset($_POST['login']) && !empty($_POST['email'])
&& !empty($_POST['password'])) {
$email = $_POST['email'];
$password = $_POST['password'];
$sel_user = "select * from customer where email='$email' AND password='$password'";
$run_user = mysql_query($sel_user);
$check_user = mysql_num_rows($run_user);
if($check_user == 1){
$_SESSION['email']=$email;
echo "<script>window.open('userinfo.php','_self')</script>";
}
else {
$error = 'Invalid username or password';
}
}
in this code page is redirecting to another page after login but i want to redirect it to same page as index.php. And add links like my account and logout links instead of login link in index.php. I have a Php sessions page code like this
if(!isset($_SESSION))
{
session_start();
}
$login_session=$_SESSION['email'];
if(!isset($_SESSION['email']))
{
// not logged in
header('Location: login.php');
exit();
}
How can i make this for example like flipkart.com. Please help me.
You can do one thing:
Go for the code that you have already written for the login, i.e.,
$error = '';
if (isset($_POST['login']) && !empty($_POST['email'])
&& !empty($_POST['password'])) {
$email = $_POST['email'];
$password = $_POST['password'];
$sel_user = "select * from customer where email='$email' AND password='$password'";
$run_user = mysql_query($sel_user);
$check_user = mysql_num_rows($run_user);
if($check_user == 1){
$_SESSION['email']=$email;
echo "<script>window.open('userinfo.php','_self')</script>";
}
else {
$error = 'Invalid username or password';
}
}
After this you do a check of the session variable
if(!isset($_SESSION))
{
session_start();
}
$login_session=$_SESSION['email'];
if(!isset($_SESSION['email']))
{
// not logged in
header('Location: index.php'); //change the redirect page to index.php
//now customize the menu accoringly
echo "<nav class='your-class'>";
echo "<li>";
echo "<ul>Login</ul>";
//Some more menus
echo "</nav>";
exit();
}
else
{
header('Location: index.php'); //now configure the menu accordingly
//now customize the menu accoringly on login
echo "<nav class='your-class'>";
echo "<li>";
echo "<ul>My Account</ul>";
//Some more menus
echo "<ul>Logout</ul>";
echo "</nav>";
}
But as far as I know this is somewhat tiresome and please try to avoid the queries like this. Use PDO instead. Thanks.
Try this
header('location:'.$_SERVER['PHP_SELF']);
instead of
echo "<script>window.open('userinfo.php','_self')</script>";
and for manage links
<ul>
<?php if(isset($_SESSION['email'])){ ?>
<li><a href="logout.php" >Logout</a></li>
<?php }else{ ?>
<li><a href="index.php" >Home</a></li>
<?php }?>
</ul>

Storing information to database using JavaScript prompt function

If I were to do a JavaScript prompt and allow a user to enter the information, how do I store this information into the database?
<?php
if(isset($_GET['cancel_req_id']))
{
echo "<script>var reason = prompt('Reason')</script>";
$res = "<script>document.write(reason)</script>";
$query="UPDATE booking SET status='Cancelled', s_approval='', s_authorize='', cancellation='$res' WHERE req_id='$id'";
$result=$conn->query($query);
echo "<script>alert('Booking cancelled!')</script>";
echo "<script>window.location=('status.php')</script>";
}
?>
this is my current code.

Categories

Resources