Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Database Update Problem - using Form
Old 12-11-2010, 06:18 PM Database Update Problem - using Form
Skilled Talker

Posts: 77
Name: adam
Location: UK
Trades: 0
Hi, I am creating a user profile section and just wondering whats the best way to create a update form. I can make it so one field gets updated, or all of them using this code. But as you are probably aware, when I try to update 2 or more fields the rest of the fields then get saved as blank. Just wondering how I can solve this issue. Thanks

PHP Code:
//echo $userid;
$update_contact $_POST["update_contact"];
$privacy $_POST["privacy"];
$relationship $_POST["relationship"];
$facebook_url $_POST["facebook"];
$sex $_POST["sex"];
$profile_picture $_POST["profile_picture_field"];

//Stops hackers
$update_contact mysql_real_escape_string($update_contact);
$privacy mysql_real_escape_string($privacy);
$relationship mysql_real_escape_string($relationship);
$sex mysql_real_escape_string($sex);
$facebook_url mysql_real_escape_string($facebook_url);
$profile_picture mysql_real_escape_string($profile_picture);

//Update Contact Only
if(($update_contact != NULL) && empty($privacy) && empty($relationship) && empty($sex) && empty($facebook_url) && empty($profile_picture))
{
    
$sql="UPDATE numbers SET number = '".$update_contact."' WHERE id = '".$userid."'";
    
$result=mysql_query($sql);
    if (!
$result) die('Invalid query: ' mysql_error());
    
    
header ('Location: ../control_panel.php');
    
//echo 'blah';
}
//Update Relationship only
else if(($relationship != NULL) && empty($privacy) && empty($update_contact) && empty($sex) && empty($facebook_url) && empty($profile_picture))
{
    
$sql="UPDATE numbers SET relationship = '".$relationship."' WHERE id = '".$userid."'";
    
$result=mysql_query($sql);
    if (!
$result) die('Invalid query: ' mysql_error());
    
    
header ('Location: ../control_panel.php');
    
//echo 'blah';
}
//Update Privacy Mode Only
else if(($privacy != NULL) && empty($update_contact) && empty($relationship) && empty($sex) && empty($facebook_url) && empty($profile_picture))
{
    
$sql="UPDATE numbers SET privacy_mode = '".$privacy."' WHERE id = '".$userid."'";
    
$result=mysql_query($sql);
    if (!
$result) die('Invalid query: ' mysql_error());
    
    
header ('Location: ../control_panel.php');
    
//echo 'blah2';
}
else if((
$facebook_url != NULL) && empty($update_contact) && empty($relationship) && empty($sex) && empty($privacy) && empty($profile_picture))
{
        
$sql="UPDATE numbers SET facebook_url = '".$facebook_url."' WHERE id = '".$userid."'";
    
$result=mysql_query($sql);
    if (!
$result) die('Invalid query: ' mysql_error());
    
    
header ('Location: ../control_panel.php');
    
//echo 'blah2';
}
else if((
$sex != NULL) && empty($update_contact) && empty($relationship) && empty($facebook_url) && empty($privacy) && empty($profile_picture))
{
        
$sql="UPDATE numbers SET sex = '".$sex."' WHERE id = '".$userid."'";
    
$result=mysql_query($sql);
    if (!
$result) die('Invalid query: ' mysql_error());
    
    
header ('Location: ../control_panel.php');
    
//echo 'blah2';
}
else if((
$profile_picture != NULL) && empty($update_contact) && empty($relationship) && empty($facebook_url) && empty($privacy) && empty($sex))
{
        
$sql="UPDATE numbers SET picture = '".$profile_picture."' WHERE id = '".$userid."'";
    
$result=mysql_query($sql);
    if (!
$result) die('Invalid query: ' mysql_error());
    
    
header ('Location: ../control_panel.php');
    
//echo 'blah2';
}
//Update All
else
{
    
$sql="UPDATE numbers SET number = '".$update_contact."', privacy_mode = '".$privacy."', relationship = '".$relaionship."', sex = '".$sex."', facebook_url = '".$facebook_url."', picture = '".$profile_picture."' WHERE id = '".$userid."' ";
    
$result=mysql_query($sql);
    if (!
$result) die('Invalid query: ' mysql_error());

    
header ('Location: ../control_panel.php');

__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
mintuz is offline
Reply With Quote
View Public Profile Visit mintuz's homepage!
 
 
Register now for full access!
Old 12-11-2010, 07:37 PM Re: Database Update Problem - using Form
Super Spam Talker

Posts: 879
Name: Paul W
Trades: 0
Curious ... What's the error message on the multiple column update? Can you echo the query so we can eyeball it. BTW, you can siplify code slightly by testing single variables for a value and building up the clauses one by one, with a bit of handling to get commas in.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE


*** New:
Please login or register to view this content. Registration is FREE
PaulW is offline
Reply With Quote
View Public Profile
 
Old 12-12-2010, 05:17 AM Re: Database Update Problem - using Form
Skilled Talker

Posts: 77
Name: adam
Location: UK
Trades: 0
there are no errors........it just wipes those fields to blank.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
mintuz is offline
Reply With Quote
View Public Profile Visit mintuz's homepage!
 
Old 12-12-2010, 07:05 AM Re: Database Update Problem - using Form
Super Spam Talker

Posts: 879
Name: Paul W
Trades: 0
Can you echo the query so we can eyeball it.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE


*** New:
Please login or register to view this content. Registration is FREE
PaulW is offline
Reply With Quote
View Public Profile
 
Old 12-12-2010, 08:42 AM Re: Database Update Problem - using Form
Skilled Talker

Posts: 77
Name: adam
Location: UK
Trades: 0
Code:
UPDATE numbers SET number = 'mintuz1989@gmail.com', privacy_mode = 'public', relationship = '', sex = '', facebook_url = 'mintuz', picture = '' WHERE id = '34'
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
mintuz is offline
Reply With Quote
View Public Profile Visit mintuz's homepage!
 
Old 12-12-2010, 08:45 AM Re: Database Update Problem - using Form
Skilled Talker

Posts: 77
Name: adam
Location: UK
Trades: 0
ive managed to do a work around by setting the form field values to values retrieved from the database then when it gets update those same values are inputted. But I don't know if that is safe/efficient.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
mintuz is offline
Reply With Quote
View Public Profile Visit mintuz's homepage!
 
Old 12-12-2010, 08:46 AM Re: Database Update Problem - using Form
Super Spam Talker

Posts: 879
Name: Paul W
Trades: 0
No, a query where every form field has been filled in please
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE


*** New:
Please login or register to view this content. Registration is FREE
PaulW is offline
Reply With Quote
View Public Profile
 
Old 12-12-2010, 09:03 AM Re: Database Update Problem - using Form
Skilled Talker

Posts: 77
Name: adam
Location: UK
Trades: 0
Code:
UPDATE numbers SET number = 'mintuz', privacy_mode = 'public', relationship = 'Taken', sex = 'Females', facebook_url = 'jjjjj', picture = 'x' WHERE id = '34'
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
mintuz is offline
Reply With Quote
View Public Profile Visit mintuz's homepage!
 
Old 12-12-2010, 11:22 AM Re: Database Update Problem - using Form
Super Spam Talker

Posts: 879
Name: Paul W
Trades: 0
DUH! sorry, looking at query only and ignoring code! By your logic you always fall into the final ELSE if two or more form fields are set, which isn't what you want. Have a look at http://www.mredding.co.uk/pw.html Test each form variable in turn. If it's not null, add to the set_clauses variable. Note the handling in relationship and privacy -- determining if comma needed - I've left that out of the remaining three for clarity so don't forget to add it. Finally, build the full query from the components and execute. If it fails, do something, else do your header call. Note the exit() -- always put one of these in after header Location -- else code lwoer down may execute which isn't usually what you want.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE


*** New:
Please login or register to view this content. Registration is FREE

Last edited by PaulW; 12-12-2010 at 11:35 AM..
PaulW is offline
Reply With Quote
View Public Profile
 
Old 12-12-2010, 11:39 AM Re: Database Update Problem - using Form
Super Spam Talker

Posts: 879
Name: Paul W
Trades: 0
ps I did a few quick edits in vi to convert your code to mine: I think the " and ' are correct but I wouldn't guarantee it.
No, missed a " at the end of the concatenation of the three bits of the query
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE


*** New:
Please login or register to view this content. Registration is FREE

Last edited by PaulW; 12-12-2010 at 12:13 PM..
PaulW is offline
Reply With Quote
View Public Profile
 
Old 12-12-2010, 12:21 PM Re: Database Update Problem - using Form
Skilled Talker

Posts: 77
Name: adam
Location: UK
Trades: 0
alright thanks alot that was very useful. you must of been bored to do a quick knock up lol thanks alot
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
mintuz is offline
Reply With Quote
View Public Profile Visit mintuz's homepage!
 
Reply     « Reply to Database Update Problem - using Form
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.33628 seconds with 12 queries