1

Edited: In my application I want to store two array value in a two column with a single query. I am stuck here.Last two column value insert is my problem.I tried many ways but it's does not work exactly what i want. New db design

in database that question id is same only other two things r different. My array values goes to the last column of database. I am unable to do that.

from here data goes

<form method="post" action="questionmadddb.php">
<input name='questionid' value='<?php echo $questionid?> 
<?php
if(isset($_POST['num'])){
    $num=$_POST['num'];
    for ($i=1; $i <=$num ; $i++) { 
        echo "<tr>
        <td><label>OPTION " . $i . "</label></td>
        <td><input class='form-control' name='txtfllname[]'></td>
        <td><input class='form-control' name='txtflrname[]'></td>
        </tr>";
    }
}
?>
<td colspan=6><button class="btn btn-success btn-lg btn-block" type="submit" >ADD QUESTION</button></td>

here is query

<?php

if(isset($_POST["txtfllname"]) && is_array($_POST["txtfllname"])){ 
$capture_field_vals ="";
foreach($_POST["txtfllname"] as $key => $text_field){
    $a=("INSERT INTO qflinfo (questionid,qfllid,qfllname,qflrid,qflrname,status) VALUES ('$questionid','$i','$text_field',0)");
    $i++;
    //echo $a;
    mysqli_query($conn,$a) or die(mysqli_error($conn));
}

}?>

how to get last two column data and insert?please help me...How can I do that?

3
  • can you post the complete log error rather, with line number ? Commented Feb 23, 2018 at 12:17
  • 1
    please change name of either $value=array(); or foreach ($_POST['txtoptname'] as $value) { , you have used $value twice Commented Feb 23, 2018 at 12:17
  • please check my edited code and question. Commented Feb 23, 2018 at 18:03

2 Answers 2

1
    <?php

if(isset($_POST["txtfllname"]) && is_array($_POST["txtfllname"])){ 
$capture_field_vals ="";

foreach($_POST["txtfllname"] as $key => $text_field){

    if($key>=count($_POST["txtfllname"])-2)
    {
        $a=("INSERT INTO qflinfo (questionid,qfllid,qfllname,qflrid,qflrname,status) VALUES ('$questionid','$i','$text_field',0)");
        $i++;

        mysqli_query($conn,$a) or die(mysqli_error($conn));
    }

}

}?>
Sign up to request clarification or add additional context in comments.

3 Comments

@ArpanSarkar "Fatal error: Uncaught Error: [] operator not supported for strings" this error resolved as per your question.
this is my edited question.tell me how to insert txtflrname.
<?php if(isset($_POST["txtfllname"]) && is_array($_POST["txtfllname"])){ $capture_field_vals =""; foreach($_POST["txtfllname"] as $key => $text_field){ if($key>=count($_POST["txtfllname"])-2) { $a=("INSERT INTO qflinfo (questionid,qfllid,qfllname,qflrid,qflrname,status) VALUES ('$questionid','$i','$text_field',0)"); $i++; mysqli_query($conn,$a) or die(mysqli_error($conn)); } } }?>
0

You redeclare $value in your for-loop. Rename either $value=array(); or $_POST['txtoptname'] as $value

Comments

Your Answer

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