$sql not returning results, this can't be this tough

Discussion in 'PHP' started by cspgsl, May 5, 2006.

  1. #1
    I'm new but I don't think I'm that dumb... (leading with my chin)
    I need an update form on a page that will look to see if anything is entered in the users "welcome" field in their record and, if there is content, display it for editing / updating.

    If there is nothing in the user's field enter "My Default Text" and use that to update the user's welcome field in their record.

    Thus far I have the following.

    In the login form
    
    <input type="submit" name="kt_login1" id="kt_login1" value="Login" />
    
    Code (markup):
    In the page that opens as a result of loggin in:
    
    <? session_start();
            if (isset($_POST['kt_login1'])):
             $_SESSION['kt_login_user'] = $_POST['kt_login_user'];
             $_SESSION['kt_name'] = $_POST['kt_name'];
            endif;
    ?>
    <p>Hello <? echo  $_SESSION['kt_name'] ?> Choose a link to update a part of your records...</p>
    
    Code (markup):
    In the page that is to contain the update record form:
    
    <? session_start();
            if (!isset($_SESSION['kt_login_user'])):
            $_SESSION['kt_login_user'] = "Anonymous";
            $_SESSION['kt_name'] = $_POST['kt_name'];
            $_SESSION['kt_welcome'] = $_POST['kt_welcome'];
    endif;
    ?>
    <p>Hello <? echo  $_SESSION['kt_name'] ?> Yada yadda...</p>
    <?php $sql = "SELECT 'welcome' FROM `users` WHERE `id` = $kt_login_user";
    $qry = mysql_query($sql);
    if(@mysql_num_rows($qry) > 0)
    {
    while($r = mysql_fetch_array($qry))
    {
    $kt_welcome = $r[1];
    }
    }
    else
    {
    $kt_welcome = "My default text";
    }
    ?>
    <textarea name="welcome" cols="80" rows="20"><?php echo $_SESSION['kt_welcome']; ?></textarea>
    <?php mysql_close(); ?>
    
    Code (markup):
    I have created 2 users, one with content in the welcome field of their record and the other with a blank welcome field in the welcome field of their record.

    What is happening at the moment is
    1 - The "echo $_SESSION['kt_name" is returning the name of the logged in user (as expected)
    2 - the only thing that is being returned in the textarea is My default text. The contents of the user with pre-existing data in his welcome field is not being returned in spite of being logged in.

    I am coming to honestly believe that PHP was developed to drive me nuts.

    I would appreciate some direction here. Many thanks in advance.
     
    cspgsl, May 5, 2006 IP
  2. Big 'G'

    Big 'G' Member

    Messages:
    89
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    48
    #2
    Your query
     $sql = "SELECT 'welcome' FROM `users` WHERE `id` = $kt_login_user";
    Code (markup):
    uses the $kt_login_user var but i can not see any where you declaring it. Hence if not declared it will return nil records and display your default text
     
    Big 'G', May 5, 2006 IP
  3. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #3
    It doesn't look like you are setting the $kt_login_user variable.

    Change:

    
    $sql = "SELECT 'welcome' FROM `users` WHERE `id` = $kt_login_user";
    
    to
    
    $sql = "SELECT 'welcome' FROM 'users' WHERE 'id' = '$_SESSION[kt_login_user]'";
    
    or 
    
    $kt_login_user = $_SESSION['kt_login_user'];
    $sql = "SELECT 'welcome' FROM `users` WHERE `id` = $kt_login_user";
    
    PHP:
     
    jestep, May 5, 2006 IP
  4. Edmunds

    Edmunds Peon

    Messages:
    136
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #4
    If $kt_login_user wasn't set, the query would be
    SELECT 'welcome' FROM `users` WHERE `id` = 
    Code (markup):
    which would return a MySQL error.

    The actual mistake is here:

    $sql = "SELECT [color=red][b]'welcome'[/b][/color] FROM `users` WHERE `id` = $kt_login_user";
    Code (markup):
    and
    $kt_welcome = $r[1];
    Code (markup):
    Phew, there's 2 mistakes here!

    First of all, you are selecting something in apostrophes. MySQL treats apostrophes as literals. This means that doings SELECT 'welcome' literally returns the word 'welcome'. Were you looking for this - ` - character instead?

    Secondly, you were trying to select only 1 column! Yet, $r[1] refers to a second column! The indexes start from 0, remember? Why not just do $r['welcome'] instead?
     
    Edmunds, May 5, 2006 IP