-1

I have 1 variable

$last_id

and 4 Arrays:

$outA Array ( [0] => 1 [1] => 2 [2] => 4 [3] => 5 )
$outB Array ( [0] => 1 [1] => 1 [2] => 2 [3] => 3 )
$sub_min_points Array ( [0] => 700 [1] => 800 [2] => [3] => 500 )
$sub_max_points Array ( [0] => 200 [1] => 400 [2] => [3] => 300)

$stmt = $DBcon->prepare("INSERT INTO `position_grading` 
                                (`position_id`, `grading_sub_id`,
                                `grading_main_id`,sub_min_points,
                                sub_max_points) 
                        VALUES (?,?,?,?,?)");
$stmt->bind_param("sssss",$last_id, $d1, $d2, $sub_min_points, $sub_max_points);

I used this to fetch data from arrays and insert it to database

    $sub_elements = $_POST['sub_elements'];
    $outA = array();
    $outB = array();
    foreach($sub_elements as $value)
    {
        list($x, $y) = explode(":",$value);
        $outA[] = $x;
        $outB[] = $y;
    }

    foreach(array_combine($outA, $outB) AS $d1 => $d2) {
        $d1;
        $d2;
        $sub_min_points =  $sub_min_points[$d1];
        $sub_max_points = $sub_max_points[$d1];
        $stmt->execute();
    }

when I delete these two lines it's working and send data to the database

        $sub_min_points =  $sub_min_points[$d1];
        $sub_max_points = $sub_max_points[$d1];

I want to use foreach to fetch 4 arrays at once this is my problem.

The problem fetch 2 arrays and use $d1 as a key

I tried the solution here looping through multiple arrays and inserting into SQL

it's not working with me.

Thanks

6
  • 4
    What is the error you are actually having? Commented Sep 22, 2017 at 10:07
  • 3
    i's working don't know how to fix this what do u wanna fix when its working? Commented Sep 22, 2017 at 10:09
  • You cannot store a PHP array into a table column as a raw PHP array. Commented Sep 22, 2017 at 10:13
  • 1
    You could try serialize() or json_encode() on the arrays to turn them into a string and store that Commented Sep 22, 2017 at 10:14
  • when I delete the lines i mentioned foreach working and insert the data to database Commented Sep 22, 2017 at 14:09

1 Answer 1

0

This code fixed my problem

    $stmt = $DBcon->prepare("
        INSERT INTO 
              `position_grading` 
                  (`position_id`, `grading_sub_id`, `grading_main_id`,sub_min_points,sub_max_points) 
        VALUES    
              (?,?,?,?,?)");

        foreach($sub_min_points AS $index => $code){
        $code;
        $d1 = $sub_max_points[$index];
        $d2 = $outA[$index];
        $d3 = $outB[$index];
        $stmt->execute();
    }
Sign up to request clarification or add additional context in comments.

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.