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
https://forums.phpfreaks.com/topic/229376-mysql_real_escape_string-and-arrays/
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);

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!!

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.