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

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.