Jump to content

ltrim and mysql_real_escape_string problem


Walker33

Recommended Posts

I've been using mysql_real_escape_string to allow for Irish names for a login, and I just added ltrim and rtrim to the code to eliminate accidental spaces at the beginning and end.  Everything worked except the $memlname variable.  When I pulled the mysql_real_escape_string snippet from the code, the ltrim and rtrim worked for $memlname variable.  Not sure what I'm doing wrong.  Any help would be greatly appreciated.

 

<?php

//this allows for accidental extra spaces at beginning and end of name

$memfname = ltrim($memfname);

$memlname = ltrim($memlname);

$mememail = ltrim($mememail);

$mempass = ltrim($mempass);

 

$memfname = rtrim($memfname);

$memlname = rtrim($memlname);

$mememail = rtrim($mememail);

$mempass = rtrim($mempass);

 

//this allows for Irish names like O'Sullivan, O'Donnell, etc.

$memlname = mysql_real_escape_string($_POST['memlname']);

?>

 

Link to comment
Share on other sites

thanks.  Came across ltrim and rtrim befor trim.  You're right, cleaner with trim.

 

I was using mysql_real_escape_string to stop a single quote ' from breaking up the input query and causing the error, as in O'Malley, O'Sullivan, etc.  To this point, it seems to have worked.  Am I misunderstanding something?

 

I took your advice and changed ($_POST['memlname'] to simply ($memlname) and now all is working as it should.  If you care to explain why this is, I'm always eager to learn.  Thanks for the help!

 

New code:

 

//this allows for accidental extra spaces at beginning and end of name
$memfname = trim($memfname);
$memlname = trim($memlname);
$mememail = trim($mememail);
$mempass = trim($mempass);

//this allows for Irish names like O'Sullivan, O'Donnell, etc.
$memlname = mysql_real_escape_string($memlname);

Link to comment
Share on other sites

Explain why ($_POST['memlname']) didn't work, but ($memlname) does work?  When I used the same trim code, but

 

$memlname = mysql_real_escape_string($_POST['memlname']);

 

the $memlname variable would not allow whitespace.  When I took your suggestion and used:

 

$memlname = mysql_real_escape_string($memlname);

 

instead, then $memlname variable would allow the whitespace at beginning and end.  Why is that?  Thanks.

Link to comment
Share on other sites

You trimmed the value of $_POST['memlname'] and store that value into the variable $memlname. However, the value of $_POST['memlname'] doesn't change. So the obvious choice is to use the trimmed value rather than the original one. If you had used $_POST['memlname'] instead of $memlname, there's no point in trimming it. Those lines of code would have no purpose. It's like this:

 

$a = 5;
$b = 4;

$c = 4 + 5;
echo $c;

 

I assigned the variables $a and $b to be 5 and 4 respectively. Then when I initialized $c, I chose to inline the numbers 4 and 5 rather than using the variables. So in that code, the first 2 lines are useless. Same with your situation.

 

Does that help?

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.