php_guest Posted January 28, 2009 Share Posted January 28, 2009 I have an user edit profile page where user can change data. There are titles which are already set (Age, Gender, Country, ...) + user can add some own titles (ie. My hobbies, My family, ...). There are two tables: users and titles. Example of table titles: ID - UserID - uTitle - uValue 1 - 10 - "Music" - "Rock, Rape" 2 - 10 - "About My Girl" - "She a pain at times" 3 - 11 - "Me" - "I am God" Below is the code I wrote for displaying all user generated titles and values. The problem I have is how to update changes, because I don't know the ID of each title because I use while loop. As you can see, I tried with isset($_POST) but it is not correct. Please tell what option could be used to update changes. <form action="<?=$_SERVER['PHP_SELF']?>" method="post"> <table> <?php $UserID =3; //UserID as user id, uTitle as user title, uValue as description/value of title connect(); $query = "SELECT * FROM titles WHERE UserID = $UserID"; $result = mysql_query($query); $row = mysql_fetch_assoc($result); echo "<br /><b>Edit your data:</b> <br /><br />"; while($row = mysql_fetch_assoc($result)) { extract ($row); echo " <tr> <td><b>$uTitle</b></td> <td><input type='text' name='$ID' value='$uValue'></td> </tr>"; } ?> <tr><td><input type="submit" value="Submit"></td> </tr> </table> </form> <?php while(isset($_POST)) { mysql_query("UPDATE titles SET uValue='_$POST[$uValue]' WHERE ID='$_POST[$ID]'"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/142830-solved-trying-to-solve-the-problem/ Share on other sites More sharing options...
gaza165 Posted January 28, 2009 Share Posted January 28, 2009 <?php if($_POST['subit']) { mysql_query("UPDATE titles SET uValue='$_POST[$uValue]' WHERE ID='$_POST[$ID]'"); } ?> <form action="<?=$_SERVER['PHP_SELF']?>" method="post"> <table> <?php $UserID =3; //UserID as user id, uTitle as user title, uValue as description/value of title connect(); $query = "SELECT * FROM titles WHERE UserID = $UserID"; $result = mysql_query($query); $row = mysql_fetch_assoc($result); echo "<br /><b>Edit your data:</b> <br /><br />"; while($row = mysql_fetch_assoc($result)) { extract ($row); echo " <tr> <td><b>$uTitle</b></td> <td><input type='text' name='$ID' value='$uValue'></td> </tr>"; } ?> <tr><td><input type="submit" value="Submit" name="subit"></td> </tr> </table> </form> i am not sure what your extract method does.... can you tell please? Quote Link to comment https://forums.phpfreaks.com/topic/142830-solved-trying-to-solve-the-problem/#findComment-748756 Share on other sites More sharing options...
printf Posted January 28, 2009 Share Posted January 28, 2009 You need to place your update action before the edit action, if you don't they won't see their changes after they update... I quick example... <?php // where is the database connect and select db coming from? connect (); // where is $UserID coming from? $UserID =3; if ( isset ( $_POST['edit'] ) ) { foreach ( $_POST['edit'] AS $name => $value ) { mysql_query ( "UPDATE titles SET uValue = '" . mysql_real_escape_string ( $value ) . "' WHERE ID = '" . intval ( $name ) . "' AND UserID = '" . $UserID . "';" ); } } ?> <form action="<?=$_SERVER['PHP_SELF']?>" method="post"> <table> <?php $result = mysql_query ( "SELECT * FROM titles WHERE UserID = '" . intval ( $UserID ) . "';" ); if ( mysql_num_rows ( $result ) > 0 ) { echo " <br /> <b> Edit your data: </b> <br /> <br /> "; while ( $row = mysql_fetch_assoc ( $result ) ) { echo " <tr> <td> <b> " . $row['uTitle'] . " </b> </td> <td> <input type='text' name='edit[" . $row['ID'] . "]' value='" . htmlentities ( $row['uValue'] ) . "'> </td> </tr> "; } ?> <tr> <td> <input type="submit" value="Submit"> </td> </tr> <?php } else { ?> <tr> <td> No titles to edit... </td> </tr> <?php } ?> </table> </form> Quote Link to comment https://forums.phpfreaks.com/topic/142830-solved-trying-to-solve-the-problem/#findComment-748768 Share on other sites More sharing options...
sasa Posted January 28, 2009 Share Posted January 28, 2009 try <?php if (isset($_POST['Submit'])){ foreach ($_POST['data'] as $ID => $uValue) { mysql_query("UPDATE titles SET uValue='$uValue' WHERE ID='$ID'"); } } ?> <form action="<?=$_SERVER['PHP_SELF']?>" method="post"> <table> <?php $UserID =3; //UserID as user id, uTitle as user title, uValue as description/value of title connect(); $query = "SELECT * FROM titles WHERE UserID = $UserID"; $result = mysql_query($query); if (mysql_num_rows($result)){ //$row = mysql_fetch_assoc($result); echo "<br /><b>Edit your data:</b> <br /><br />"; while($row = mysql_fetch_assoc($result)) { extract ($row); echo " <tr> <td><b>$uTitle</b></td> <td><input type='text' name='data[$ID]' value='$uValue'></td> </tr>"; } } else echo 'No data to edit'; ?> <tr><td><input type="submit" value="Submit"></td> </tr> </table> </form> Quote Link to comment https://forums.phpfreaks.com/topic/142830-solved-trying-to-solve-the-problem/#findComment-748921 Share on other sites More sharing options...
php_guest Posted January 28, 2009 Author Share Posted January 28, 2009 tnx everybody, you are great! printf: I have require (functions.php) on top. gaza165 extract() function uses array keys as variable names and values as variable values. Quote Link to comment https://forums.phpfreaks.com/topic/142830-solved-trying-to-solve-the-problem/#findComment-749050 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.