I created a for loop that loops through an array or words exploded from a string. The for loop works and has been tested by echo statements. When running this code it only inserts 1 record into the database rather than the number of strings in the array.
$item=explode(" ", $items);
for ($i = 0; $i < count($item); ++$i) {
// Create connection
$conn = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "INSERT INTO invlog (itemid, qty)
VALUES ('".$item[$i]."', '-1')";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
}
$itemscontain? you should post it in the question too. if one of those words are quoted, then your query will be screwed. you should bind them instead.