eleven0 Posted March 8, 2008 Share Posted March 8, 2008 I have members table where i store my members info and etc. I'm preparing an admin section to manage all of my members. In my admin index page, I list all of my members by using mysql and php. I want to put "Edit profile" link next to their names. When you click on, it will open a window where you can edit that profile. I can prepare a "Update.php" page by using forms and all. But how do i make it automatic? so when I add a new member to that table, it will automatically put the edit link next to their name. and yes i do use "id" in my table. what would be the easiest for me to do this? Quote Link to comment Share on other sites More sharing options...
ohdang888 Posted March 8, 2008 Share Posted March 8, 2008 if i understand what you're trying to do, then its easy well you use php and mysql <?php $result = mysql_query("SELECT `id`, `username` FROM `table`") while($row = mysql_fetch_row($result)){ // its either this or "mysql_fetch_assoc($result)" echo 'Member: '.$row['username']; echo '<a href="edit_profile.php?id='.$row['id'].'">Edit Profile<a/>'; } ?> Quote Link to comment Share on other sites More sharing options...
eleven0 Posted March 8, 2008 Author Share Posted March 8, 2008 Thanks that worked. I know i said i could prepare update.php. but can you help me with getting those id numbers to my edit.php page. I always used switch statements to define actions. how am going to do that? can you give me example for editing "username" row? Quote Link to comment Share on other sites More sharing options...
ohdang888 Posted March 8, 2008 Share Posted March 8, 2008 well this line right here: echo '<a href="edit_profile.php?id='.$row['id'].'">Edit Profile<a/>'; will direct you to edit_profile.php?id=the id of the user so you will grab that info from the url. its a certain code, and i'm not sure what it is, and i'm not at my home computer, so i don't know exactly what it is right now. But after you grab the url info, you would do something like this: $id = sql_quote['id']; $id = mysql_real_escape_string($id); // helps protect against injections $result = mysql_query("SELECT * FROM `table` WHERE `id` = '{$id}' ") i won't email to send the code later, so email me at tommycrsuh@gmail.com and i'll get it to you. Quote Link to comment Share on other sites More sharing options...
eleven0 Posted March 8, 2008 Author Share Posted March 8, 2008 k thanks..... Can someone else help me with my second question? Quote Link to comment Share on other sites More sharing options...
ohdang888 Posted March 8, 2008 Share Posted March 8, 2008 EDIT: fixed a few things..... anyways, echo '<a href="edit_profile.php?id='.$row['id'].'">Edit Profile<a/>'; will direct you to edit_profile.php?id=the id of the user on the edit page, you would have this code... <?php function sql_quote($data) { if (get_magic_quotes_gpc()) { $data = stripslashes($data); } return addslashes($data); } $id = sql_quote($_GET['id']); $action = sql_quote($_GET['action']); //connect to database $id = mysql_real_escape_string($id); // helps protect against injections $result = mysql_query("SELECT * FROM `table` WHERE `id` = '{$id}' ") ?> //then display all the user info code in text areas, <form action="edit_profile.php?action=edit> <textarea rows="10" cols="20" name="email" value="<?php echo $email ?>"> </textarea> <input type="submit" name="submit" value="save"/> </form> <?php if($action == 'edit'){ mysql query to save all that info } ?> Quote Link to comment Share on other sites More sharing options...
eleven0 Posted March 9, 2008 Author Share Posted March 9, 2008 <?php function sql_quote($data) { if (get_magic_quotes_gpc()) { $data = stripslashes($data); } return addslashes($data); } $id = sql_quote($_GET['id']); $action = sql_quote($_GET['action']); include 'config.php'; include 'opendb.php'; $id = mysql_real_escape_string($id); // helps protect against injections $query = "SELECT * FROM `members` WHERE `id` = '{$id}' "; $result = mysql_query($query) or die(mysql_error()); $name=$row['name']; $rank=$row['rank']; $lastname=$row['lastname']; $pos=$row['pos']; $s=$row['squad']; $asn=$row['asn']; $tour=$row['tour']; ?> <form name="update" action="edit_profile.php?action=edit method="post"> Name:<input type="text" name="name" value="<?php print $name; ?>"/> Last name:<input type="text" name="lastname" value="<?php print $lastname; ?>"/> Rank:<input type="text" name="rank" value="<?php print $rank; ?>"/> Pos:<input type="text" name="pos" value="<?php print $pos; ?>"/> <input type="submit" name="submit" value="save"/> </form> It's not getting the values from database and put them into text fields. What am i doing wrong? Quote Link to comment Share on other sites More sharing options...
ohdang888 Posted March 9, 2008 Share Posted March 9, 2008 well you never defined what "row" means.... you forgot this: $row = mysql_fetch_array($result); try and see if that does it. and in this: <form name="update" action="edit_profile.php?action=edit method="post"> you forgot a " .....needs to be this: <form name="update" action="edit_profile.php?action=edit" method="post"> Quote Link to comment Share on other sites More sharing options...
eleven0 Posted March 9, 2008 Author Share Posted March 9, 2008 <?php function sql_quote($data) { if (get_magic_quotes_gpc()) { $data = stripslashes($data); } return addslashes($data); } $id = sql_quote($_GET['id']); $action = sql_quote($_GET['action']); include 'config.php'; include 'opendb.php'; $id = mysql_real_escape_string($id); // helps protect against injections $query = "SELECT * FROM `members` WHERE `id` = '{$id}' "; $result = mysql_query($query) or die(mysql_error()); $row = mysql_fetch_array($result); $name=$row['name']; $rank=$row['rank']; $lastname=$row['lastname']; $pos=$row['pos']; $s=$row['squad']; $asn=$row['asn']; $tour=$row['tour']; ?> <form name="update" action="edit_profile.php?action=edit" method="post"> Name:<input type="text" name="name" value="<?php print $name; ?>"/> Last name:<input type="text" name="lastname" value="<?php print $lastname; ?>"/> Rank:<input type="text" name="rank" value="<?php print $rank; ?>"/> Pos:<input type="text" name="pos" value="<?php print $pos; ?>"/> <input type="submit" name="submit" value="save"/> </form> Still not working and not giving me an error either. Quote Link to comment Share on other sites More sharing options...
Xajel Posted March 9, 2008 Share Posted March 9, 2008 first is your members table names members or another thing ( like users ), if other than members then change ( SELECT * FROM `members`) to ( SELECT * FROM `TABLE_NAME_HERE` ) can you just delete the { & } from this line and give it a try from $query = "SELECT * FROM `members` WHERE `id` = '{$id}' "; to $query = "SELECT * FROM `members` WHERE `id`='$id' "; BTW, if your ID coulmn in the db is numeric ( normally ID's are INT ), then you may want to remove the single qutation to so you will use $query = "SELECT * FROM `members` WHERE `id`=$id "; Quote Link to comment Share on other sites More sharing options...
ohdang888 Posted March 9, 2008 Share Posted March 9, 2008 echo $query and see what you're even searching for is right. Quote Link to comment Share on other sites More sharing options...
eleven0 Posted March 9, 2008 Author Share Posted March 9, 2008 It's working now. But got a small problem. I can't echo my values in text areas. <br />Tour:<br /><textarea name="tour" value="<?php echo $tour; ?>" rows="30" cols="60"></textarea> Nothing is wrong with my variable, it works out of textarea. Quote Link to comment Share on other sites More sharing options...
eleven0 Posted March 9, 2008 Author Share Posted March 9, 2008 lol nevermind fixed... Thanks for help. Quote Link to comment 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.