Digitry Designs Posted January 24, 2010 Share Posted January 24, 2010 Ok, I am so lost and frustrated. I want to display all data that a user has submitted. I am able to display all data, but not all data for the logged in user. Do I have to use sessions to do this? Here is what I have so far. I have been told that mysql_fetch_assoc function is what I need. However, I know very little about mysql and how to use it. I am a complete newb here and swimming in deep water!! Here is the members area that actually displays all data. <?php // Connects to your Database mysql_connect("localhost", "root", "") or die(mysql_error()); mysql_select_db("login") or die(mysql_error()); //checks cookies to make sure they are logged in if(isset($_COOKIE['ID_my_site'])) { $username = $_COOKIE['ID_my_site']; $pass = $_COOKIE['Key_my_site']; $check = mysql_query("SELECT * FROM users WHERE username = '$username'")or die(mysql_error()); while($info = mysql_fetch_array( $check )) { //if the cookie has the wrong password, they are taken to the login page if ($pass != $info['password']) { header("Location: login.php"); } //otherwise they are shown the admin area else { echo "Admin Area<p>"; echo "<a href=logout.php>Logout</a>"; } } } else //if the cookie does not exist, they are taken to the login screen { header("Location: login.php"); } ?> <form action="insert.php" method="post"> <p align="left"> </p> <table width="87%" border="0"> <tr> <td width="34%">Date: </td> <td width="60%"><input type="text" name="date" /></td> <td width="6%"> </td> </tr> </table> <table width="87%" border="0"> <tr> <td width="34%">Firstname: </td> <td width="60%"><input type="text" name="firstname" /></td> <td width="6%"> </td> </tr> </table> <table width="87%" border="0"> <tr> <td width="34%">Lastname: </td> <td width="60%"><input type="text" name="lastname" /></td> <td width="6%"> </td> </tr> </table> <table width="87%" border="0"> <tr> <td width="34%">Job Name:</td> <td width="60%"><input type="text" name="jobname" /></td> <td width="6%"> </td> </tr> </table> <table width="87%" border="0"> <tr> <td width="34%">Time In:</td> <td width="60%"><input type="text" name="timein" /></td> <td width="6%"> </td> </tr> <tr> <td>Time Out:</td> <td><input type="text" name="timeout" /></td> <td> </td> </tr> <tr> <td>Lunch:</td> <td><input type="text" name="lunch" /></td> <td> </td> </tr> <tr> <td>Total Hours:</td> <td><input type="text" name="totalhours" /></td> <td> </td> </tr> </table> <table width="87%" border="0"> <tr> <td width="7%"> </td> <td width="7%"> </td> <td width="7%"> </td> <td width="13%"> </td> <td width="38%"><input type="submit" /></td> <td width="7%"> </td> <td width="7%"> </td> <td width="7%"> </td> <td width="7%"> </td> </tr> </table> <p> </p> <p align="center"> </p> <p align="left"> </p> <p align="center"> </p> </form> <div align="center"> <p> </p> <p> </p> <p> </p> <p> </p> <p> </p> </div>"; <?php //connect to the data base mysql_connect("localhost", "root", "") or die(mysql_error()); mysql_select_db("time_sheets") or die(mysql_error()); //get info and parse that info $query="SELECT * FROM employees"; $result=mysql_query($query); $num=mysql_num_rows($result); mysql_close(); echo "<b><center>Database Output</center></b><br><br>"; $i=0; while ($i < $num) { $date=mysql_result($result,$i,"date"); $first=mysql_result($result,$i,"FirstName"); $last=mysql_result($result,$i,"LastName"); $job=mysql_result($result,$i,"JobName"); $timein=mysql_result($result,$i,"TimeIn"); $timeout=mysql_result($result,$i,"TimeOut"); $lunch=mysql_result($result,$i,"Lunch"); $total=mysql_result($result,$i,"TotalHours"); echo "<b>$date $first $last</b><br>$job $timein - $timeout Lunch (-$lunch)<br>Total hours for $date = $total<hr><br>"; $i++; } ?> If it helps, my db for login is called login and my database with the info i want to parse is called time_sheets. I am so lost and at my wits end here. Can anyone help me??? Quote Link to comment https://forums.phpfreaks.com/topic/189599-just-need-to-get-php-to-get-a-username-from-mysql/ Share on other sites More sharing options...
jl5501 Posted January 24, 2010 Share Posted January 24, 2010 There appears to be a lot of confusion here between databases and tables. I see your login database, which has a table called users, which is being used to check the login credentials Then you are connecting to a different database, called timesheets where you are reading from a table called employees. It is not clear why 2 separate databases are being used here, but that is not necessarily a problem. Assuming that the employees table in the timesheets database, contains the field names in your loop, then what you have should work, except that I personally dislike the mysql_result() way of doing things. So making the assumption that all the field names you have in your code, I offer 2 alternative ways of doing that output loop. Method 1 : $query="select * from employees"; $result=mysql_query($query); echo "<b><center>Database Output</center></b><br><br>"; while ($row = mysql_fetch_assoc($result)) { $date = $row['date']; $first = $row['FirstName']; $last = $row['LastName']; $job = $row['JobName']; ........ echo "<b>$date $first $last</b><br>$job $timein - $timeout Lunch (-$lunch)<br>Total hours for $date = $total<hr><br>"; } mysql_close(); Method 2 ( my personal preference ) $query="select * from employees"; $result=mysql_query($query); echo "<b><center>Database Output</center></b><br><br>"; while ($row = mysql_fetch_object($result)) { $date = $row->date; $first = $row->FirstName; $last = $row->LastName; $job = $row->JobName; ........ echo "<b>$date $first $last</b><br>$job $timein - $timeout Lunch (-$lunch)<br>Total hours for $date = $total<hr><br>"; } mysql_close(); But again, these 2 methods are simply alternatives to what you have, and the data handled will be the same Quote Link to comment https://forums.phpfreaks.com/topic/189599-just-need-to-get-php-to-get-a-username-from-mysql/#findComment-1000747 Share on other sites More sharing options...
Digitry Designs Posted January 24, 2010 Author Share Posted January 24, 2010 There appears to be a lot of confusion here between databases and tables. I see your login database, which has a table called users, which is being used to check the login credentials Then you are connecting to a different database, called timesheets where you are reading from a table called employees. It is not clear why 2 separate databases are being used here, but that is not necessarily a problem. Assuming that the employees table in the timesheets database, contains the field names in your loop, then what you have should work, except that I personally dislike the mysql_result() way of doing things. The reason for the 2 seperate databases is simple. I am a newb!!! lol However, what I am trying to accomplish is to get the user name (last name) of the person who is logged in so the php will parse the database information pertaining only to that user. In other words, If I log in the only information from the database that is parsed is all fields in the rows in which my last name is found. Same for all other users. I was thinking some type of way of setting up strings to store the information could work. call one db and store info in a string like so: $username = "SELECT username FROM users WHERE (\\\currently logged in user- what goes here?///)"; call the other data base and use said string in array? is that right? $query = "SELECT * FROM employees where $username"; $result=mysql_query($query); $num=mysql_num_rows($result); after this I run a loop and parse all info for only the user that is logged in. Quote Link to comment https://forums.phpfreaks.com/topic/189599-just-need-to-get-php-to-get-a-username-from-mysql/#findComment-1000920 Share on other sites More sharing options...
jl5501 Posted January 24, 2010 Share Posted January 24, 2010 Ok lets look at this step by step I will need to make a lot of assumptions but you can correct as necessary Assuming you are starting off with an id of a logged in user and that is represented by a user_id field in the users table. then you would do this $ query = "select * from users where user_id='$user_id'"; $result = mysql_query($query); $user_record = mysql_fetch_assoc($result); That would give you access to all the information in the user record So if you had a name there it could be $username = $user_record['username']; then you could call the employee table like $query = "select * from employees where username='$username'"; $result = mysql_query($query); $emp_record = mysql_fetch_assoc($result); and then you could access all the fields in the employee record. There is a lot more you could do with checking you have results etc, but that is the bare bones of it Quote Link to comment https://forums.phpfreaks.com/topic/189599-just-need-to-get-php-to-get-a-username-from-mysql/#findComment-1000929 Share on other sites More sharing options...
Digitry Designs Posted January 25, 2010 Author Share Posted January 25, 2010 Ok lets look at this step by step I will need to make a lot of assumptions but you can correct as necessary Assuming you are starting off with an id of a logged in user and that is represented by a user_id field in the users table. then you would do this $ query = "select * from users where user_id='$user_id'";///// where do I get $user_id??? $result = mysql_query($query); $user_record = mysql_fetch_assoc($result); That would give you access to all the information in the user record So if you had a name there it could be $username = $user_record['username']; then you could call the employee table like $query = "select * from employees where username='$username'"; $result = mysql_query($query); $emp_record = mysql_fetch_assoc($result); and then you could access all the fields in the employee record. There is a lot more you could do with checking you have results etc, but that is the bare bones of it This all makes complete sense, but the $user_id string. What is that. where did it come from and how does it know who is logged in? all i have is a simple login that compares info to the data base and sets a cookie. Now everything is accessed by checking the cookie. to log out, it is a simple destroy cookie snippet. Is what i am trying to do possible, or do I have to completely redo my login code? Many thanks. Quote Link to comment https://forums.phpfreaks.com/topic/189599-just-need-to-get-php-to-get-a-username-from-mysql/#findComment-1001012 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.