I want to insert
into sql database , the items in the array. All I am doing is incrementing the index of array in INSERT
command ( cash[$i+1]
) using for
loop. But it shows an error :
unexpected '+', expected ']'
How can I solve this problem?
for($i = 0; $i < count($cash_list); $i = $i+8) {
$sql = "INSERT INTO Payments VALUES('$cash_list[$i]','$cash_list[$i+1]','$cash_list[$i+2]','$cash_list[$i+3]','$cash_list[$i+4]','$cash_list[$i+5]','$cash_list[$i+6]','$cash_list[$i+7]')";
if (mysqli_query($con, $sql))
echo "yes";
else
echo "Error: " . $sql . "<br>" . $con->error;
}
This is because
$i+1
Doesn't get interpreted as math expression. It's just a string. So you would have to concatenate your strings like this:
$sql = "INSERT INTO Payments
VALUES('" . $cash_list[$i] . "',
'" . $cash_list[$i+1] . "',
'" . $cash_list[$i+2] . "',
'" . $cash_list[$i+3] . "',
'" . $cash_list[$i+4] . "',
'" . $cash_list[$i+5] . "',
'" . $cash_list[$i+6] . "',
'" . $cash_list[$i+7] . "'
)";
Also you might want to take a look into: mysqli_* prepared statements
which is much safer! (Also please take the count($cash_list)
out of your loop condition and put it into a variable and then use the variable in the condition)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.