I have a function in PHP mySQL that inserts records into a database. It take in an array of decoded JSON data, a field name, a connection and a DB name. It then builds a SQL statement and inserts the records one by one, or at least thats what I think it should do. I run everything, make the connection, database and table but when it gets to this function, it fails. I feel it has to do with my INSERT statement but I am not sure how to fix it. Any help would be greatly appreciated.
Function:
function insertRecords($array,$fieldname, $conn, $db_NAME)
{
mysqli_select_db($conn, $db_NAME);
$sql_insert = "INSERT INTO `tbl_articles` (`" . $fieldname . "`) VALUES ('" . $records . "')";
foreach ($array as $records)
{
if(mysqli_query($conn, $sql_insert))
{
echo "Records Inserted.";
}
else
{
die('Error : ' . mysqli_error($conn) . "<br>");
}
}
echo $sql_insert . "<br>";
}
$recordswhen you create your INSERT, before you define it.