Welcome to Dream.In.Code
Become a PHP Expert!

Join 150,184 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 2,131 people online right now. Registration is fast and FREE... Join Now!




Updating only their username

 
Reply to this topicStart new topic

Updating only their username

Sayid Ahmed
24 Aug, 2008 - 02:24 PM
Post #1

New D.I.C Head
*

Joined: 20 Aug, 2008
Posts: 29


My Contributions
Hello,

I thought I would experiment some mysql I just learnt by making a little php page called 'getoil.php'. I have a database with an id, username, password and an oil column. All this code does is it updates a user's amount of oil (as if they're purchasing it - i took this idea from a game), the problem is, it only updates one of the user's oil, the one i've speciifed as 'ahmed'. What i want to know is, how can i specify it to update the amount of oil for the user that submitted it. In other words, if user ahmed submits the form, only his oil amount gets updated, same for user fred, george...etc.

I attempted doing this by using require_once('myloginpage.php'); and then doing mysql_query("SELECT * FROM users WHERE username='$user'") but all that did was attempt to log me in blink.gif

Here's the code I'm using:

php

<?php
require_once('global.php'); // session start
require_once('security.php'); // restrict access to logged users
require_once('conn.php'); // db connection

$result = mysql_query("SELECT * FROM users WHERE username='ahmed'") or die(mysql_error());
$row = mysql_fetch_array($result); // gets the data from username ahmed - i want this to be for whatever username is signed in

$oldoil = $row['oil']; // the originally amount of oil they had

$oil = $_POST['oil']; // the oil they're buying
$newoil = $oil+$oldoil; // the oil they're going to have in total
$sql = "UPDATE users SET oil = $newoil WHERE username='ahmed'";
mysql_query($sql)or die(mysql_error());
echo "You now have ".$newoil." barrels after purchasing ".$oil;

?>


This post has been edited by Sayid Ahmed: 24 Aug, 2008 - 02:24 PM
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Updating Only Their Username
24 Aug, 2008 - 03:32 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,660



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
When the user logs in, you can set a session variable to the username they used. That or after you authenticate them, pull the username from their record and store it in the $_SESSION variable. Then whenever you need access to the user's name, you can use this variable in the query.


Lets say after they logged in and you verified who they were, you would set $_SESSION['user'] to their username they used during login.

Then on this page you can access it like so...

php

// Get the session variable and put it in a variable called $user
$user = $_SESSION["user"];

$result = mysql_query("SELECT * FROM users WHERE username='$user'") or die(mysql_error());
$row = mysql_fetch_array($result); // gets the data from username ahmed - i want this to be for whatever username is signed in

$oldoil = $row['oil']; // the originally amount of oil they had

$oil = $_POST['oil']; // the oil they're buying
$newoil = $oil+$oldoil; // the oil they're going to have in total
$sql = "UPDATE users SET oil = $newoil WHERE username='$user'";
mysql_query($sql)or die(mysql_error());
echo "You now have ".$newoil." barrels after purchasing ".$oil;


See how we pulled out their username from the session, stored it in $user and then used $user in the two queries?

Hope that helps you out.

"At DIC we be pulling usernames from sessions type of code ninjas...we also pull teeth, hair, and sometimes on rare occasions fingers." decap.gif
User is offlineProfile CardPM
+Quote Post

Sayid Ahmed
RE: Updating Only Their Username
25 Aug, 2008 - 06:59 AM
Post #3

New D.I.C Head
*

Joined: 20 Aug, 2008
Posts: 29


My Contributions
I completely understood that but unfortunately it didn't work. I attempted it again by adding $user = $_SESSION["user"]; to several different pages (login page, login success page) and echoing it but it was blank sad.gif
User is offlineProfile CardPM
+Quote Post

pemcconnell
RE: Updating Only Their Username
25 Aug, 2008 - 07:21 AM
Post #4

D.I.C Regular
Group Icon

Joined: 5 Aug, 2008
Posts: 403



Thanked: 38 times
Dream Kudos: 75
My Contributions
Hi Sayid,

Martyr2s example should work. I've broken it down into a few steps to help try to clear anything up.

Firstly make sure session_start() is at the top of each page - with this on, it will allow you to store and move 'session' data.

Secondly, where you have on the login page something like:
CODE

$sql = "SELECT userId FROM users WHERE username = '".$username."' AND password = '".md5($_POST['password'])."'";

in your if condition (if a user is found - i think you have a header('Location:blahblah... in there at the mo) you put this line before the header location:
CODE

if($results>1){
$_SESSION['username'] = $_POST['username_txt']; // assuming username_txt is the name of your username text field in your login form
header('Location:loggedin.php');


Then, on any page on the site, echo $_SESSION['username']; will show you the username. smile.gif

Let us know if you're still stuck.
User is offlineProfile CardPM
+Quote Post

Sayid Ahmed
RE: Updating Only Their Username
25 Aug, 2008 - 07:23 AM
Post #5

New D.I.C Head
*

Joined: 20 Aug, 2008
Posts: 29


My Contributions
woops i didnt put the $_POST['uname_txt'] part in, and i did it after the header(), it works now

Thanks to both of you

EDIT: Randomly im getting
QUOTE
Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\xampp\htdocs\Web\menu.php:6) in C:\xampp\htdocs\Web\checkLogin.php on line 4


What's this about?

This post has been edited by Sayid Ahmed: 25 Aug, 2008 - 07:50 AM
User is offlineProfile CardPM
+Quote Post

JBrace1990
RE: Updating Only Their Username
25 Aug, 2008 - 10:48 AM
Post #6

D.I.C Regular
Group Icon

Joined: 9 Mar, 2008
Posts: 479



Thanked: 24 times
Dream Kudos: 350
My Contributions
as I posted before in my giant list of common php problems, headers and sessions need to start BEFORE anything is echo'd on the page... so move session_start() up to the top of the page
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 03:57AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month