MySQL and PHP Problem

Discussion in 'PHP' started by qualityfirst, Dec 13, 2008.

  1. #1
    Hey!

    I've been trying to use this code for the past 3 hours, and despite following everything from all the examples and tutorials online, it won't work.

    This is the file:
    <?php session_start(); ?>
    <html>
    <body>
    <?php
    /* Make the user fill out the form if they haven't already done so */
    if (!isset($_POST["firstName"]) or !isset($_POST["lastName"]) or !isset($_POST["username"]) or !isset($_POST["password"]) or !isset($_POST["email"])){
    echo '<form action="register.php" method="post">
    First Name: <input type="text" size="15" name="firstName"></input> Last Name: <input type="text" size="15" name="lastName"></input><br />
    Username: <input type="text" size="30" name="username"></input><br />
    Password: <input type="password" size="20" name="password"></input><br />
    Email: <input type="text" size="50" name="email"></input><br />
    <input type="submit" value="SIGN UP!"></input>
    </form>';
    } else {
    /* If the user is here, it means they have filled out the form. Now we can begin to insert them into the database */
    require("includes/mysql_connect.php");
    $firstName = $_POST["firstName"];
    $lastName = $_POST["lastName"];
    $username_user = $_POST["username"];
    $password_user = $_POST["password"];
    $emailadd = $_POST["email"];
    /* Check if email address is valid */
    if (!filter_var($emailadd, FILTER_VALIDATE_EMAIL)){
    echo "Please enter a valid email address!";
    } else {
    $enterdata = "INSERT INTO members (firstName,lastName,username,password,email,admin) VALUES ('$firstName','$lastName','$username_user','$password_user','$emailadd',0)";
    if (!mysql_query($enterdata,$con)){
    die("Error: " . mysql_error());
    } else {
    echo "You have successfully signed up!";
    /* Grab User Id and Store it in a Session */
    $grabid = "SELECT * FROM 'members' WHERE username='$username_user'";
    $grabid_query = mysql_query($grabid,$con)
                    or die("ERROR 1201:" . mysql_error());
    $id = mysql_fetch_assoc($grabid_query);
    /* Store IP Address */
    $enteripdata = "INSERT INTO ipaddress (ipaddress,userid) VALUES ('$_SERVER[REMOTE_ADDR]','$id[userid]')";
    mysql_query($enteripdata,$con);
    }}}
    ?></body></html> 
    PHP:
    The problem that currently seems to be occurring is that $id[userid] is not being set. I don't see how I can fix that as I just used the variables from above?

    Thanks!
     
    qualityfirst, Dec 13, 2008 IP
  2. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #2
    Try this:

    /* Grab User Id and Store it in a Session */
    $grabid = "SELECT * FROM `members` WHERE username='$username_user'";
    $grabid_query = mysql_query($grabid,$con)
                    or die("ERROR 1201:" . mysql_error());
    $id = mysql_fetch_assoc($grabid_query);
    /* Store IP Address */
    $enteripdata = "INSERT INTO ipaddress (ipaddress,userid) VALUES ('$_SERVER[REMOTE_ADDR]','$id['userid']')";
    mysql_query($enteripdata,$con);
    }}}
    ?></body></html>
    PHP:
    Untested.
    But i think there is something wrong with the single quotes between the members
    try it and see if any error occurs?
     
    ads2help, Dec 13, 2008 IP
  3. qualityfirst

    qualityfirst Peon

    Messages:
    147
    Likes Received:
    6
    Best Answers:
    1
    Trophy Points:
    0
    #3
    Thanks!

    Here is the error I get when I put that in:

    Removing the single quotes solves the error.

    EDIT:

    Fixed the error. Seems this is what was causing it:

    Changing the userid column to simply id solved the problem.

    EDIT2:

    NEW PROBLEM

    K, so now that the query was working, I decided to put it in a function.

    Now I get this error:

    Here is the functions file:

    <?php
    require("includes/mysql_connect.php");
    /* Grab User ID */
    function grabuserid($usernamae_user1, $password_user1){
    $grabid = "SELECT * FROM members WHERE username='$username_user1' AND password='$password_user1'";
    $grabid_query = mysql_query($grabid,$con)
                    or die("ERROR 1001: Please Contact Administrator");
    $id = mysql_fetch_assoc($grabid_query);
    return $grabuserid = $id['userid'];
    }
    /* Record the User's IP Address */
    function recordip($functionip, $functionuserid){
    $enteripdata = "INSERT INTO ipaddress (ipaddress,id) VALUES ('$functionip','$functionuserid')";
    mysql_query($enteripdata,$con);
    }
    ?>
    PHP:
    Here is the data I am transferring to the functions through register.php:

    
    <?php session_start(); ?>
    <html>
    <body>
    <?php
    require("includes/functions.php");
    /* Make the user fill out the form if they haven't already done so */
    if (!isset($_POST["firstName"]) or !isset($_POST["lastName"]) or !isset($_POST["username"]) or !isset($_POST["password"]) or !isset($_POST["email"])){
    echo '<form action="register.php" method="post">
    First Name: <input type="text" size="15" name="firstName"></input> Last Name: <input type="text" size="15" name="lastName"></input><br />
    Username: <input type="text" size="30" name="username"></input><br />
    Password: <input type="password" size="20" name="password"></input><br />
    Email: <input type="text" size="50" name="email"></input><br />
    <input type="submit" value="SIGN UP!"></input>
    </form>';
    } else {
    /* If the user is here, it means they have filled out the form. Now we can begin to insert them into the database */
    require("includes/mysql_connect.php");
    $firstName = $_POST["firstName"];
    $lastName = $_POST["lastName"];
    $username_user = $_POST["username"];
    $password_user = $_POST["password"];
    $emailadd = $_POST["email"];
    /* Check if email address is valid */
    if (!filter_var($emailadd, FILTER_VALIDATE_EMAIL)){
    echo "Please enter a valid email address!";
    } else {
    $enterdata = "INSERT INTO members (firstName,lastName,username,password,email,admin) VALUES ('$firstName','$lastName','$username_user','$password_user','$emailadd',0)";
    if (!mysql_query($enterdata,$con)){
    die("Error: " . mysql_error());
    } else {
    echo "You have successfully signed up!";
    grabuserid($username_user,$password_user);
    $_SESSION['userid'] = $grabuserid;
    recordip($_SERVER['REMOTE_ADDR'],$_SESSION['userid']);
    }}}
    ?></body></html> 
    PHP:
     
    qualityfirst, Dec 13, 2008 IP