Shri Shri - 2 months ago 6
MySQL Question

Concatenate variable into $_POST[]

While working on task I got following requirement.

In a for loop, I have to insert data in db.
I have SQL query in string with double quotes.
I have to concatenate counter variable inside

$_POST[]


for($k=1;$k<=$total_questions;$k++)
{
$sql_insert_survey_question ="insert into survey_questions_options (question_detail,option_1) values ('".$_POST['survey_que\"$k\"']."','".$_POST['survey_que\"$k\"_option1']."')
$res_insert_que1=$obj->insert($sql_insert_survey_question);
}


Please need some ideas.

Answer
for($k=1;$k<=$total_questions;$k++)
 {
    $firstValueToInsert = mysql_real_escape_string($_POST['survey_que'.$k]);
    $secondValueToInsert = mysql_real_escape_string$_POST['survey_que'.$k.'_option1']);
    $sql_insert_survey_question ="insert into survey_questions_options  (question_detail,option_1) values ('$firstValueToInsert','$secondValueToInsert')";
    $res_insert_que1=$obj->insert($sql_insert_survey_question);
}