ROHIT JHA ROHIT JHA - 5 months ago 11
JSON Question

PHP code not inserting data into MySQL database properly

This code is showing data correctly but not inserting data

<?php
$servername = "";
$username = "";
$password = "";
$dbname = "";


It is showing data correctly but not inserting data

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$cnt = 0;
$rows = array('146615679', '8', 'Vendor 8', 'NULL', 'NULL', 'NULL', 'SriLanka', 'NULL', 'NULL', 'NULL', 'arunmehra991@gmail.com', 'N', 'N', '0000-00-00 00:00:00', '0','1446615279', '8', 'Vendor 8', 'NULL', 'NULL', 'NULL', 'SriLanka', 'NULL', 'NULL', 'NULL', 'arunmehra991@gmail.com', 'N', 'N', '0000-00-00 00:00:00', '0');
$insertQry = "insert into retail_store_vendor1(Store_Id, Vend_Id, Vend_Nm, Vend_cntct_Nm, Add_1, City, Ctr_Desc, Zip, Tele, Mobile, Email,Vend_Inv, Active) values ";
foreach($rows as $row) {
if($cnt == 15) {
$insertQry = rtrim($insertQry ,", ");
$insertQry .= "), ";
}
if($cnt > 14) {
$cnt = 0;
}
if($cnt == 0) {
$insertQry .= "(";
}

$insertQry .= $row.", ";
$cnt++;
}
$insertQry = rtrim($insertQry ,", ");
$insertQry .= ");";

$response=$insertQry;
echo json_encode($response);

Answer
<?php

$row= array('John',123, 'Lloyds Office','Jane',124, 'Lloyds Office','Billy',125, 'London Office','Miranda', 126, 'Bristol Office');
$arraychuk = array_chunk($row, 3);

foreach ($arraychuk as $array_num => $array) {
  echo "Array $array_num:\n";
 }


?>

//you can easily chunk like which will break into a perfect array