Jump to content

[SOLVED] Help with mysqli update


Narel

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 :P

 

Anyway... if that's not it, I'm off to sleep... good luck.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.