Jump to content

php mysql (html?) echo 'username' problem


Go to solution Solved by ProdSawax,

Recommended Posts

Hello,

I have download a 'sign up email login form' on the web, the process works (email confirmation), but when after signed i want to echo/print the username I got errors. Here my code (at bellow 'echo 'username'':

 

<?php
session_start();
include_once 'inc/php/config.php';
include_once 'inc/php/functions.php';

error_reporting(E_ALL);
ini_set( 'display_errors','1');

include 'inc/elements/signed.php';
?>

<?php

//setup some variables
$action = array();
$action['result'] = null;

//check if the $_GET variables are present
    
//quick/simple validation
if(empty($_GET['email']) || empty($_GET['key'])){
    $action['result'] = 'error';
    $action['text'] = 'We are missing variables. Please double check your email.';            
}
        
if($action['result'] != 'error'){

    //cleanup the variables
    $email = mysql_real_escape_string($_GET['email']);
    $key = mysql_real_escape_string($_GET['key']);
    
    //check if the key is in the database
    $check_key = mysql_query("SELECT * FROM `confirm` WHERE `email` = '$email' AND `key` = '$key' LIMIT 1") or die(mysql_error());
    
    if(mysql_num_rows($check_key) != 0){
                
        //get the confirm info
        $confirm_info = mysql_fetch_assoc($check_key);
        
        //confirm the email and update the users database
        $update_users = mysql_query("UPDATE `users` SET `active` = 1 WHERE `id` = '$confirm_info[userid]' LIMIT 1") or die(mysql_error());
        //delete the confirm row
        $delete = mysql_query("DELETE FROM `confirm` WHERE `id` = '$confirm_info[id]' LIMIT 1") or die(mysql_error());
        
        if($update_users){
                        
            $action['result'] = 'success';
            $action['text'] = 'User has been confirmed. Thank-You!';
        
        }else{

            $action['result'] = 'error';
            $action['text'] = 'The user could not be updated Reason: '.mysql_error();;
        
        }
    
    }else{
    
        $action['result'] = 'error';
        $action['text'] = 'The key and email is not in our database.';
    
    }

}


?>

<?=
show_errors($action); ?>
<?php
include 'hotel/reception.php';
    echo "Welcome 'username'"; ?>

<?php
include 'inc/elements/footer2in.php'; ?>confirm11.phpconfirm11.phpconfirm11.phpconfirm11.php

Link to comment
https://forums.phpfreaks.com/topic/280210-php-mysql-html-echo-username-problem/
Share on other sites

  • 2 weeks later...

To accomplish this i set the username as a session variable (mostly because i had it display on all pages at the top right so the user could verify it was them as well as change any settings for their account). I am not sure if this is what you would want to do, but seeing as you already have a session going it seems like the easiest approach.

 

I had a box when the user signs in for their username and literally just posted it to session username.

$_SESSION['username']=$_POST['username'];

 

 

As far as your line,  echo "Welcome 'username'" is concerned, You arent actually telling it to echo the username you would have for that person, but rather just username itself.

Edited by a246530

A couple of things RE the last post, first, the form method being used here is GET, not POST and second, the username is not part of the form data at all, so your idea won't work at all, even after changing to GET. 

 

Now to the OP.  What you will actualy need to do is echo the information brought back from the database by changing the line pointed out by the last post to the following, assuming the field in the database that is hoding the username info is called username (if not change accordingly) is

echo "Welcome '{$confirm_info['username']}'";

That said, check your database to see that the update and delete queries are doing exactly what you expect them to, I would be supprised if they are, I also seriously urge you to never use select * for production scripts.  And another thing - you need to stop breaking in and out of PHP all over the place when it is totaly unnescacery to do so.

Thank you both. I'm newby too.

Muddy_Funster had right.

In the dB, I'm using these 2 tables: users(id, username, password, email, active) and confirm(id, userid,key,email).

When I echo:

echo "Welcome '{$confirm_info['userid']}'";

I got well

"Welcome 133" and 133 corresponding well to new user.

 

So now, I need to go one step further and to query 'replace {$confirm_info['userid']} by {$users_info['username']} ?

In order to get:

"welcome UserVerynice"

 

.

 

Had tried (doesn't work) this:

 

$users_info = mysql_query("SELECT users.id, users.username, confirm.userid WHERE `$users_info[id]` = '$confirm_info[userid]' LIMIT 1") or die(mysql_error());

You completly missed out the FROM clause here, and you only need to prefix the field name with the tablename if the same field exists in multiple tables that you are selecting from.  What you want should be someting like this :

$users_info = mysql_query("SELECT users.id, users.username FROM users INNER JOIN confirm ON users.id = confirm.userid WHERE `email` = '$email' AND `key` = '$key' LIMIT 1") or die(mysql_error());

My last try (didn't echo any error, but didn't echo the 'username' too) :

 

$users_info = mysql_query("SELECT users.id, users.username FROM users INNER JOIN confirm ON users.id = confirm.userid WHERE users.id = confirm.userid LIMIT 1") or die(mysql_error());

I'm stock at error: Column 'email' in where clause is ambiguous

 

$users_info = mysql_query("SELECT users.id, users.username FROM users INNER JOIN confirm ON users.id = confirm.userid WHERE `email` = '$email' AND `key` = '$key' LIMIT 1") or die(mysql_error());
 

I'm stock at error: Column 'email' in where clause is ambiguous

 

$users_info = mysql_query("SELECT users.id, users.username FROM users INNER JOIN confirm ON users.id = confirm.userid WHERE `email` = '$email' AND `key` = '$key' LIMIT 1") or die(mysql_error());

Apparently the "email" column exists in both tables. You will need to qualify it in the WHERE clause with the name of the table you want to compare it to:

... WHERE users.email = '$email'
-- OR
... WHERE confirm.email = '$eamil'
 

FYI /When I made:

 

print_r($users_info);

 

I got the error: Resource id #7

$users_info is the reqult of a mysql_query call. It is a resource for accessing the data. You need to use mysql_fetch_assoc or one of its siblings to actually get the data.
  • Solution

Thank you so much Guru,
I replaced by:
$users_info = mysql_query("SELECT users.id, users.username FROM users INNER JOIN confirm ON users.id = confirm.userid WHERE confirm.email = '$email' AND `key` = '$key' LIMIT 1") or die(mysql_error());

with later in code:
<?php
while($resultat = mysql_fetch_assoc($users_info))
{ print_r($resulat);
echo $resultat['username'];
}?>

and that's work !

Have a nice day Guru!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.