last day (14 days later) » 

12:49 PM
0
Q: PHP echo username after login

user7327480I am trying to echo the username of user when they are signed in but the code I have isn't doing anything . I am also not getting any errors . I've changed a few things around but nothing changed . Can someone please help me ? <?php include("connect.php"); if(isset($_POST['usernam'])) { $use...

 
You don't check for mysql errors so you don't know if your query is failing
 
Try $check = mysqli_query("SELECT usernam FROM users WHERE usernam="'.$usernam.'");
 
user7327480
@bos570 that didn't change anything
 
user7327480
@JohnConde how do i check ?
 
user7327480
12:49 PM
where do i put that ? the top of my code ?
 
user7327480
I have an error now
 
user7327480
Parse error: syntax error, unexpected ''.$usernam.''
 
user7327480
line 12 where the error is : ` $check = mysqli_query("SELECT usernam FROM users WHERE usernam="'.$usernam.'" "); `
 
You have an extra " at the end
 
Please use the original query that you have in your question and then check if you have any errors or not.
 
user7327480
12:49 PM
No I get nothing at all
 
Try to debug your code manually. Add some echos in different parts of your code and see which of them shows and which of them do not show. Also, When you say you get nothing at all, what do you mean? Because even if your query does not return something, then you have to see the username that the users has type and posted as $_POST['usernam']
 
user7327480
I don't know where to start @EhsanT . I don't know where to put my echos
 
It's easy, first add echo '1-'.$usernam; die(); after this line: $usernam = mysqli_real_escape_string($_POST['usernam']); then see if you see anything?
 
user7327480
nope nothing still
 
ideone.com/TKf1lP Run this code and see what are the messages. Also try echo in different places to find the issue
 
12:49 PM
Are you posting data to this page?! Please add var_dump($_POST) to the beginning of the page and see what it will show?
 
user7327480
@Devs I get no username when I use that code
 
user7327480
@EhsanT This is what I get C:\wamp\www\TestSocialNetwork\profile.php:3: array (size=0) empty Please try again
 
Please elaborate how are you posting to this file? Are you even posting or just typing the name of the file in the browser. Please edit your post and add the <form> section of the file which is posting to this file to your question
 
user7327480
This is what I put
 
user7327480
<?php var_dump($_POST); include("connect.php"); if (isset($_POST['usernam'])) { $usernam = mysqli_real_escape_string($_POST['usernam']); if(ctype_alnum($usernam)) { $check = mysqli_query("SELECT usernam FROM users WHERE usernam='$usernam'"); if(mysqli_num_rows($check)==1) { $get = mysqli_fetch_assoc($check); $usernam = $get['usernam']; } else { echo"user does not exists"; } } else { echo"username is not alphanumeric"; } } else { echo "Please try again"; } ?>
 
user7327480
12:49 PM
$usernam = isset($usernam) ? $usernam : '-No Username-';
 
user7327480
<center><h5><?php echo $usernam;?></h5></center>
 
Not this file, the other file that you have a <form> in it and is posting to this file. If the other file an this section of code are both in a single file, then please add the <form> section of your code to your post
 
user7327480
The login file ? I honestly have no clue what your talking about
 
Your code should have 2 parts. 1- a <form> that has some input elements in it like username and a submit button. 2- this section of your code that you have shared in your question that receives the data from the form. So you have to post data to this section from your <form>. Now my question is that are you even posting the data to this code?! Do you know what POST method is?! and then I need to see the <form> section of your code and any related code to it to see why you $_POST is empty
 
user7327480
` <form action="process2.php" method="post"> <p id="p2"><input type="usernam" size="25" name="username" placeholder="Username" ><p /> <p><input type="password" size="25" name="pw" placeholder="Password" ><p /> <p id="p3"><a href="forgotpass.php">Forgot Password ?</a></p> <button id="bt2" type="submit" name="signin">Login</button> </form>`
 
user7327480
12:49 PM
Is that what your talking about ?
 
user7327480
I had to break up the code because it was to long
 
Please edit your question and add these codes to your question. Reading the code in comments gives me headache! There are 3 links at the bottom left of your question, one of them is edit. Use that link to edit your question and add these code you just pasted in the comment to your question...
But, from what I can see, the name of the input element in your form is username, So in your php you have to use $_POST['username'] instead of all $_POST['usernam']
 
user7327480
But when I use username and try to show it , it shows the username in the connect.php file .
 
user7327480
Which is root
 
user7327480
I updated my code
 
12:49 PM
So, don't you get this error right now fill in username to sign in .?
 
user7327480
I get this fill in username to sign in . fill in password to sign in . Incorrect username or Password. Try again
 
user7327480
When I go to process2.php
 
Do you click on Login button to go to process2.php or you just type the address of process2.php file in your address bar? because you should not get error for your password!
 
Question: Is '$_POST['usernam']' even set? The 'name' of the input field is 'username' and not 'usernam'.
 
user7327480
@EhsanT I just typed the address of process2.php
 
user7327480
12:49 PM
@JokerDan I know that , but when I put username it shows my username in my connect.php file
 
You should not type the address, How do you suppose to get logged in when you type the address and not submit the form to post the username and password to your file?!?!
 

last day (14 days later) »