ccmcs Posted February 27, 2011 Share Posted February 27, 2011 Hi, I've been scratching my head for a while now about how to do this, I'm relatively new to php and mysql and perhaps foolishly taking on creating a user area for a website. I have everything else working, all of my register account functions and confirmations and all of the login scripts etc. I have created a profile page which returns various information to the user (this bit works fine) and I've got some nice show/hide toggles running with some javascript/css but my intention is to allow the user to change thier information (e-mail address, contact phone number and also whether they are subscribed to the e-mail list), it also displays any support tickets or messages. So after the long intro, here's what I'm struggling with... I have a form in a visibility toggled <div> which submits a 'change_email' script, so a user wants to change their e-mail, clicks on change, the <div> appears, they bang in the new e-mail and hit submit. My php script appears to work (because it doesn't throw up any errors), until you realise that actually it's not updated the record in the db... I'm using mysql_query("UPDATE users SET email='$new_email' WHERE username='$user'"); Do I need to setup variables for all of the information in the db (name, username, password, email, contno etc etc) and include them in the command to get it to work or should that just pick the correct record and then update it? If that is the case is there a way I can include 'blank' variables so I don't have to set them all up... e.g. mysql_query("UPDATE users SET user='',password='',email='$new_email', etc WHERE username='$user'"); Many thanks in anticipation Quote Link to comment Share on other sites More sharing options...
ronnie3148 Posted February 27, 2011 Share Posted February 27, 2011 I don't see a problem try adding the code I have below and I hope you didn't forget to add your variables i.e. $new_email= $POST_['email']; ........username='$user'") or die("MySQL ERROR: ".mysql_error()); this should tell you what the deal is... Quote Link to comment Share on other sites More sharing options...
flolam Posted February 27, 2011 Share Posted February 27, 2011 I actually don't see a problem with the mysql you have, although you mustn't use the second one, it will empty those fields. Are you sure that $user contains the correct username? Quote Link to comment Share on other sites More sharing options...
ccmcs Posted February 27, 2011 Author Share Posted February 27, 2011 thanks for the responses, I am using $_SESSION setups for username etc, I had a a problem with that format before tho because the info didn't seem to be retained across different scripts but I think that was only when scripts were in different folders on the server, I am not at the desk at the moment but I will try post the script asap, cheers again Quote Link to comment Share on other sites More sharing options...
ccmcs Posted February 28, 2011 Author Share Posted February 28, 2011 This is the script I am trying to use for the above, I've obviously taken out passwords etc (CAPS are replacements) but they are definitely correct and throwing up no errors, I've copied them from other scripts, I'm thinking that the problem could be to do with $_SESSION pull throughs from the other page but I'm not sure, is there a way to include the PHP for the script directly in the other file in the form and self action it? I was trying to keep things neater with seperate files, the profile.php is already riddled with js/php/html all over the shop! Anybody any ideas? basically the only button the form has is "change_email_submit" and the text field is "change_email" but you'll be able to see that from the code. <?php // Connects to the DB $link = mysql_connect('localhost', 'USER', 'PASS') or die("I couldn't connect to your database, please make sure your info is correct!"); mysql_select_db('DBNAME') or die("Database cannot be found"); if(!isset($link)) { echo "Connection Error:" mysql_error(); } // gathers existing e-mail from session $email = $_SESSION['email']; // Gathers new e-mail address from form $new_email = mysql_real_escape_string($_POST['change_email']) or die (mysql_error()); // Sets up username for session $user = $_SESSION['username']; // Sets up command to update record in DB $update = mysql_query("UPDATE users SET email='$new_email' WHERE username='$user'", $link); if(!isset($update)) { echo "Sorry but there has been an error, your e-mail address cannot be changed at this time, please contact E-MAIL ADDRESS to have your e-mail address altered manually."; } else { header('Location: PROFILEPAGE'); } ?> FORM CODE(extracted from another file): <div id="change_email_form" style=""> <form name="change_email_form" action="change_email.php" method="post"> <label for="change_email"> <div align="center">Please enter new e-mail address in the box below:</div> </label> <div align="center"> <input name="change_email" type="text" size="60" /> <input type="submit" value="Update Email" name="email_change_submit" /> </div> </form> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.