将变量连接到$ _POST []

时间:2014-02-20 06:17:19

标签: php mysql string post

在完成任务时,我得到了以下要求。

在for循环中,我必须在db中插入数据。 我在带有双引号的字符串中进行SQL查询。 我必须在$_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);
  }

请提出一些想法。

6 个答案:

答案 0 :(得分:0)

$_POST['name'].$k

你可以在php

中连接这样的变量

答案 1 :(得分:0)

像这样编辑:

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

答案 2 :(得分:0)

怎么样:

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

答案 3 :(得分:0)

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

答案 4 :(得分:0)

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

答案 5 :(得分:0)

这将有助于您动态插入n个产品..

    $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')");
    }