-1

I have 2 arrays (as an example but I've 8) to insert the record to MySQL database. But I'm confused about how to insert them. Please guide me about.

$bunit = $_POST['bunit'];
$prodID = $_POST['productID'];
$section = $_POST['section'];
$remarks = array();
$remarksType = array();



INSERT INTO `remarks` (`remarks_id`, `remarks`,`bunit`, `prodID`,  `remarks_section_ID`, `remarks_type`, `remarks_defectID`, `remarks_productID`, `remarks_active`) VALUES (NULL, '$remarks', '$bunit', '$prodID', '$section', '$remarksType', '1', '2', 1);"
sgeorgiou
  • 59
  • 10
  • 1
    Where are you stuck? – nice_dev Apr 02 '19 at 13:31
  • I'm stuck at inserting the data to mysql, $bunit, $prodID & $section will be same while $remarks and $remarkType are arrays and they will be dynamic and separate. how would i write the foreach statement to insert each entry to mysql – Muhammad Usman Apr 02 '19 at 19:14

1 Answers1

-1

Hey before insert you have do one thing encode array to json_encode() and insert to db when retrieve from db simple use json_decode()

$bunit = $_POST['bunit'];
$prodID = $_POST['productID'];
$section = $_POST['section'];
$remarks = json_encode(array('id'=>1 ,'data'=>'remarks')) ;
$remarksType = json_encode(array('id'=>1 ,'data'=>'remarksType'));
$sql = "INSERT INTO `remarks` (`remarks_id`, `remarks`,`bunit`, `prodID`,  `remarks_section_ID`, `remarks_type`, `remarks_defectID`, `remarks_productID`, `remarks_active`) VALUES (NULL, '$remarks', '$bunit', '$prodID', '$section', '$remarksType', '1', '2', 1);"
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149