0

i have a not difficult question, but I couldn't find an answer.

I have a multidimensional array for insert data with one query. The result is this:

INSERT INTO table (field1, field2, field3, field4) values ('1', '2', '3'),('1', '2', '3'),('1', '2', '3'),('1', '2', '3')

and I want to add one value in each tuple like this:

INSERT INTO table (field1, field2, field3, field4) values ('1', '2', '3','10'),('1', '2', '3','10'),('1', '2', '3','10'),('1', '2', '3','10')

This is the code (I also tried with array_push inside foreach):

$DataArr = array();
    for($i=1;$i<5;$i++){
        $fieldVal1 = 1;
        $fieldVal2 = 2;
        $fieldVal3 = 3;
 
        $DataArr[] = "('$fieldVal1', '$fieldVal2', '$fieldVal3')";
    }
$id=10;
$new=array();

foreach ($DataArr as $value) {
    $value[4]="'$id'";
}

$sql = "INSERT INTO table (field1, field2, field3) values ";
    $sql .= implode(',', $DataArr);
print_r($sql);

I tested this code but it doesn't work, can anyone help me?

3
  • Simple php function. Use array_push($your_variable) Commented Jan 23, 2021 at 12:01
  • @RobinSingh I have try with array_push($value,"'$id'"); but don't work Commented Jan 23, 2021 at 12:05
  • Please show your array structure Commented Jan 23, 2021 at 12:07

2 Answers 2

1
Here is the easy solution

    $DataArr = array();
    for($i=1;$i<5;$i++){
        $fieldVal1 = 1;
        $fieldVal2 = 2;
        $fieldVal3 = 3;
        $DataArr[] = "('$fieldVal1', '$fieldVal2', '$fieldVal3')";
    }
    $id=10;
    $new=array();
    foreach ($DataArr as $key =>$value) {
        $tmpVal = ltrim($value,"(");
        $tmpVal = rtrim($tmpVal,")");
    
        $tmpArray = explode(',',$tmpVal);
        array_push($tmpArray,'4');
    
        array_push($new, "(".implode(',',$tmpArray).")");
         
    }
    
    
    $sql = "INSERT INTO table (field1, field2, field3, field4) values ";
    $sql .= implode(',', $new);
    print_r($sql);
Sign up to request clarification or add additional context in comments.

Comments

0

You'd want to set the imploded data to a variable to insert and then bind the param.

$DataArr = array();
for($i = 0; $i < 4; $i++) {
  $DataArr[$i] = "1,2,3";
}

$field1_arr = !empty($DataArr[0]) ? implode(",", $DataArr[0]) : "";
$field2_arr .= !empty($DataArr[1]) ? implode(",", $DataArr[1]) : "";
$field3_arr .= !empty($DataArr[2]) ? implode(",", $DataArr[2]) : "";
$field4_arr .= !empty($DataArr[3]) ? implode(",", $DataArr[3]) : "";;

$sql = $connection->prepare("INSERT INTO table (field1, field2, field3, field4) VALUES (?,?,?,?)");
$sql->bind_param("ssss", $field1_arr, $field2_arr, $field3_arr, $field4_arr);
if($sql->execute()) {
  // It worked
}
$sql->close();

1 Comment

basically I want to insert an array with a single insert into rather than doing a lot of queries with a foreach. I thought about creating this this dataarr which contains 3 of the 4 values, but I have to add the last attribute and do the insert into with mysqli.

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.