Jump to content

Archived

This topic is now archived and is closed to further replies.

esiason14

simple query help

Recommended Posts

Trying to find the user id from the users table based on the session login name. This is what I have, but its not working. Any pointers?
[code]
$sessu = $_SESSION['username'];

    $sql = "SELECT id from users WHERE username = '$sessu'";[/code]

Share this post


Link to post
Share on other sites
put in an "echo $sql;" so you can see the query you're sending to the database. It's possible that your session management isn't working yet.

Share this post


Link to post
Share on other sites
Thanks for the reply..session management is definitely working...I did a print_r(_$SESSION) and it returns my username and passwd. Also did an echo"$sql" and it returned my username.

What I'm trying to do is get the user id from the above query and use it in the following query, which stores user entered info. The following query is storiing $newp in my db, but not the user id from the first query. Maybe my problem is how I'm using the results from the first query here:

[code]foreach($myplayers AS $newp) {

    $sql = "INSERT INTO myplayers (user_id, player_id) VALUES ('$id', $newp)";
    mysql_query ($sql) or die(mysql_error());
}[/code]



Share this post


Link to post
Share on other sites
Is the user ID an integer? You have it wrapped in inverted commas, so that would cause a problem.

Share this post


Link to post
Share on other sites
No, that won't cause a problem.

esiason, show me how you are loading up that $myplayers array.

Share this post


Link to post
Share on other sites
Thanks guys...I ended up figuring it out. this works for me. Not sure if it is efficient, but it works.

[code]$sessu = mysql_escape_string($_SESSION['username']);

$result = mysql_query("SELECT id from users WHERE username = '$sessu'");
if (!$result) {
   die('Invalid query: ' . mysql_error());
}
while ($row = mysql_fetch_array($result))
{
    $id = $row['id'];
}

foreach($player_id AS $newp) {

    $sql = "INSERT INTO myplayers (user_id, player_id) VALUES ($id, $newp)";
    mysql_query ($sql) or die(mysql_error());
}[/code]

Share this post


Link to post
Share on other sites

×

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.