princeofpersia Posted November 18, 2010 Share Posted November 18, 2010 Hi guys I have a registration form working fine, my database is as below: userid username email password repeatpassword I have added another column which is "name", users can update their profile once they have logged in so I have created updateprofile.php and when I login-->go to update profile and insert my name nothing adds to mysql name column this is my code below: <?php include ("global.php"); //username session $_SESSION['username']=='$username'; $username=$_SESSION['username']; //welcome messaage echo "Welcome, " .$_SESSION['username']."!<p>"; if ($_POST['register']) { //get form data $name = addslashes(strip_tags($_POST['name'])); $update = mysql_query("INSERT INTO users (name) VALUES ('$_POST[name]') WHERE username='$username'"); } ?> <form action='updateprofile.php' method='POST'> Company Name:<br /> <input type='text' name='name'><p /> <input type='submit' name='register' value='Register'> </form> can you please tell me where in this code is wrong? Im new in php so please excuse me if I have silly mistakes. thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/219091-insert-mysql/ Share on other sites More sharing options...
Pikachu2000 Posted November 18, 2010 Share Posted November 18, 2010 Before you can make use of any session variables, you need to have session_start() in the script. Most programmers make it the first thing in the code, right after the <?php open tag. Quote Link to comment https://forums.phpfreaks.com/topic/219091-insert-mysql/#findComment-1136111 Share on other sites More sharing options...
mikosiko Posted November 18, 2010 Share Posted November 18, 2010 .... //username session $_SESSION['username']=='$username'; $username=$_SESSION['username']; ... what???... I'm lost kind of lost with those 2 sentences... echo your $username variable and check if the value will be consistent with your SELECT and what Pikachu2000 suggested is also valid. Quote Link to comment https://forums.phpfreaks.com/topic/219091-insert-mysql/#findComment-1136113 Share on other sites More sharing options...
princeofpersia Posted November 18, 2010 Author Share Posted November 18, 2010 Thanks for help, I have added the session_start(); but still when i insert name, i can see the name added to my column in phpmyadmin, any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/219091-insert-mysql/#findComment-1136116 Share on other sites More sharing options...
litebearer Posted November 18, 2010 Share Posted November 18, 2010 ALSO when making a change to an EXISTING record use UPDATE not INSERT http://www.tizag.com/mysqlTutorial/mysqlupdate.php Quote Link to comment https://forums.phpfreaks.com/topic/219091-insert-mysql/#findComment-1136118 Share on other sites More sharing options...
princeofpersia Posted November 18, 2010 Author Share Posted November 18, 2010 I have echoed the username in the code echo "Welcome, " .$_SESSION['username']."!<p>"; and it works fine, the username shows up Quote Link to comment https://forums.phpfreaks.com/topic/219091-insert-mysql/#findComment-1136119 Share on other sites More sharing options...
princeofpersia Posted November 18, 2010 Author Share Posted November 18, 2010 Ive changed it to update but still can not see anything added, its funny because i dont even get an error :'( Quote Link to comment https://forums.phpfreaks.com/topic/219091-insert-mysql/#findComment-1136120 Share on other sites More sharing options...
litebearer Posted November 18, 2010 Share Posted November 18, 2010 may we see your most current coding Quote Link to comment https://forums.phpfreaks.com/topic/219091-insert-mysql/#findComment-1136121 Share on other sites More sharing options...
Pikachu2000 Posted November 18, 2010 Share Posted November 18, 2010 Post your current code, please. Quote Link to comment https://forums.phpfreaks.com/topic/219091-insert-mysql/#findComment-1136122 Share on other sites More sharing options...
princeofpersia Posted November 18, 2010 Author Share Posted November 18, 2010 there you go ________________________ <?php session_start(); include ("global.php"); //username session $_SESSION['username']=='$username'; $username=$_SESSION['username']; //welcome messaage echo "Welcome, " .$_SESSION['username']."!<p>"; if ($_POST['register']) { //get form data $name = addslashes(strip_tags($_POST['name'])); $update = mysql_query("UPDATE users VALUES ('$_POST[name]') WHERE username='$username'"); } ?> <form action='updateprofile.php' method='POST'> Company Name:<br /> <input type='text' name='name'><p /> <input type='submit' name='register' value='Register'> </form> Quote Link to comment https://forums.phpfreaks.com/topic/219091-insert-mysql/#findComment-1136124 Share on other sites More sharing options...
litebearer Posted November 18, 2010 Share Posted November 18, 2010 Psuedo code $query = "UPDATE table_name_here SET fieldname_here = 'variable_name_here WHERE some_field = 'some_value_here'"; $result = mysql_query($query); Quote Link to comment https://forums.phpfreaks.com/topic/219091-insert-mysql/#findComment-1136132 Share on other sites More sharing options...
princeofpersia Posted November 18, 2010 Author Share Posted November 18, 2010 thanks im trying it but what is going to be some_field here? Quote Link to comment https://forums.phpfreaks.com/topic/219091-insert-mysql/#findComment-1136135 Share on other sites More sharing options...
litebearer Posted November 18, 2010 Share Posted November 18, 2010 some_field would be replaced with an actual filed name ie user_name so - WHERE user_name = '$user_name' clear? Quote Link to comment https://forums.phpfreaks.com/topic/219091-insert-mysql/#findComment-1136138 Share on other sites More sharing options...
princeofpersia Posted November 18, 2010 Author Share Posted November 18, 2010 yup, worked fantastic, u r a star mate. thanks for your time and advice Quote Link to comment https://forums.phpfreaks.com/topic/219091-insert-mysql/#findComment-1136140 Share on other sites More sharing options...
princeofpersia Posted November 18, 2010 Author Share Posted November 18, 2010 one more question, now we assume we add another column named telephonenumber and we use the same query to update. Now if user doesnt fill the name and only enteres the telephone number name field overrites the empty space. how i should avoid that? SO on update we have two input text name telelphone I have only inserted telephone, it added the telephone but now the name is empty in php myadmin thanks Quote Link to comment https://forums.phpfreaks.com/topic/219091-insert-mysql/#findComment-1136146 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.