Shri
Shri

Reputation: 731

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.

Upvotes: 1

Views: 9065

Answers (6)

Manish Patil
Manish Patil

Reputation: 1

this will help u in making dynamic insertion of n number of product ..

    $loopLength=$_POST['ProudctSize'];

            for ($i=1; $i <=$loopLength; $i++) 
          {
            $productid=$_POST['P'.$i];
            $quatity=$_POST['Q'.$i];
            $rate=$_POST['R'.$i];

           $Insert=mysql_query("INSERT INTO saledisplay ( `saleid`, `productid`, `quantity`, `rate`) 
            VALUES('$oid','$productid','$quatity','$rate')");
    }

Upvotes: 0

Yogesh
Yogesh

Reputation: 924

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);
}

Upvotes: 1

Sriniwas
Sriniwas

Reputation: 515

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

Upvotes: 0

Drixson Ose&#241;a
Drixson Ose&#241;a

Reputation: 3641

How about :

for($k=1;$k<=$total_questions;$k++)
{
  //store them in a temp variable , so it's easy for me to read
  $value1 = $_POST['survey_que' . $k];
  $value2 = $_POST['survey_que' . $k . '_option1'];

  $sql_insert_survey_question ="insert into survey_questions_options  (question_detail,option_1) values ('$value1' , '$value2')";

  $res_insert_que1=$obj->insert($sql_insert_survey_question);
}

Upvotes: 0

Sherin Jose
Sherin Jose

Reputation: 2526

Edit like this:

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

Upvotes: 0

Vikas Gautam
Vikas Gautam

Reputation: 978

$_POST['name'].$k

you can concatenate variable like this in php

Upvotes: 1

Related Questions