Jump to content

Please help with special characters


lingo5
Go to solution Solved by TOA,

Recommended Posts

Hi,

I'm trying to update my database with this query:

 

$id_categoria=$_POST["id_categoria"];
$id_subcategoria=$_POST["id_subcategoria"];
$articulo_tit=$_POST["articulo_tit"];
$articulo_descripcion=$_POST["articulo_descripcion"];
$articulo_novedad=$_POST["articulo_novedad"];
$articulo_visible=$_POST["articulo_visible"];
$articulo_oferta=$_POST["articulo_oferta"];

$Sql="UPDATE t_articulos SET  id_categoria='$id_categoria',id_subcategoria='$id_subcategoria',articulo_tit='$articulo_tit',articulo_descripcion='$articulo_descripcion',articulo_novedad='$articulo_novedad',articulo_visible='$articulo_visible',articulo_oferta='$articulo_oferta',articulo_imagen='$filePath' WHERE id_articulo='$colname_productos'";

mysql_query($Sql) or die('Error, query failed : ' . mysql_error()); 

the problem is I get an error when there are spacia characters in the entered text such apostrophes etc.

Any suggestions?

thanks

Link to comment
Share on other sites

  • Solution

For each of these

 

$id_categoria=$_POST["id_categoria"];
$id_subcategoria=$_POST["id_subcategoria"];
$articulo_tit=$_POST["articulo_tit"];
$articulo_descripcion=$_POST["articulo_descripcion"];
$articulo_novedad=$_POST["articulo_novedad"];
$articulo_visible=$_POST["articulo_visible"];
$articulo_oferta=$_POST["articulo_oferta"];

it would be

$id_categoria=mysql_real_escape_string($_POST["id_categoria"]);
[...]

Then just use the variables as you normally would.

 

Deprecated means they will stop supporting it soon so you should switch to the mysqli group of functions. Here's a link to get you started: mysqli.

Edited by TOA
Link to comment
Share on other sites

Actually, I wouldn't use output escaping there. That's where I'd validate the input, to make sure it conforms to the expected formats.

 

Only when sending the data to the SQL (query) would I escape it, to prevent both SQL injections and mangling the content when sent to other systems (like the browser, in case of validation errors).

The result would look something like this, in pseudo-code:

if (submitted) {
    $errors = array ()
    $name = validate ('name', POST['name']));
    $email = validate ('email', POST['email']));
    // And so forth.

    if (!empty ($errors)) {
        // Show validation errors and repopulated form.
        return;
    }

    // Now we escape output for SQL.
    $query = "UPDATE TABLE SET field_1 = '%s', field_2 = %d, field_3= '%s";
    $query = sprintf ($query, mres ($name), $zip, mres ($email));

    // And the rest.
}
The reason I use sprintf here is to avoid having (too many) function calls in the middle of a string, which would make the actual SQL query a lot harder to read than necessary.
Link to comment
Share on other sites

Actually, I wouldn't use output escaping there. That's where I'd validate the input, to make sure it conforms to the expected formats.

 

Only when sending the data to the SQL (query) would I escape it, to prevent both SQL injections and mangling the content when sent to other systems (like the browser, in case of validation errors).

 

Valid point. Since there was nothing in between the variable declarations and the actual query, I failed to notice the need for that comment. But very worth pointing out. Good catch.

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.