Jump to content

mysql_real_escape_string and arrays


RLJ

Recommended Posts

Hi all,

 

I use mysql_real_escape_string on user inputs before using them in a MySQL query. However, some of my queries use arrays or imploded arrays, for example a query of the form: SELECT .. FROM .. WHERE .. IN ..

 

It seems like in these cases I can't use mysql_real_escape_string, am I correct in thinking this? If so, what can I use instead to ensure the best possible security against SQL injections?

 

Thanks!

Link to comment
Share on other sites

Do not arbitrarily apply stripslashes(). You can use array_map() to escape the elements of a single-dimensional array, or you can use a loop if you need to handle some data types/values in the array differently from others.

 

$array = array_map('mysql_real_escape_string', $array);

Link to comment
Share on other sites

Thanks!

 

Just one more question though, because now I'm running into difficulties with my query. This is basically what I have:

<?php
$EXPfields = array_map('mysql_real_escape_string', $_SESSION['EXPfields']); $EXPfields = implode("', '", $EXPfields); 
$EXPvalues = array_map('mysql_real_escape_string', $_SESSION['EXPvalues']); $EXPvalues = implode("', '", $EXPvalues);

$insert = mysql_query   ("INSERT INTO tablename (ID,'".$EXPfields."') VALUES ('$ID','".$EXPvalues."')");
?>

 

But this gives me an SQL syntax error. I've been looking over my code again and again, but I can't spot the mistake. Help pls!!

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.