Jump to content

What's wrong with this syntax?


eMonk

Recommended Posts

$height = trim(htmlentities(mysqli_real_escape_string($db,$_POST['height'],ENT_QUOTES)));

 

Here's the error message I'm getting:

 

Warning: mysqli_real_escape_string() expects exactly 2 parameters, 3 given

 

I just added in the htmlentities and ENT_QUOTES to the line but not sure how to format it. Any ideas?

 

Link to comment
https://forums.phpfreaks.com/topic/247470-whats-wrong-with-this-syntax/
Share on other sites

$height = trim(htmlentities(mysqli_real_escape_string($db,$_POST['height'],ENT_QUOTES)));

$height = trim(htmlentities(mysqli_real_escape_string($db,$_POST['height']), ENT_QUOTES));

 

There's no need to fit everything onto one line. Split it up, it makes the code more readable:

 

$height = trim($_POST['height']);
$height = mysqli_real_escape_string($db, $height);

 

I actually left out htmlentities(), as this is something you should do as you output user input, not prepare it for saving to a database.

Why "ugly"? I mean, I use htmlspecialchars() myself generally, but htmlentities() just encodes more characters. When escaping user input the idea is you have little to no HTML anyway, so it's not exactly ugly but just more than necessary.

Ugly is mostly cosmetic in this case. The important reason is not having to deal with character encoding, assuming ISO-8859-1, ISO-8859-15, UTF-8, cp866, cp1251, cp1252, or KOI8-R

 

The following

<meta http-equiv="Content-Type" content="text/html;charset=utf-8" >
<?php 

$phrase = "Mon collègue a étudié à <b>l'hôpital</b>. Il est un garçon <i>naïf</i>";

echo 'entities: '. htmlentities( $phrase, ENT_COMPAT, 'UTF-8' ) . "\n";
echo 'specialchars: '. htmlspecialchars( $phrase );

?>

Outputs

<meta http-equiv="Content-Type" content="text/html;charset=utf-8" >
entities: Mon collègue a étudié à <b>l'hôpital</b>. Il est un garçon <i>naïf</i>
specialchars: Mon collègue a étudié à <b>l'hôpital</b>. Il est un garçon <i>naïf</i>

 

Both output the same on the page. You're adding extra parsing, and extra data to send to the client with no advantage. On top of that, you have to specify your character set if you want to use UTF-8.

Archived

This topic is now archived and is closed to further replies.

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