Jump to content

[SOLVED] trying to solve the problem


php_guest

Recommended Posts

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]'");
        }
?>

Link to comment
https://forums.phpfreaks.com/topic/142830-solved-trying-to-solve-the-problem/
Share on other sites

<?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?

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>

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>

Archived

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

×
×
  • Create New...

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.