Jump to content

How to read user's data


KYKK

Recommended Posts

So i have a register-login-insert data to database codewhat i now need is read data from database of what i put in before, and not showing the whole columns but showing only the info. user info from the user loginwhat i have now is

<?phpsession_start();require_once 'db.php';  $title = $_POST['title'];mysql_query("UPDATE users SET title='{$title}'WHERE name='{$_SESSION['user_name']}'") or exit(mysql_error()); $query = ("SELECT id, name, email, title FROM users WHERE name='{$_SESSION['user_name']}'");?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html>  <head>    <title></title>  </head>  <body>    <?php    if (isset($_SESSION['user_id']))    {    ?>        Hello, <?php echo $_SESSION['user_name']; ?>,        <p>start row</p><?php echo $row['name']; ?> <?php echo $row['users_title']; ?><?php echo $row['title']; ?><?php echo $row['title']; ?><p>end row</p>              <form action="index.php" method="post">    <input type="hidden" name="page_mode" value="">    <div class="left_box">title FOR FORM</div>    <div class="right_box"><input type="text" name="title" size="30" maxlength="255" value="title"></div>    <div class="left_box"> </div>    <div class="right_box"><input type="submit" value="update" size="30"></div>    <br><br>    <a href="logout.php">Log out</a>    <?php    }    else    {    ?>    Click <a href="register.php">here</a> to register or click <a href="login.php">here</a> to log in.    <?php    }    ?>  </body></html>

I put select row from that user $query = ("SELECT id, name, email, title FROM users WHERE name='{$_SESSION['user_name']}'");and then echo row <?php echo $row['title']; ?>why it not showing the title?

Link to comment
Share on other sites

I really don't get the result thingy it say $result = mysql_query($query);if (!$result) { $message = 'Invalid query: ' . mysql_error() . "\n"; $message .= 'Whole query: ' . $query; die($message);}what whole query, while ($row = mysql_fetch_assoc($result)) {it what you say "assigned a value to $row" right? but when i put while ($row = mysql_fetch_assoc($result)) {it say mysql_fetch_assoc(): supplied argument is not a valid MySQL result resourcevalid mysql code ? so ?

Link to comment
Share on other sites

'{$_SESSION['user_name']}'
To many single quotes, maybe? Try assigning $_SESSION['user_name'] to a variable and using the variable in the query?(Or do curly braces fix that somehow? I wouldn't think so, but I've never actually tried.)
Link to comment
Share on other sites

Curly braces fix the quote thing.Can you post your complete code (the one with the $row assignment).

Link to comment
Share on other sites

<?php$sql = "SELECT * FROM `users` WHERE `name` =  '$session_name'"; $result = mysql_query($sql); while($row = mysql_fetch_array($result)){ echo "$row[title]<br />$row[name]<br />$row[users_title]";}?>

or

<?php$query = mysql_query("SELECT * FROM users WHERE name='$session_name'");$array = mysql_fetch_array($query);echo "$array[title]<br />$array[name]<br />$array[users_title]";?>

this should work for you if you havent managed to fix yet!

Link to comment
Share on other sites

For Synook,

 <?phpsession_start();require_once 'db.php';$title = $_POST['title'];mysql_query("UPDATE users SET title='{$title}'WHERE name='{$_SESSION['user_name']}'") or exit(mysql_error());$query = ("SELECT id, name, email, title FROM users WHERE name='{$_SESSION['user_name']}'");?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html><head><title></title></head><body><?phpif (isset($_SESSION['user_id'])){?>Hello, <?php echo $_SESSION['user_name']; ?>,<p>start row</p><?php echo $row['name']; ?><?php echo $row['users_title']; ?><?php echo $row['title']; ?><?php echo $row['title']; ?><p>end row</p><form action="index.php" method="post"><input type="hidden" name="page_mode" value=""><div class="left_box">title FOR FORM</div><div class="right_box"><input type="text" name="title" size="30" maxlength="255" value="title"></div><div class="left_box"> </div><div class="right_box"><input type="submit" value="update" size="30"></div><br><br><a href="logout.php">Log out</a><?php}else{?>Click <a href="register.php">here</a> to register or click <a href="login.php">here</a> to log in.<?php}?></body></html>

and after edit what Scott Milner said

