Narel Posted March 8, 2009 Share Posted March 8, 2009 Hello to everyone. I am new to PHP (this is my first post here) and i have been trying all day to write a simple function that enables a user to change his name using a form but unfortunately with no success. I use mysqli and procedural way. The code is the following: function change_name($changeName) { $connection = db_connection(); $id = $_SESSION['id']; echo "changeName is: ".$changeName."<br />"; //A visual check for changeName $query = "Update Users SET userName = '$changeName' Where userId = '$id'"; echo $query; //A visual check to see the query $result = mysqli_query($connection, $query); if ($result) echo '<p class="registerWarning">The name has changed.</p>'.$changeName; } When is executed the echo "changeName is: ".$changeName."<br />"; i can see the value of the variable but the problem is that inside the query the variable $changeName for some reason is Null. I don't know why. I have tried all the compinations of quotes and i dont think that this is the problem. Moreover when the echo "$query"; is executed all seems ok.. For example if i give the value "someone" in the form this is a typical echo: Update Users SET userName = 'someone' Where userId = '110' . Also if ($result) comes true end echo the rest. If someone knows what can i do please help.. Quote Link to comment Share on other sites More sharing options...
Mchl Posted March 8, 2009 Share Posted March 8, 2009 Welcome Just to make sure I understand: echo $query; displays well formed query, but when the query is actually run, it doesn't update the data as it should? Please use tags for posting your code in the future. Quote Link to comment Share on other sites More sharing options...
Narel Posted March 8, 2009 Author Share Posted March 8, 2009 Thank you for the quick response and i am sorry about the code tags. The problem is exactly what you describe. The field userName in the database gets Null when the query is executed. But why? Quote Link to comment Share on other sites More sharing options...
Mchl Posted March 8, 2009 Share Posted March 8, 2009 Please paste the table structure Use SHOW CREATE TABLE Users query and paste the results. Quote Link to comment Share on other sites More sharing options...
Narel Posted March 8, 2009 Author Share Posted March 8, 2009 Please paste the table structure Use SHOW CREATE TABLE Users query and paste the results. CREATE TABLE `users` ( `userId` mediumint(9) NOT NULL auto_increment, `userName` varchar(40) NOT NULL, `userEmail` varchar(32) NOT NULL, `userPassword` char(40) NOT NULL, `userBranch` tinyint(4) NOT NULL, `userRegDate` date default NULL, `userLastSign` date default NULL, `userRegion` varchar(40) default NULL, `userSex` set('m','f') default NULL, `userBirthDate` date default NULL, `userOccupation` tinytext, `userActivities` tinytext, `userPersonality` tinytext, `userWebsite` varchar(200) default NULL, `userPhone` varchar(40) default NULL, `userPic` varchar(100) default NULL, PRIMARY KEY (`userId`), UNIQUE KEY `userId` (`userId`) ) ENGINE=InnoDB AUTO_INCREMENT=112 DEFAULT CHARSET=latin1 Quote Link to comment Share on other sites More sharing options...
Mchl Posted March 8, 2009 Share Posted March 8, 2009 Ok... nothing wrong with that... Change $result = mysqli_query($connection, $query); to $result = mysqli_query($connection, $query) or die(mysqli_error($connection).": $query"); Maybe this will show something... Quote Link to comment Share on other sites More sharing options...
Narel Posted March 8, 2009 Author Share Posted March 8, 2009 Ok... nothing wrong with that... Change $result = mysqli_query($connection, $query); to $result = mysqli_query($connection, $query) or die(mysqli_error($connection).": $query"); Maybe this will show something... Nothing happened. Whatever i do the variable $changeName keeps getting Null inside the query for some reason. But thank you anyway. I hope i find it because it's at least irritating to have stuckt to this piece of code for the last 6+ hours.. Quote Link to comment Share on other sites More sharing options...
Mchl Posted March 8, 2009 Share Posted March 8, 2009 Yeah... this is strange... At first I thought you might have wrong datetype for userName field inn the table, but it's not the case. The query seem to be running all right... Last thing I can come up with today is to check, if you uploaded the modified file to your server... people sometimes forget about it... I do Anyway... if that's not it, I'm off to sleep... good luck. Quote Link to comment Share on other sites More sharing options...
Narel Posted March 9, 2009 Author Share Posted March 9, 2009 After many many hours i figured out what was wrong. I had to perform the simple check: if ($_POST['changeName']) get_action($_GET['action']); inside the function. This means that the only difference was that i had ommited the: if ($_POST['changeName']) But i still cannot understand why didnt work at first place because i assume that without the "if" check, the get_action("...") will be still called. Please, all beginners (like me) try to avoid these kind of mistakes. And Mchl, thanks for your time. 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.