Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 3 of 3
  1. #1
    Junior Member
    Join Date
    Aug 2005
    Posts
    29
    Member #
    10866
    I have the following code:


    PHP Code:
    <?


    if (!mysql_connect('myhost','mydatabase','mypassword'  ))

    echo 
    mysql_error();

    else echo 
    "connection established.";

    mysql_select_db('mydatabase');

    $get=mysql_query(SELECT FROM users WHERE firstname=$_POST['firstname']);

    while(
    $row=mysql_fetch_array($get))

    echo 
    "<p><br><font face='verdana'>"$row ['firstname'] , " " $row ['lastname'];

    die (
    mysql_error());

    ?>

    My problem is in this string:
    PHP Code:
     $get=mysql_query(SELECT FROM users WHERE firstname=$_POST['firstname']); 
    when I try and select $_POST['firstname'] , it wont let me do it. Since the person I want to select is the person who just registered, how could I do this without using $_POST['firstname'] or how can I use it correctly?

  2.  

  3. #2
    WDF Staff smoseley's Avatar
    Join Date
    Mar 2003
    Location
    Boston, MA
    Posts
    9,729
    Member #
    819
    Liked
    205 times
    I'm going to make a few suggestions. You have a lot of errors in your PHP code...

    First, change your php code to something more like this:
    PHP Code:
    <?
        
    // Try to get the user's id from the session
        
    session_start();
        if (!isset(
    $_SESSION["id"]) die("No ID specified.");
        
    $id $_SESSION["id"];

        
    // Try to establish a connection
        
    $connectionLinkmysql_connect('myhost','mydatabase','mypassword');
        if (!
    $connectionLink) die("Could not connect: " mysql_error());
        
    mysql_select_db('mydatabase');

        
    // Execute the query
        
    $query "SELECT * FROM users WHERE id = '$id' ";
        
    $resultLink=mysql_query($query$connectionLink);

        
    // Print the user's name
        
    if ($fields mysql_fetch_array($resultLink)) {
            echo 
    "<p>"$fields['firstname'] . " " $fields['lastname'] . "</p>";
        }
    ?>
    Then, the page where you create the user should look something like this:
    PHP Code:
    <?

        
    // Try to establish a connection
        
    $connectionLinkmysql_connect('myhost','mydatabase','mypassword');
        if (!
    $connectionLink) die("Could not connect: " mysql_error());
        
    mysql_select_db('mydatabase');

        
    mysql_query("INSERT INTO mytable (firstname, lastname) values ('$firstname', '$lastname')");
        
    $id mysql_insert_id();
        if (
    $id 0) {
            
    session_start();
            
    $_SESSION("id") = $id;
            
    header("Location: display_user.php");
        } else {
            echo 
    "There was a problem inserting the user.";
        }
    ?>

  4. #3
    WDF Staff smoseley's Avatar
    Join Date
    Mar 2003
    Location
    Boston, MA
    Posts
    9,729
    Member #
    819
    Liked
    205 times
    I should make a note that this will only work with an AUTO_INCREMENT id field... which you should be using anyway...


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 03:02 AM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com