-5
$array = array(); 
foreach ($Addproduct_image as $row) { 
$array[] = '("' . mysql_real_escape_string($row["image"]) . '", ' . $row["color"] . ' , ' . $last_ID .')'; } 
$stmt1 = $this->con->prepare("INSERT INTO Product_img_col (image, color, pro_ID) VALUES '.implode(",", $array[])"); 
if ($stmt1->execute()) {return true;} else{return false;}

// Data to add.
{"name":"world","color":"world",($Default_Value)}]

I want to add this data into multiple rows. I think I will have to use implode function for multiple rows with a default value for third column "$default-value". I am not very good in PHP can anyone please help .

5
  • What have you tried ? Commented Aug 6, 2018 at 8:45
  • $array = array(); foreach ($Addproduct_image as $row) { $array[] = '("' . mysql_real_escape_string($row["image"]) . '", ' . $row["color"] . ' , ' . $last_ID .')'; } $stmt1 = $this->con->prepare("INSERT INTO Product_img_col (image, color, pro_ID) VALUES '.implode(",", $array[])"); print_r($array); if ($stmt1->execute()) {return true;} else{return false;} I tried this Commented Aug 6, 2018 at 8:49
  • 1
    @jerry Refering to your ealier comment on an awnser; we are not here to do your work. We simply help if it aint working or see how to fix it, but its not we do your work. Commented Aug 6, 2018 at 8:49
  • @Dorvalla Let me Re-edit my question I tried something which was not working. Commented Aug 6, 2018 at 8:55
  • Possible duplicate of Parse JSON string contents into PHP Array Commented Aug 6, 2018 at 8:56

3 Answers 3

0

As you are trying to add all items to the query you will need to remove the [] from the implode(",", $array[]) so it reads implode(",", $array).

You could try adding echo "INSERT INTO Product_img_col (image, color, pro_ID) VALUES '.implode(",", $array[])"); to your page so you have the query that you can test in phpMyAdmin or similar. This should help you debug.

Also if color is a string you should really put it inside quotes as you have done with image.

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

Comments

-1
  • Use json_decode to get a php array from your JSON.

  • for each element in the array use json_encode to turn it into JSON again, and then insert that as a single row

1 Comment

I need code as well. I am not very good in PHP.
-1
$connect = mysqli_connect("localhost", "root", "", "test");
$query = '';
$filename = "employee_data.json";
$data = file_get_contents($filename); //Read the JSON file in PHP
$array = json_decode($data, true); //Convert JSON String into PHP Array
foreach($array as $row) //Extract the Array Values by using Foreach Loop
{
      $query .= "INSERT INTO tbl_employee(name, gender, designation) VALUES ('".$row["name"]."', '".$row["gender"]."', '".$row["designation"]."'); ";  // Make Multiple Insert Query 
}

 if(mysqli_multi_query($connect, $query)) //Run Mutliple Insert Query
    {
         echo 'Imported JSON Data';
    }

JSON_File - employee_data.json

[  
       {  
         "name": "Michael Bruce",  
         "gender": "Male",  
         "designation": "System Architect"  
       },  
       {  
         "name": "Jennifer Winters",  
         "gender": "Female",  
         "designation": "Senior Programmer"  
       },  
       {  
         "name": "Donna Fox",  
         "gender": "Female",  
         "designation": "Office Manager"  
       }

     ]

For more details see the example in the below URL, It may help you

https://www.webslesson.info/2016/04/get-multiple-json-data-insert-into-mysql-database-in-php.html

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.