Get user information from mysql database - php

I'm trying to do a very easy user management.
I want to select and display the information from the mysql database.
I have connected to the database and it works fine. And I have a log in form and registration which works fine. Now when the user is logged in I want to display the information and the user should be able to update it.
Can i just use a simple thing like this:
<?php
$firstname= $_SESSION['user_name'];
$email= $_SESSION['user_email'];
if ($email!= null) {
echo $email;
}
else {
echo 'no email found';
}
?>

You are leaving a lot out, so it is hard to know what all you want to do, but you can use the UPDATE statement to change the database values which have already been inserted.
UPDATE users SET user_email = 'some_email'
WHERE user_name = 'some_user';
Other than that I'm not sure what you're asking for. The whole HTML form?

Related

Make each user vote only once with Session username

I made a simple vote poll it works fine,but i can't make it for each user i mean i can somehow hide if i even manage votepoll to be saved in users info but it's impossible when i tried insert into users it makes a new user with only votePoll declared...everything else is empty,i think it's a wrong way to approach,i can't even mind how to show votepoll whitch user is voteing.
This is my code of vote
<?php
if(isset($_POST['submitme'])){
$submitme=$_POST['submitme'];
$yesOrNo=$_POST['yesOrNo'];
if(!empty($yesOrNo)){
$sqlv=$con->query("INSERT INTO votepoll (yesorno) VALUES ('{$yesOrNo}')");
header('location:index.php');
}
}
?>
Add your "yesorno" row in users,than make query update,don't insert!
if(isset($_POST['submitme'])){
$submitme=$_POST['submitme'];
$yesOrNo=$_POST['yesOrNo'];
if(!empty($yesOrNo)){
$sqlv=mysqli_query($con,"UPDATE users SET yesorno='$yesOrNo' WHERE username='$username'");
}
}
okay so when you click button yesorno is updated in rows as "yes" or "no" ,you should know how user statuses work, if($userStatus==1) and 1 is equal to Admin do so and etc. i'm just giving you direction now just get yesrno from rows and set it equal to variable and if("$userVote!="yes" && $userVote!="no"):
your html cde below and endif;
I just gave you some idea how to get through this problem and gave a direction.

SELECT column mysql

I am in a little bit in doubt, if I am on the correct path here. I have a mysql database, where I have login details of users. I am making a profile page, where I would like the informations on a user is shown to the user. I am now trying to return the firstname there is a column in the database. Am I on the correct path with this code?
<td>
Firstname
<?php
$stmt = $mysqli->prepare("SELECT firstname FROM login");
$stmt->execute();
$fname = null;
$stmt->bind_result( $fname);
while($stmt->fetch()) {
$firstname = // Code here
echo $firstname;
}
$stmt->close();
$mysqli->close();
?>
</td>
Update:
I tried to make the code a little bit smaller. This code actually retrives users, but it is all the users in the database, and not only the user I am logged into with. Should the SELECT query be asigned with the primarykey, if I only need the firstname on the current user I am logged in as?
<td>
Firstname
<?php
$sql ="SELECT firstname FROM login;";
$res = $mysqli->query($sql);
//print($res);
if($res){
while($row = $res->fetch_assoc()){
echo $row['firstname'];
}
}
?>
</td>
You are on the correct path but you need to assign your array to a variable so you can print adding your column name in the scope and remove the bind of $fname
//$stmt->bind_result($fname);
while($column = $stmt->fetch()) {
$firstname = $column['firstname'];
echo $firstname;
}
Or you can just use the variable you bind before
$stmt->bind_result($fname);
while($stmt->fetch()) {
echo $fname;
}
You could optimize your query and limit tresult to only one user by adding a WHERE condition to your query, you can use user ID for example
SELECT firstname FROM login WHERE userid = 1
If you're playing around with PHP and trying to learn how things work, you're on a great path. If you're planning on deploying this code to the internet, you have a few issues:
Your markup seems off. Why are you putting all of the first names in a single <td>?
You shouldn't have a SQL query happening inside of a markup. What if you want to show results from a cache or a text file some day? Ideally you wouldn't even mix PHP and HTML. Some folks use PHP's built-in templating abilities, but it's generally preferred to use a template language like jade or twig.
Your code alignment isn't consistent.
But if you're just seeing what PHP can do, good job. Keep trying stuff out. It's the best way to learn for most people. Others like reading a book, then trying stuff.