<?phpsession_start();require_once 'db.php';  $title = $_POST['title'];mysql_query("UPDATE users SET title='{$title}'WHERE name='{$_SESSION[user_name]}'") or exit(mysql_error()); $query = ("SELECT id, name, email, title FROM users WHERE name={$_SESSION['user_name']}");?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html>  <head>    <title></title>  </head>  <body>    <?php    if (isset($_SESSION['user_id']))    {    ?>        Hello, <?php echo $_SESSION['user_name']; ?>,        <p>start row</p><?php$sql = "SELECT * FROM `users` WHERE `name` =  {$_SESSION['user_name']}";$result = mysql_query($sql);while($row = mysql_fetch_array($result)){echo $row['title'];}?><p>end row</p>              <form action="index.php" method="post">    <input type="hidden" name="page_mode" value="">    <div class="left_box">title FOR FORM</div>    <div class="right_box"><input type="text" name="title" size="30" maxlength="255" value="title"></div>    <div class="left_box"> </div>    <div class="right_box"><input type="submit" value="update" size="30"></div>    <br><br>    <a href="logout.php">Log out</a>    <?php    }    else    {    ?>    Click <a href="register.php">here</a> to register or click <a href="login.php">here</a> to log in.    <?php    }    ?>  </body></html>

________________________________and for Scott Milner$result = mysql_query($sql);while($row = mysql_fetch_array($result)){Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/ksclans/public_html/myadmin/index.php on line 35________________________________it same problem i have for my 3rd post but why mysql_fetch_array(): supplied argument is not a valid MySQL result resourcewhat that mean?

Link to comment
Share on other sites

what that mean?
It means there was something wrong with your query. Can you see it?
$sql = "SELECT * FROM `users` WHERE `name` = {$_SESSION['user_name']}";

You need quotes around the name's value

Link to comment
Share on other sites

of course that is something wrong.....I asking like wrong on quotes, ; , and it space this time..and after delete that space i still have problem with while($row = mysql_fetch_array($result)){ I am new to result thingy are there a tutorial about result other then php.net i don't really understand php.net like there exsamples but not explaining what each one do...

Link to comment
Share on other sites

Uh... did you change it to this?

$sql = "SELECT * FROM `users` WHERE `name` = '{$_SESSION['user_name']}'";

Link to comment
Share on other sites

wait is $sql work for mysql too ? and i have$sql = "SELECT * FROM `users` WHERE `name` = {$_SESSION['user_name']}";not$sql = "SELECT * FROM `users` WHERE `name` = '{$_SESSION['user_name']}'";because there is ' for session and ' for username so it not work 4 '

Link to comment
Share on other sites

wait is $sql work for mysql too ?
$sql is just a variable name. You could call it $dfasdgkhlasigsfighasd if you really wanted to - just pass the right var into mysql_query().
because there is ' for session and ' for username so it not work 4 '
Not quite. The curly braces fix that. It is incorrect to leave the quotes out, that is what is breaking your query!
Link to comment
Share on other sites

You can test this for yourself. If $_SESSION['user_name'] is "joe", then with this statement:$sql = "SELECT * FROM `users` WHERE `name` = {$_SESSION['user_name']}";if you print $sql you will see that it equals this:SELECT * FROM `users` WHERE `name` = joeit should say this:SELECT * FROM `users` WHERE `name` = 'joe'

Link to comment
Share on other sites

I just came up with this,$name=joe and echo $name and it workedso i made$name='$_SESSION["user_name"]';and when i do <?php echo $name;?>it show the word $_SESSION["user_name"] instead of the session name....so how i make the $name is the session instead of the words...?if that work then i can $query = ("SELECT id, name, email, title FROM users WHERE name='$name'");

Link to comment
Share on other sites

Putting single quotes around a string does not do variable substitution. If you want to substitue variables you need to use double quotes. Listen, all you need to do is add single quotes around the value in the query. That's it.$sql = "SELECT * FROM `users` WHERE `name` = '{$_SESSION['user_name']}'";If you want to store the value in the session in another variable you don't need to put quotes around it.$name=$_SESSION["user_name"];$name="the value in the session is {$_SESSION['user_name']}";

Link to comment
Share on other sites

Great it worked ! thanksbut look at'{$_SESSION['user_name']}'then the qutoes is '{$_SESSION['']}'because there an other '' for user name and have '' for session then there 4 '''' will it still work?__________________edit:are there anyway that my data i put in missing? so last time i asked about update info to database and it successful and it work everything fine but now there no data for title ( luck i use echo row name too) anyway it possible like cookie update and after few days it delete itself?

Link to comment
Share on other sites

because there an other '' for user name and have '' for session then there 4 '''' will it still work?
The string is a double-quoted string, it has double quotes around it. There are single quotes inside the string, but they aren't going to end the string because the string was started with double quotes, not single quotes. So the single quotes are part of the text data inside the string itself. The single quotes inside the curly brackets are there so that PHP knows that it's dealing with an array (the $_SESSION array) and an element in the array. But if you want to know whether or not it works, the best thing to do is test it and print the string out to see what it did. Try changing things in the string and see how it affects the output. I can talk about quotes until my face is blue but the best thing to do is test it out and see what happens when you change different things.This works:$sql = "SELECT * FROM `users` WHERE `name` = '{$_SESSION['user_name']}'";PHP will have problems with this:$sql = "SELECT * FROM `users` WHERE `name` = '$_SESSION['user_name']'";The curly brackets are there to tell PHP that what is inside the brackets is a variable that it needs to replace.
it possible like cookie update and after few days it delete itself?
No, databases don't do anything automatically. It sounds like at some point you're updating the database with an empty title.
Link to comment
Share on other sites

um.. so now I go back to database and it not there again.....why don't you try..register login update the title field and then you can see what you type in at index too but after few sec like 20, i should say when you re go to the URL not reloading because when reloading it ask resend info again ( if you using firefox like me) so like reenter the URL and go there it not there anymore and same as database..... then it disappear..so this my codes

<?phpsession_start();require_once 'db.php';  $title = $_POST['title'];mysql_query("UPDATE users SET title='{$title}'WHERE name='{$_SESSION[user_name]}'") or exit(mysql_error()); $query = ("SELECT id, name, email, title FROM users WHERE name='{$_SESSION['user_name']}'");$name='';?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html>  <head>    <title></title>  </head>  <body><?php echo $name;?>    <?php    if (isset($_SESSION['user_id']))    {    ?>        Hello, <?php echo $_SESSION['user_name']; ?>,        <p>start row</p><?php$sql = "SELECT * FROM `users` WHERE `name` = '{$_SESSION['user_name']}'";$result = mysql_query($sql);while($row = mysql_fetch_array($result)){echo $row['title'];echo $row['name'];}?><p>end row</p>              <form action="index.php" method="post">    <input type="hidden" name="page_mode" value="">    <div class="left_box">title FOR FORM</div>    <div class="right_box"><input type="text" name="title" size="30" maxlength="255" value="title"></div>    <div class="left_box"> </div>    <div class="right_box"><input type="submit" value="update" size="30"></div>    <br><br>    <a href="logout.php">Log out</a>    <?php    }    else    {    ?>    Click <a href="register.php">here</a> to register or click <a href="login.php">here</a> to log in.    <?php    }    ?>  </body></html>

Link to comment
Share on other sites

The first thing you do on that page is update the title in the database. You don't check to see if the form was even submitted, you just update the database. If there's nothing in $_POST['title'] then you're going to update the database with an empty title.

Link to comment
Share on other sites

to avoid syntax headaches like you had above, I will sometimes do something like this

$sql = "SELECT * FROM `users` WHERE `name` = '" . $_SESSION['user_name'] . "'";

the only odd part is that you end up with a double-single-double quote thing, but I still find it easier to keep track of what quotes go where, since you are completely out of the double quote part

Link to comment
Share on other sites

To justsomeguyO what you mean is like everytime i go to index it automatically update it, and i didn't set update AFTER press update button and don't update if not press the buttonso I look at the register page you have in php tips andif ($_POST['title'] == ''){echo "there nothing in input box";}else{mysql_query("UPDATE users SET title='{$title}'WHERE name='{$_SESSION[user_name]}'") or exit(mysql_error());}that one work !! never done something so successful__________________________________To smartalcowhat you saying is . . is another kind of quotes right ( if right you don't need to reply) __________________________________EDIT EDITSo I making a website that make website for people ( the simplest way using simple code) and should i send data to database and read them whenever they go to their website for use fopen function to change a file? which one is better ?

Link to comment
Share on other sites

ok so like easy to see what each user have then open their files each time ... a quick questionsmy codes is in http://clanwebsite.co.cc/admin/register.phpand i make dir in ../namewhich is http://clanwebsite.co.cc/name right?__________________________________I found out that make a new folder is mkdirdon;t really know how to use it$folder = trim($_POST['folder']); // trim to remove whitespace$folder = $_REQUEST['foldername'];if(is_dir(\"..\$folder"))echo "directory exists!";else{mkdir(\"../'$folder'");echo "folder created";}_____________THE FORM IS_____________ <div class="left_box">Your Folder(you put Myclan and your URL will be clanwebsite.co.cc/myclan )</div> <div class="right_box"><input type="text" name="folder" size="30"></div>and there problems....what mkdir(\"../'$folder'"); mean , and why there a \ behind the mkdir( ?

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...