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

Ask about a PHP problem here.
Post Reply
Shahlin
Posts: 49
Joined: Sat Jan 14, 2012 10:35 am

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

Post 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]
Just A PHP Beginner!
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

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

Post 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]
Last edited by Temor on Fri Feb 03, 2012 6:47 am, edited 1 time in total.
Shahlin
Posts: 49
Joined: Sat Jan 14, 2012 10:35 am

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

Post 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?
Just A PHP Beginner!
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

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

Post 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]
Image
Shahlin
Posts: 49
Joined: Sat Jan 14, 2012 10:35 am

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

Post 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
Just A PHP Beginner!
Shahlin
Posts: 49
Joined: Sat Jan 14, 2012 10:35 am

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

Post 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]
Just A PHP Beginner!
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

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

Post 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.
Image
Shahlin
Posts: 49
Joined: Sat Jan 14, 2012 10:35 am

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

Post 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!? :?:
Just A PHP Beginner!
lakc
Posts: 32
Joined: Fri Oct 21, 2011 6:05 pm

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

Post 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.
Post Reply