Trouble returning custom field values from DB

I am integrated a php login system and created some custom fields in the user registration form which populate the database just fine. But I can't seem to return the custom field values from the database--only the fields that were built in like username. I created a field called displayname and want to show that on a page. I know the answer could be dependent on the code, but in general, shouldn't I be able to return any value regardless as long as the session is active and the connection to the database works and retuns the built in field data?
here is the php code on each page (that works for native username)
<?php
session_start();
if ( !isset($_SESSION['login']) || $_SESSION['login'] !== true) {
if(empty($_SESSION['access_token']) || empty($_SESSION['access_token']['oauth_token']) ||
empty($_SESSION['access_token']['oauth_token_secret'])){
if ( !isset($_SESSION['token'])) {
if ( !isset($_SESSION['fb_access_token'])) { ...etc.
Then I am trying to call my custom field "bio" with:
<?php echo "hi ".$_SESSION['username']; ?>
<?php echo "Your Bio ".$_SESSION['bio']; ?>
Why will username return the value but not my custom bio field? Again, I know it might depend on the script I am using, but in theory, shouldn't this work? Or is their some general thoughts anyone might have on why it doesn't return custom field values?
Session doesn't contains every field value. Suppose you are retrieving the bio from database and storing it in $bio. You must also store bio in session using $_SESSION['bio'] = $bio; Only then you will be able to use it everywhere until your session is destroyed.
Edit
Session Tutorial
http://www.tizag.com/phpT/phpsessions.php

Individual Username from a Database

I have a database running and I'm currently printing out in a website, in a "php block" the usernames of the database. I achieved it with this
if ($db_handle) {
print "Database ISSSSS Found ";
$SQL = "SELECT * FROM `database.com`.`users`";
$result = mysql_query($SQL);
//print $result;
while ( $db_field = mysql_fetch_assoc($result) ) {
print $db_field['username'] . "<BR>";
}
mysql_close($db_handle);
}
However this gives me a giant string of all the users (I currently have 4). How do I make it so its just the individual user accessing their profile through the website
Typically, when someone logs in, you would store non sensitive information about the user in the session. This way, you can get to it quickly without needing to make database calls on every page. For instance, if you wanted to show their username in the pages header, you would always have their username handy to do so. Then, when they go to view their profile, you can use that username you stored as part of your SQL WHERE clause to pull in information pertaining only to that specific user.
Use WHERE username = 'yourusername' in your SQL query.
That shall fix your problem

Display data if usertype is admin otherwise display error message

I currently have a list of users in my mysql database. One of the columns is "type". I am trying to display certain data if type is equal to admin. If type is equal to anything else, it should just echo an error message.
Unfortunately, I have tried multiple methods but it just does not seem to be working out for me. Can anyone help me get this to work properly?
This is what I have, but obviously I am doing something wrong....
<?php
$usertype = $_SESSION['type'];
if ($usertype == "admin" ){
?>
admin stuff only goes here
<?
}
else
{
echo "not priveleged usertype";
}
?>
EDIT:
The following code works when displaying via username, however, I need content displayed by usertype, not the username.
<?php
if($_SESSION['user']['username'] == "oneoftheadminusernames" )
{
?>
Each page has to start with
<?php
#session_start();
?>
otherwise, php does not "see" the sessions contents. So that's probably it.
The # prevents the php error: A session has already been started... by the way.
Now, every page that uses the session must have this directive at the top.
At least, in a quick example, that reproduces your error perfectly.
If you are saving each logged in users type field in $_SESSION['type'] variable than the code you are writing is correct. Or if you are storing type in another variable than you that variable to check.
i have an idea like add a field EnableFlag in the table. if enablee flag is set to 1 consider it as a admin else as a User;

Categories