Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

php, sessions and sql

Options
  • 03-08-2005 12:08pm
    #1
    Registered Users Posts: 180 ✭✭


    Hi,

    I am having an issue with php sessions and sql queries.

    Basically what is happening is I am using a form to update details and when I go to update I get bombed out of the session. (i.e. I have to log in again).

    I use this kind of technique through all sorts of sites and every now and again this happens and I am not sure why. I recall it being something to do with the sql query but after that I don't know.

    here is the code

    [php]
    require("php/session.php");
    require_once("includes/inc.dunboy.php");
    require_once("includes/inc.connect.php");

    $id = $_GET;
    $action = $_GET;

    if ($action === "edit") {

    $id = $_POST;
    $name = $_POST;
    $address = $_POST;
    $phone = $_POST;
    $fax = $_POST;
    $email = $_POST;
    $username = $_POST;
    $password = $_POST;

    //////////////////////////////
    // update players details
    $sql = "update players set
    name = '$name',
    address = '$address',
    phone = '$phone',
    fax = '$fax',
    email = '$email',
    username = '$username',
    password = '$password'
    where id = '$id'";
    // echo $sql;
    mysql_query($sql) or die("SQL Update Error: ".mysql_error());
    header("Location: players.php");

    } else {

    $sql = "select * from players where id = '$id'";
    $result = mysql_query($sql) or die("SQL Select Error: ".mysql_error());
    $row = mysql_fetch_row($result);



    $content = <<< END

    <form name="form" id="form" action="$PHP_SELF?action=edit" method="post">
    <input type="hidden" name="id" id="id" value="$id">
    <table class="admin_list" summary="Edit player Details" cellpadding="0" cellspacing="0">

    <tr class="color_head">
    <th colspan="2" style="text-align:center">Edit Player</th>
    </tr>

    <tr>
    <td>Name: </td>
    <td><input type="text" class="text" name="name" id="name" value="$row[1]"></td>
    </tr>

    <tr>
    <td>Address: </td>
    <td><textarea name="address" class="text" id="address">$row[2]</textarea></td>
    </tr>

    <tr>
    <td>Phone: </td>
    <td><input type="text" class="text" name="phone" id="phone" value="$row[3]"></td>
    </tr>

    <tr>
    <td>Fax: </td>
    <td><input type="text" class="text" name="fax" id="fax" value="$row[4]"></td>
    </tr>

    <tr>
    <td>Email: </td>
    <td><input type="text" class="text" name="email" id="email" value="$row[5]"></td>
    </tr>

    <tr>
    <td>Username: </td>
    <td><input type="text" class="text" name="username" id="username" value="$row[6]"></td>
    </tr>

    <tr>
    <td>Password: </td>
    <td><input type="text" class="text" name="password" id="password" value="$row[7]"></td>
    </tr>

    <tr>
    <td colspan="2" align="center"><input type="submit" class="button" name="edit" id="edit" value="Edit Player Details"></td>
    </tr>

    </table>
    </form>

    END;

    }

    require_once("includes/inc.template.php");
    [/php]

    php/session.php simply contains the following
    [php]
    session_start();

    $usernameVAR = "username";
    $passwordVAR = "password";

    if($_SESSION!=$usernameVAR || $_SESSION!=$passwordVAR)
    {
    header('location: index.php');
    }
    [/php]

    I used to think that it was a header() problem (and it still may be) but I think it has something to do with the sql query and the way that sessions interact with them.

    If anyone has any ideas I would greatly appreciate a pointer in the right direction.

    thanks!

    (i thought that i posted something on this here previously but I couldn't fin it so it must have been a different board)


Comments

  • Moderators, Politics Moderators Posts: 39,776 Mod ✭✭✭✭Seth Brundle


    print out the variables and SQL statement to double check.
    Looking at the form you write value="$id" - is it writing the actual id number or $id in the HTML?


  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    What script creates the session, ie. the login page?


  • Registered Users Posts: 180 ✭✭marcphisto


    thanks very much for the swift replies.


    print_r($_POST) is
    [id] => 5
    [name] => me
    [address] => address
    [phone] => +phone number
    [fax] => +fax number
    [email] => me@me.com
    [username] => username
    [password] => password

    Query printd out as
    update players set name = 'me', address = 'address', phone = '+phonme number', fax = '+fax number', email = me@me.com', username = 'username', password = 'password' where id = '5'

    it is writing out value="5" in the html


    when i login i goto an authentication file which contains the following

    [php]
    session_start();

    $username = $_POST;
    $password = $_POST;

    if ($username === "username" && $password === "password") {

    $_SESSION = $username;
    $_SESSION = $password;
    header("Location: ../developments.php");

    } else {

    header("Location: ../index.php");

    }
    [/php]


    any thoughts/ideas are greatly appreciated.[/email]


  • Moderators, Politics Moderators Posts: 39,776 Mod ✭✭✭✭Seth Brundle


    Are the === intentional?
    [edit: doh - sure this means identical]

    Your SQL statement appears wrong with a missing quote @ 'me@me.com'


  • Closed Accounts Posts: 19,777 ✭✭✭✭The Corinthian


    kbannon wrote:
    Are the === intentional?
    [edit: doh - sure this means identical]
    Does it? I thought == was correct.


  • Advertisement
  • Registered Users Posts: 180 ✭✭marcphisto


    that's a typo when i was editing the code for display here. It is actually there in the real code.


  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    Yeah == should be the one. Would the fact you are re-writing the $username and $password variables have anything to do it. I think it shouldn't as you no longer use them as they in the sessions.
    I'll look more into it...

    By the way im in killarney as well at the moment working in a computer store...


  • Registered Users Posts: 180 ✭✭marcphisto


    $a == $b Equal TRUE if $a is equal to $b.
    $a === $b Identical TRUE if $a is equal to $b, and they are of the same type. (PHP 4 only)


  • Registered Users Posts: 4,003 ✭✭✭rsynnott


    marcphisto wrote:
    Hi,


    [php]
    ...
    $username = $_POST;
    $password = $_POST;

    //////////////////////////////
    // update players details
    $sql = "update players set
    name = '$name',
    address = '$address',
    phone = '$phone',
    fax = '$fax',
    email = '$email',
    username = '$username',
    password = '$password'
    where id = '$id'";

    [/php]

    No! Wrong! Bad! Don't do this! Haven't you ever heard of SQL injection?!


  • Registered Users Posts: 5,618 ✭✭✭Civilian_Target


    Yes - what he said. You're not checking any of your variables - if you intend to actually use this program in the public arena you're at least going to have to use the mysql_real_escape_string function to make sure that you're not open to people reading from your database....


  • Advertisement
Advertisement