Jump to content

& in text is giving me fits


shanetastic

Recommended Posts

$Rep = htmlspecialchars($_GET["Rep"]);
echo $Rep;

 

The above will print "John Doe & Assoc." (without the quotes)

 

$result = mysql_query("SELECT RepName FROM Reps WHERE Repname = '".$Rep."'");
$repcount = mysql_num_rows($result);
echo $repcount;

 

The above code prints "0"

 

If I delete the first block of code and replace it with

$Rep = "John Doe & Assoc."

 

The second block of code will then return a "1" as it should.  Why is one returning a 0 and one returning a 1 when the text in $Rep appears to be identical in both cases?  This problem only appears to be occurring when the $Rep value contains an &.

 

 

 

Link to comment
Share on other sites

htmlspecialchars is replacing "&" with "&"

 

As smoseley states htmlspecialchars() is escaping the input to make it safe for HTML output. It will modify the value if certain characters are in it. You need to make a decision if you will run values through htmlspecialchars() before storing them or not. Then you need to do the same thing with values before using them for comparison. You could run values through htmlspecialchars() before storing them and then you can just echo them to the page. However, I would advise against this. I prefer to store values in their "native" state - i.e. no escaqping/sanitizing. If you escape the values for a specific purpose (in this case HTML output) you cannot effectively reverse the process if you need the data for a different output.

 

So, it seems you are doing just that - storing the value without any escaping. So, you need to not escape values if you are going to use them for comparisons in queries. Just make sure you use the appropriate escape functions when outputting the values. But, what you SHOULD be doing is running the value through mysql_real_escape_string() to prevent SQL Injection.

 

Also, it is not common to have a query looking for an exact comparison to a string like this. Typically, you will see LIKE comparisons. So, if the user entered "john" or "doe", it would find the same record. That would be implemented like this

$Rep = mysql_real_escape_string($_GET['Rep']);
$query = "SELECT RepName FROM Reps WHERE Repname LIKE '%$Rep}%'";
$result = mysql_query($query) or die("Query: $query<br>" . mysql_error());

 

I would also advise against building your query inside the mysql_query() function. Instead, build the query as a string variable that you can echo to the page when there are errors. It makes debugging much simpler.

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.