4

I want to INSERT a set of data submitted by $_POST (in form of array) as

foreach($_POST['data'] as $single) {
$set[]="('static', '$single')";
}
$sets=implode(", ", $set);
mysql_query("INSERT INTO table (Static, Data) VALUES $sets");

Where is the best place to use use mysql_real_escape_string to avoid SQL injection, as the data are submitted by users.

2 Answers 2

10

Before going to your first foreach.

$_POST['data'] = array_map("mysql_real_escape_string", $_POST['data']);
Sign up to request clarification or add additional context in comments.

Comments

-1

Try with this:

foreach($_POST['data'] as $single)
{
    $set[]="('static', mysql_real_escape_string($single))";
}
$sets = implode(", ", $set);
mysql_query("INSERT INTO `table` (`Static`, `Data`) VALUES ".$sets."");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.