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.
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.
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.
I am writing a php script to edit the values in my database and I have the following line:
mysqli_query($con, "UPDATE Child
SET current_points = ('$points' - '$point_value')
WHERE Reward.child_username = '$username'
AND Reward.reward_name = '$reward'");
This doesn't throw any errors but is not updating the database. username, points, point_value and reward are all read in variables. Could someone please tell me where I am going wrong?
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.
Okay, I can post the PHP code if needed but I'm trying to use some data in my database, but have no idea how I can access it. It might be an easy reply but I'm new to this game...
Basically, I have been able to set up a login system for my site, where if you log in, it will display the logged in user's username, this is done through the use of $_SESSION and a session class.
Where if the user is logged in ($session->logged_in) etc
<h3>Welcome <? echo $session->username?> </h3>
</head>
<body>
<p> Welcome to the website, your details are below </p>
</body>
<?php } ?>
I return the user's username as such.
However, I can only currently access data from the table 'users', and 'users' is connected through foreign keys to a table called 'passengers' through the 'username' field.
What I would like to do, is instead of printing the username, print the user's surname.
So essentially it is like:-
Logging in sets up a session and recognises the username that is logged in.
By querying this username in a different table, can pull up data from all corresponding tables in the database.
But I have no idea how to go about it..
If you guys and girls know of any sample code, or could point me in the right direction that'd be fantastic.
<? php
mysql_connect("localhost", "username", "password");
mysql_select_db("databasename");
$query = "SELECT surname FROM passengers WHERE username = " . $session->username . "";
$result = mysql_query($query);
$data = mysql_fetch_assoc($result);
echo "Hello, your lastname is $data";
?>
I think this is what you are referring to?
Also, even though you're new at php etc., try looking into mysql injection and how to prevent it (this current code is very susceptible to mysql injection -- if it were to use user input; thanks to Carrie Kendall lol =])
Good place to start is by using mysql_real_escape_string.
edit-
Not sure but this might also help out as I'm not entirely sure what you mean; mysql (left) join