Please forgive me if I'm over complicating this.
My goal: build an online course which allows the user to return to where they last stopped on a multi-page html/php site.
I purchased the aMember script, it's a php script that protects folders and files and allows membership levels. It does not come with any pre-made course pages or such, just a server side protection. It allows registration of user accounts and gives them access to specific folders and pages.
====
What I want to do is to build a sequential html5 course, with smaller chunks of info in each for easier learning. Building a menu to jump around is not ideal for this type of course. So I would want a button that takes a logged in user back to the page where they visited last time and to include it in the DB so that they can log in from anywhere and not count on cookies.
I am not a programmer so it's hard for me to explain in shorter terms, I hope you can understand and direct me to the right resources. Thanks!
Create a script that's included in every page of your site which will send info about user id,last visited page,time etc to database and your login script will redirect user accordingly from the information in your DB. Should not be that hard, if you need any code examples then ask.
You should store maybe user id in a session in login script.
For example the script for saving should be something like this:
if(isset($_SESSION['u_id'])){
$db_handler=mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME) or die
('ERROR: Could not connect.');
$u_id = $_SESSION['u_id'];
$ref = $_SERVER['HTTP_REFERER'];
$query= "INSERT INTO user_activity(u_id,page) VALUES ($u_id, $ref);";
$res = mysqli_query($db_handler,$query);
if(!$res) {
die("ERROR: " . mysqli_error($db_handler));
}
}
And in your login script you should have something like this:
if(isset($_POST['user'])){
$db_handler=mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME) or die
('ERROR: Could not connect.');
$u_id = $_POST['user'];
$query= "SELECT t.id,u.u_id,u.page from users t JOIN users_activity u ON t.id=u.u_id WHERE u.u_id=$u_id;";
$res = mysqli_query($db_handler,$query);
if(!$res) {
die("ERROR: " . mysqli_error($db_handler));
}
}
Should give you some idea, got busy at work so can't do better right now.
Related
Im trying to display a "welcome, {first name}" to my website, but im not sure how to pull that info from my database to my page, i have looked at multiple other questions like this but i dont really follow what they want the people to do.
Here is some example code i have in a div on my html page, i want to draw their name if signed in, and nothing if they arent signed in. I was using this code to see if i could get the name to appear in the div as just to figure out how it works but im stuck.
<?php
if (isset($_SESSION['userId'])) {
echo "<p>You're logged in!</p>";
}
else{
echo "<p>You're logged out!</p>";
}
?>
thanks for any help :)
Assuming the user is already authenticated, and you know the user ID,
<?php if($_SESSION['auth']) { << condition >> } ?>
you might wanna create a connection to your database and make query to extract the name,
$conn = new mysqli($servername, $username, $password, $database);
$query = 'SELECT username FROM tablename WHERE user_id = '.$user_id;
$username = $conn->query($query);
and display it.
<? $username; ?>
else, display whatever you like.
Sorry Question is totally confusing ! You have said you are trying to pull data from dataBase but again in the code you are trying something from SESSION.
Make sure what you are doing is logically clear to you.
If you want to show user name then SESION will not help you. Also make sure user logged in and you have started session. If you are getting user data from database then you don't need session. Just check current user and then run query.
But all this is Php and MySQL related task not html.
I have created and app with a working login and registration. When someone logs in and presses a button there username is sent to a Earn.php file that is connected to my database that has a points column in it. My problem lies with adding, lets say 5 points to the users specific account.
Example: Username sent to php file from app> then the php file takes that specific username and add 5 points to its point column in the database. Like 5+5=10
What I have now:
<?php
$con = mysqli_connect("localhost", "id177667_root", "***", "id177667_loginb");
$username = $_POST["username"];
?>
Sorry if this is a lot to ask for, I very knew at this! Any help is very much appreciated, thanks!
This has a simple solution. You can use update statement:
$q = "UPDATE TableName SET points = points + 5 WHERE username_column ='".$username."'";
$r = mysqli_query($con , $q) or die();
Let me know if there is anything else.
I have been working on a program for a job interview coming up soon, and I was nearing the completion of the program which had everything running the way I needed it to... but then my computer crashed. When I opened up the files again, everything was the same, nothing changed, all my changes were saved before it crashed. Only thing is, now my MySQL table doesn't get the data sent to it from the INSERT code. Is there something I can do to make this work?
I have tried creating a new database, a different table, restarting my computers, restarting Chrome, everything.... I can't get it and I'm desperate at this point.
Please see the code below...
// Connect to the database.
$link2 = mysqli_connect("localhost", "cl60-booking", "XXXXXXX", "cl60-booking");
if (mysqli_connect_error()) {
die ("There was an error connecting to the database");
}
// Update the bookings DB with the user's hotel room.
$query = "INSERT INTO booking
(`beds`, `baths`, `booked`, `checkInDate`, `checkOutDate`)
VALUES('".mysqli_real_escape_string($link2,
$_POST['bedNumber'])."',
'".mysqli_real_escape_string($link2, $_POST['bathNumber'])."',
'Yes', '".mysqli_real_escape_string($link2,
$_POST['checkIn'])."',
'".mysqli_real_escape_string($link2,
$_POST['checkOut'])."')";
Use the following code segment :
mysqli_query($link2, $query);
In my admin section, I want the user to be able to click a button and a HTML report to display all fields from all tables with the user id that is set in that session.
So $user_id = intval($_SESSION['user_id']);
Would the button be something like:
<?php
if (isset($_POST['doReport'])) {
$link = mysql_connect(DB_HOST, DB_USER, DB_PASS) or die("Couldn't make connection.");
$query = ("SELECT user_id FROM users WHERE user_id = '$user_id');
}
// redirect to report page
header('Location: report.php?id=' . $user_id);
?>
<p align="center">
<input name="doReport" type="submit" id="doThesisReport" value="View Report">
Also, what would be the best way to lay it out in the report page as I have 7 tables.
I am looking for a quick easy fix
Thank you!
You have a mistake: " char missed at the end of $query declaration.
I don't understand your trouble. If you want to use $_SESSION['user_id'], you can use it at any page without passing it in url.
And please don't put several questions in one.
The good way to achieve this would be :
Make a button witch redirects to a new page, say "report.php".
Now create the following logic for redirect page:
Fetch the USER-ID from session using the code you've used above.
Make the database connection on this page.
You forgot to use mysql_select_db(); to select the database.
Write the query like :
$query = ("SELECT user_id FROM users WHERE user_id = '$user_id'");
// you left "
Fire the query using $result=mysql_query($query); Note that the result from database is saved into $result.
You can use tables to display data as per you liking but quick way would be to use print_r($result)
Close the connection using mysql_close();
Hopefully since the question is not clear, I've posted the basic logic.
EDITED : forgot to put query in code block.
I'm trying to increment +1 impression every time an ad is displayed on my site, however the variable increments +2 to +3 arbitrarily. I've removed everything that's working correctly and I made a page with only this code in it:
<?php
require "connect_to_mydb.php";
echo 'Hello***** '.$testVariable=$testVariable+1;
mysql_query("UPDATE `imageAds` SET `test`=`test`+1 WHERE `id`='1'");
?>
Every time the page is refreshed the, test increments arbitrarily either +2 or +3 and my page displays Hello***** 1 (Just to show its not looping). Access is restricted to this page so it's not other users refreshing the page.
Also, id and test are int(11) in the DB.
My DB required connection has nothing in it that would interfere.
Edit
Here is an updated code:
<?php
require "connect_to_mydb.php";
mysql_query("UPDATE `imageAds` SET `test`=`test`+1 WHERE `id`='1'");
$sql = mysql_query("SELECT * FROM imageAds WHERE id='1' LIMIT 1");
$check = mysql_num_rows($sql);
if($check > 0){
$row = mysql_fetch_array($sql);
echo $row['test'];
}
?>
Increments by +2 everytime
Edit
This is whats in connect_to_mydb.php
<?php
$db_host = "*************************";
$db_username = "*********";
$db_pass = "**********";
$db_name = "**************";
mysql_connect("$db_host","$db_username","$db_pass") or die ("could not connect to mysql");
mysql_select_db("$db_name") or die ("no database");
?>
Either there's a bug in MySQL's implementation of UPDATE, or you're doing something wrong in some code you haven't posted.
Hint: It's very unlikely to be a bug in MySQL. Other people would have noticed it.
From what you've shown, it looks like your page is being loaded multiple times.
This attempt to prove that the code is only being called once doesn't prove anything:
echo 'Hello***** '.$testVariable=$testVariable+1;
This will always print the same thing (Hello***** 1) even if you open this page multiple times because the value of $testVariable is not preserved across seperate requests.
This +2/+3 error is occurring only with Chrome and my Mobile Android browser and the code is solid. I looked to see if there is any issue with Chrome sending more than one http request (thx user1058351) and there is which is documented here:
http://code.google.com/p/chromium/issues/detail?id=39402
So since this way was unreliable I just completed a work around that is solid. Instead of including a PHP file that updates the amount of ad impressions on reload, I now have it so when the page loads, an AJAX request is sent to a separate PHP file which updates the ad stats and returns the appropriate data. The key I think is to send it through the JS code so only one http request can be sent to increment the data.
Thank you to all who responded especially user1058351 and Mark Byers (not a bug in MYSQL but possibly appears to be a bug in Chrome).