The code below a member and guest counter . The code runs perfectly however when I add a simple column (online) to the table active_members the entire code stops working (apart from the active_guests as this a separate table)
I have looked tirelessly trying to catch anything that may be causing this I have included the table screenshot below . I would to had add new column (online)that is a varchar to the table members_online and then query in the if SESSION below for that new column .
Why does the code stop working , Can somebody possible see the cause or solution?
As soon as i remove the online column is code runs perfect again .
$sqlt2 query below is the line I want to index the new online column .
include('..\db.php');
$con = mysqli_connect($dbsrvname, $dbusername, $dbpassword, $dbname);
//declare variables
$guest_timeout = time() -1 * 60;
$member_timeout = time() -1 * 60;
$guest_ip = $_SERVER['REMOTE_ADDR'];
$time = time();
// if the session is set PUTS THE USER IN MEMBERS TABLE
if(isset($_SESSION['CurrentUser'])){
//if user is logged in
$sqlt = mysqli_query($con,"DELETE FROM active_guests WHERE guest_ip='".$guest_ip."'");
**//I WANT TO ADD TO THE ONLINE COLUMN AND REPLACE A SIMPLE STRING INTO ONLINE**
$sqlt2 = mysqli_query($con,"REPLACE INTO active_members VALUES ('".$_SESSION['CurrentUser']."','".$time."')");
$name = $_SESSION['CurrentUser'] ;
}else{
//if user not in a session PUTS THE USER IN GUESTS TABLE
$sqlt3 = mysqli_query($con,"REPLACE INTO active_guests (guest_ip,time_visited)VALUES ('".$guest_ip."','".$time."')");
}
//execute querys
$sqlt4 = mysqli_query($con,"DELETE FROM active_guest WHERE time_visited < ".$guest_timeout);
$sqlt5 = mysqli_query($con,"DELETE FROM active_members WHERE time_visited < ".$member_timeout);
$sqlt6 = mysqli_query($con,"SELECT guest_ip FROM active_guests");
$sqlt7 = mysqli_query($con,"SELECT username FROM active_members");
$online_guests = mysqli_num_rows($sqlt6);
if(isset($_SESSION['CurrentUser'])){
$sqlt7 = mysqli_query($con,"SELECT username FROM active_members");
if($sqlt7->num_rows){
while($row = $sqlt7->fetch_object())
{
echo '<pre>',$row->username,'</pre>' ;
}
//$result->free();
}
}else{
echo "Login to see members-online list";
}
$online_members = mysqli_num_rows($sqlt7);
?>
// display results
<div class="container">
<p>_________________________</p>
<p>online Guests : <?php echo $online_guests ; ?></p>
<p>online Members : <?php echo $online_members ; ?></p>
</div>
Set column "online"s "Default/Expression" to null, hopefully it'll will work!
Related
I have read multiple stack overflow answers but still cant find one specific to my needs. Some are outdated. I am using bootstrap and I want to display the first set of records which I am doing by using 'LIMIT 9'. I just have no idea how to get the next 10 records and every 10 records after that to display once the link at the bottom has been clicked. I don't want to open a new page for every 10 records.
`<div class="container-fluid">
<div class="row row-fluid">
<div class="col-xs-12 col-sm-12 col-md-8 offset-md-2 col-lg-8 offset-lg-2">
<h2>List of current Database entries</h2>
<?php
$servername = "localhost";
$username = "xxxxx";
$password = "xxxxxx";
$dbname = "xxxxxxxxx";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, business_name, email, website, phone FROM table LIMIT 0, 9";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table class='table table-striped'>";
echo "<thead>";
echo "<tr>";
echo "<th>ID</th><th>Business Name</th><th>Email</th><th>Website</th><th>Phone</th>";
echo "</tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>"."id : ". $row["id"]."</td>
<td>".$row["business_name"]."</td>
<td>".$row["email"]."</td>
<td>".$row["website"]."</td>
<td>".$row["phone"]."</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
} else {
echo "0 results";
}
$conn->close();
?>
Next 10
</div>
</div>
</div>
That is what I have so far. I would really appreciate some help
This image shows how it looks nowimage of how it looks I dont want people to leave the page for the results. Thank you.
You would usually get a GET/POST parameter specifying current "page".
For example
$page = isset($_GET['page']) ? intval($_GET['page']) : 1; // current page, default is 1
$perPage = 10; // records per one page
$fromLimit = ($page - 1)*$perPage;
$toLimit = $perPage*$page;
$sql = ".... LIMIT $fromLimit, $toLimit";
I have a feeling that you are new to php programming, so be sure to take time and check this:
How to prevent SQL injection
The point is you need a dynamic parameter that you give to a php page so you can dynamically change the content.
Links to your pages would look something like that:
1
2
3
please try to remplace this code:
$sql = "SELECT id, business_name, email, website, phone FROM table LIMIT 0, 9";
to:
if ($_GET['pagenum'] > 0){
$a = (int) filter_var($_GET['pagenum'],FILTER_SANITIZE_NUMBER_INT);
}else{
$a = 0;
}
$Startnumber = ($currectid * 10); $limit_read = ($Limit_number + 10);
$sql = "SELECT id, business_name, email, website, phone FROM table LIMIT ".$Startnumber .", ".$limit_read ;
I am new to Android Programming.I am trying to build an Android Project which is connected to an online MySQL Server using PHP to pass the data from Android device to the online Server.I have two tables one of which contains the login details of the employee i.e.EMPLOYEE_DATA,
and the 2nd table contains the respective employee details i.e.EMPLOYEE_DETAILS.
So, I have 2 PHP files : the 1st PHP script is for the Login of the employee which runs when the employee clicks on the Login Button.This script needs to get the username of the employee which is logged in and pass it to the second PHP file.The 2nd PHP script is used for inserting the details of the respective employees. Now I need to pass the username from the 1st PHP scriptto the 2nd PHP script and insert that username to the "USERNAME" column of the table "EMPLOYEE_DETAILS". Can anyone please suggest how to proceed. I have tried using $_SESSIONS but am not getting the desired result. I have been stuck with this for a long time. Any help would be appreciated.
Edit 1 :
Login.php
<?php
$_SESSION['user_mobile'] = $_POST["mobile_num"];
$_SESSION['user_pass'] = $_POST["password"];
require "conn.php";
$user_mobile='';
$user_pass='';
$user_mobile = $_SESSION['user_mobile'];
$user_pass = $_SESSION['user_pass'];
$mysql_qry = "select * from employee_data where mobile like '$user_mobile' and password like '$user_pass';";
$result = mysqli_query($conn,$mysql_qry);
if(mysqli_num_rows($result) > 0 ) {
$row = mysqli_fetch_assoc($result);
$_POST['user'] = $name;
$name= $row["name"];
echo "Login successful.<br /> Welcome " ;
echo $name;
$insertName = "INSERT INTO employee_details(name) VALUES ('$name');";
$resultName = mysqli_query($conn,$insertName);
$shift = "SELECT CASE WHEN CURTIME() < 7 THEN 'Morning'
WHEN CURTIME() < 12 THEN 'Afternoon'
WHEN CURTIME() <17 THEN 'Evening'
ELSE 'Night'
END;";
$insertShift = "insert into employee_details(shift) values ('$shift');";
$resultShift = mysqli_query($conn,$insertShift);
}
else{
echo "Login failed.";
}
?>
I want to pass the $name variable to another PHP Script which will be used to insert the details of the user which is currently logged in.So, the username of the Employee will come from the $name variable and the other details will be the input from the Android device.
Edit 2:
Insert.php
<?php
require "conn.php";
include "login.php";
echo $_POST['user'];
$enquiry = $_POST["enquiry"];
$retail = $_POST["retail"];
$collection = $_POST["collection"];
$booking = $_POST["booking"];
$evaluation = $_POST["evaluation"];
$test_drive = $_POST["test_drive"];
$home_visit = $_POST["home_visit"];
date_default_timezone_set('Asia/Kolkata');
$IST = date('d-m-Y H:i');
$mysql_qry1 = "INSERT INTO employee_details(enquiry,retail,
collection,booking, evaluation, test_drive, home_visit, date, time) values ('$enquiry','$retail','$collection','$booking','$evaluation','$test_drive',
'$home_visit',CURDATE(),CURTIME());";
$shift = "SELECT TIME(CURRENT_TIME),
CASE WHEN TIME(CURRENT_TIME) BETWEEN '01:00:00' AND '07:00:00' THEN 'Morning'
WHEN TIME(CURRENT_TIME) BETWEEN '08:00:00' AND '13:00:00' THEN 'Afternoon'
WHEN TIME(CURRENT_TIME) BETWEEN '13:00:00' AND '18:00:00' THEN 'Evening'
END;";
$mysql_qry2 = "INSERT INTO employee_details(shift) values ('$shift');";
$ins = mysqli_query($conn,$mysql_qry2);
if($conn->query($mysql_qry1) === TRUE)
echo "Your details has been successfully inserted.";
else
echo "Error: " .$mysql_qry1. "<br>" . $conn->error;
$conn->close();
?>
I want to pass the $name variable from "Login.php" to "Insert.php" and insert into the "USERNAME" column in employee_details.
you are defining $_POST['user'] = $name before the declare $name variable so try this
login.php
<?php
require "conn.php";
$user_mobile= $_POST["mobile_num"];
$user_pass = $_POST["password"];;
$mysql_qry = "SELECT * FROM employee_data WHERE mobile = '$user_mobile' AND password ='$user_pass'";
$result = mysqli_query($conn,$mysql_qry);
if(mysqli_num_rows($result) > 0 ) {
$row = mysqli_fetch_assoc($result);
$name= $row["name"];
$_POST['user'] = $name;
echo "Login successful.<br /> Welcome",$name ;
// Your rest of code
?>
Just try to show echo $_POST['user'] in in insert.php .
for better understandig please see this
PHP Pass variable to next page
Suggestion please use prepare statement instead of direct
mysqli_query function. Why are you using like in SQL query for
matching password instead of = for login to user.
My session checks if a user is logged , however when I refresh the page the session test runs again and starts to execute the code which then adds +1 to the members online counter everytime . I would like my code to check if the session is true like below but not if I refresh the page or navigate to another page .(Only Once per login) How can this be possible ?
PS MY code is not secure My code is not in any live environment , Security will be applied later to prevent sql injection .
include('..\db.php');
$con = mysqli_connect($dbsrvname, $dbusername, $dbpassword, $dbname);
$guest_timeout = time() -1 * 60;
$member_timeout = time() -2 * 60;
$guest_ip = $_SERVER['REMOTE_ADDR'];
$time = time()
// This session check adds +1 to my member online counter when page is reloaded
if(isset($_SESSION['CurrentUser'])){
//if user is logged
$sqlt = mysqli_query($con,"DELETE * FROM active_guests WHERE guest_ip='".$guest_ip."'");
$sqlt2 = mysqli_query($con,"REPLACE INTO active_members VALUES ('".$_SESSION['CurrentUser']."','".$time."')");
}else{
//if user not logged
$sqlt3 = mysqli_query($con,"REPLACE INTO active_guest (guest_ip,time_visited)VALUES ('".$guest_ip."','".$time."')");
}
$sqlt4 = mysqli_query($con,"DELETE * FROM active_guest WHERE time_visited < ".$guest_timeout);
$sqlt5 = mysqli_query($con,"DELETE * FROM active_members WHERE time_visited < ".$member_timeout);
$sqlt6 = mysqli_query($con,"SELECT guest_ip FROM active_guests");
$online_guests = mysqli_num_rows($sqlt6);
$sqlt7 = mysqli_query($con,"SELECT username FROM active_members");
$online_members = mysqli_num_rows($sqlt7);
?>
<div class="container">
<p>online Guests : <?php echo $online_guests ; ?></p>
<p>online Members : <?php echo $online_members ; ?></p>
</div>
You need a unique index on the username column so that REPLACE will replace instead of inserting a duplicate row.
ALTER TABLE active_members ADD UNIQUE INDEX (username);
You probably also need a unique index on the guest_ip column of active_guests, although it's possible that multiple users could be coming from the same IP.
I just started learning php and mysql and i might already be way ahead of myself. The thing i would like to create is a webpage where ppl can sign up for an event, so far so good, the form to submit their first name, last name, age and email adress is working and its actually sending te information to the database.
Next thing i want to create is a page where i can display all the database records submitted (except for the email adress). This is also working, but I wanted to play around with dynamic urls.
When i visit my page http://www.example.com/ppl.php?id=1 i get the information of the first database record displayed but i also wanted to see if i could get this to work with names instead of ids so i tried to edit my code and use http://www.example.com/ppl.php?name=john this does only return an error and however there are a few people called john in the database no records are displayed.
So i would like to know if what i want is actually possible and how do i get this to work with my current code.
<head>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "event";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$id = $_GET['id'];
$firstname = 'firstname';
$lastname = 'lastname';
$age = 'age';
$sql = "SELECT * FROM people WHERE id = $id";
$result = $conn->query($sql);
echo "<table id='display' width='600' align='center'>";
echo"<tr><td> Firstname</td> <td> Lastname</td> <td> Age</td>";
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo"<tr><td width='33%'> $row[$firstname]</td> <td width='33%'> $row[$lastname]</td> <td width='33%'> $row[$age] cm</td></tr>";
}
} else {
echo "0 results";
}
$conn->close();
?>
Try to change the query:
$sql = "SELECT * FROM people WHERE id = $id";
To:
$name = $_GET['name'];
$sql = "SELECT * FROM people WHERE name LIKE '%$name%'";
Then echo for each one John you find.
Also consider using CSS like this.
<style>
#display {
width: 600px;
}
#display td {
width: 33%;
}
</style>
You should be looking for two separate $_GET keys: id OR name.
<?php
if (isset($_GET['id'])) {
// logic to get row by ID
} elseif (isset($_GET['name'])) {
// logic to get row by Name
} else {
// logic if no $_GET keys are set
}
I would recommend not using the name field for a find because it's not a primary key in your database - it may not be unique. Your query may return multiple results depending on what data is being stored.
Edit: To answer the question of where to place this in the code sample above, consider placing it where the query string is declared.
<?php
if (isset($_GET['id'])) {
$id = $_GET['id'];
$sql = "SELECT * FROM people WHERE id = $id";
} elseif (isset($_GET['name'])) {
$name = $_GET['name'];
$sql = "SELECT * FROM people WHERE name = '$name'";
}
From there you can keep the same query execution logic. But as I stated, I'd advise against using the name field as a key because it may not be unique.
I have added a form into an admin panel with a yes (1) and no (0) option.
I have changed the Db so that on submit, the value gets added to the Db with the value "seller" 1 or 0, which works fine.
I have a form on my site. I only want it to display to a logged in person if they have a value in the Db of "1" for seller. If they have a value of 0 I want to print a message.
I am guessing I need to query the database, then construct a function and apply it in the page where the form code is to display it if the condition of "1" is met?
I have tried searching but cannot grasp how I can construct it.
<?php
// insert your DB Data
$user = "";
$pass = "";
$host = "";
$dbdb = "";
$connect = mysqli_connect($host, $user, $pass, $dbdb);
if(!$connect)
{
trigger_error('Error connection to database: '.mysqli_connect_error());
}
mysqli_set_charset($connect, 'utf8');
$query = mysqli_query($connect, "SELECT `seller` FROM `nameTable` WHERE `user` = 'userName'");
$record = mysqli_fetch_array($query);
if($record['seller'] == 1){
echo "Seller is 1";
}else{
echo "Seller is 0";
}
?>
I am using if else condition for my solution here:
<?php
$userLoggedIn = $_SESSION['userName'];//session created while login is stored in variable
//Search table `tbl` for field `SELLER` where field `user` matches with loggedin user
$searchSeller = mysql_query("SELECT `SELLER` from `tbl` WHERE `user`='$userLoggedIn'");
$searchSellerArray = mysql_fetch_array($searchSeller);
//Setting variable to be used in css depending on value in table
if($searchSellerArray['seller']==1){
$displayForm="block";//displays form
$displayMessage="none";//hides message
}
elseif ($searchSellerArray['seller']==0){
$displayForm="none";//hides form
$displayMessage="block";//displays message
}
?>
<form style="display:<?php echo $displayForm;?>;">
//Form content goes here...
</form>
<p style="display:<?php echo $displayMessage;?>;">
//Message content goes here
</p>