Page 1 of 1

How To Let A User Whose Logged in change his info!?

Posted: Thu Feb 02, 2012 2:10 pm
by Shahlin
This Page Is Currently Just For Updating Your Age!
When I Type An Age And Click On Update...It Doesn't Update In My Database! It's The SQL Query's Problem!
Please Help!
Thanks!

[syntax=php]
<?php

include_once 'config.php' ;

if (!isset($_SESSION['user_id'])&& empty ($_SESSION['user_id'])) {
echo 'You must be logged in to edit your profile.';
die () ;
} else {
if (isset ($_POST['submit'])) {
$age = $_POST ['age'] ;
if (!empty ($age)) {
$sql = "INSERT INTO `users` VALUES('','','','','','','','$age','','','')" ; //I WANT THE CHANGES SHOULD BE SAVED ONLY FOR THE USER WHO IS CURRENTLY LOGGED IN!
if ($sql_run = mysql_query ($sql)) {
die ('Your profile has been updated.') ;
} else {
die ('There Was A problem Updating Profile.') ;
}
} else {
echo 'Please Fill In Fields.' ;
}
}
}
?>

<form action="edit_profile.php" method="POST">
Age : <input type="text" name="age"><br><br>
<input type="submit" name="submit" value="Update!">

</form>[/syntax]

Re: How To Let A User Whose Logged in change his info!?

Posted: Thu Feb 02, 2012 5:43 pm
by Temor
INSERT INTO will insert a new row, not update one.
To update a users info you'll want to use UPDATE.

[syntax=php]$sql = "UPDATE `users` SET `age` = {$age} WHERE `user_id` = {$user_id}[/syntax]

Re: How To Let A User Whose Logged in change his info!?

Posted: Thu Feb 02, 2012 6:22 pm
by Shahlin
What If I Have Many Optional Fields To Fill In?

Should I Use

[syntax=php]
$sql = "UPDATE `users` SET `age`='$age' AND `gender`='$gender' "
[/syntax]

Is It Done That Way?

Re: How To Let A User Whose Logged in change his info!?

Posted: Fri Feb 03, 2012 1:06 am
by jacek
It should be a comma separated list of columns to update

[syntax=sql]UPDATE `table` SET `column` = 'value', `column2` = 'value2' WHERE `id`= 7[/syntax]

Re: How To Let A User Whose Logged in change his info!?

Posted: Fri Feb 03, 2012 1:38 pm
by Shahlin
jacek wrote:It should be a comma separated list of columns to update

[syntax=sql]UPDATE `table` SET `column` = 'value', `column2` = 'value2' WHERE `id`= 7[/syntax]


Thanks! I Tried! And It Works! :D

Re: How To Let A User Whose Logged in change his info!?

Posted: Fri Feb 03, 2012 1:41 pm
by Shahlin
One Last Question!

I Did What You Said...But Now What Happens Is If They Leave Age Empty And Their Next Field Empty...Their Age Updates And The Next Field Value Blanks Out From The Profile.

This is the current edit page!

[syntax=php]
<?php

include_once 'config.php' ;
include_once 'core.php' ;

$user_id = $_SESSION['user_id'] ;
if (!isset($_SESSION['user_id'])&& empty ($_SESSION['user_id'])) {
echo 'You must be logged in to edit your profile.';
die () ;
} else {
if (isset ($_POST['submit'])) {
$age = $_POST ['age'] ;
$status = $_POST ['status'] ;
if (!empty ($age) || !empty ($status)) {
$sql = "UPDATE `users` SET `age`='$age' , `status`='$status' WHERE `id`='$user_id'" ;
if ($sql_run = mysql_query ($sql)) {
die ('Your profile has been updated.') ;
} else {
die ('There Was A Problem Updating Profile.') ;
}
} else {
echo 'Please Fill In Fields.' ;
}
}
}
?>

<form action="edit_profile.php" method="POST">
Age : <input type="text" name="age"><br><br>
Status: <input type="text" name="status"><br><br>
<input type="submit" name="submit" value="Update!">

</form>

[/syntax]

Re: How To Let A User Whose Logged in change his info!?

Posted: Sat Feb 04, 2012 12:37 am
by jacek
You could populate the form fields with default values from the table, that way they will just update to the current value if they don't change them.

Re: How To Let A User Whose Logged in change his info!?

Posted: Sat Feb 04, 2012 12:09 pm
by Shahlin
jacek wrote:You could populate the form fields with default values from the table, that way they will just update to the current value if they don't change them.


How To Set A Default Value? :?

Is It Just Like....Making A Variable Of What They Previously Added!? :?:

Re: How To Let A User Whose Logged in change his info!?

Posted: Sun Feb 05, 2012 7:49 pm
by lakc
Why dont u read about the basic php on w3schools.com, there u may find answers to ure trivial questions

U write the code above this, to validate if empty or not in php or javascript.

[syntax=php]
<? $a="Default value"; ?>
<html><body>
<input name="test" type="text" value="<? echo $a ?>"/>
</body></html>
[/syntax]

Hope this wht u want, displays the text when u run the page in the browser instantly.

Cheers.