i have recived some data via post method i wrote the data to array now how i can write da data to mysql table ? sample of data recive is shown below. i keep getting this error:Error, query failed
if (isset($_POST['submit'])) {
$data = [];
foreach($_POST['checkbox'] as $rowNum) {
$data[] = explode("::", $_POST['opt'][$rowNum]);
}
var_dump($data);
$sku =$data[$rowNum][0];
$description =$data[$rowNum][1];
$location =$data[$rowNum][2];
$quantitydate =$data[$rowNum][3];
$link = mysqli_connect("somesite", "****", "*******", "******");
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$query ="SELECT * FROM Test WHERE sku = '$sku'";
$testResult = mysqli_query($link, $query) or die('Error, query failed');
if(mysqli_fetch_array($testResult) == NULL){
$sql = "INSERT INTO test (ID, sku, description, location, quantitydate) VALUES ('$ID','$sku', '$description','$location', '$quantitydate',NOW())";
if(mysqli_query($link, $sql)){
echo "Records added successfully.<br /><br />";
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
// close connection
mysqli_close($link);
}else
{
echo "Record Already Exist<br /><br />";
}
}
output sample:
// Output sample (selected row 2 and 4):
array (size=2)
0 =>
array (size=4)
0 => string 'SKU2' (length=4)
1 => string 'DESC2' (length=5)
2 => string 'LOC2' (length=4)
3 => string 'QUAN2' (length=5)
1 =>
array (size=4)
0 => string 'SKU4' (length=4)
1 => string 'DESC4' (length=5)
2 => string 'LOC4' (length=4)
3 => string 'QUAN4' (length=5)
$dataand do anINSERT INTO TABLEquery with the values from each row.die('Error, query failed')todie(mysqli_error($link))so you see the reason for the failure.$quantitydateandNOW()in theINSERTquery? You have more values than columns you're assigning to.$IDvariable? Is that supposed to be an auto-increment column? You can just leave it out of theINSERTand it will be incremented automatically.