php


foreach loop only entering one record


If I just echo out $reasons after filling in 2 required text fields then it shows the input values for both. But if I try and insert into the database, it only inserts one record and not the second.
function insert_lost_reasons($link){
if (isset($_POST['submit']) && isset($_SESSION['last_id'])) {
$message = "";
foreach($_POST['name'] as $reasons) {
if (empty($reasons)) {
$message = "Please fill in all fields";
}
}
if ($message) {
echo error_message($message);
} else {
foreach($_POST['name'] as $reasons) {
$stmt = $link->prepare("INSERT INTO `lost_time` (`ps_id`, `reason`) SELECT ?, `lost_time_reason_id` FROM `lost_time_reasons` WHERE `lost_time_desc` = ?");
$stmt->bind_param("is", $_SESSION['last_id'], $reasons);
$stmt->execute();
$stmt->close();
unset($_SESSION['token']);
header("location: dash.php");
exit();
}
}
}
}
May be you should write your foreach like this
else {
foreach($_POST['name'] as $reasons) {
$stmt = $link->prepare("INSERT INTO `lost_time` (`ps_id`, `reason`) SELECT ?, `lost_time_reason_id` FROM `lost_time_reasons` WHERE `lost_time_desc` = ?");
$stmt->bind_param("is", $_SESSION['last_id'], $reasons);
$stmt->execute();
}
$stmt->close();
unset($_SESSION['token']);
header("location: dash.php");
exit();
}
You are redirecting in first iteration.
The problem is that you close session and exit on first foreach loop. Wait all loops and then jump with headeerfunction.
header("location: dash.php");
exit();
Do this instead:
foreach($_POST['name'] as $reasons) {
//Edit this for your usage
$stmt = $link->prepare("INSERT INTO `lost_time` (`ps_id`, `reason`) SELECT ?, `lost_time_reason_id` FROM `lost_time_reasons` WHERE `lost_time_desc` = ?");
$stmt->bind_param("is", $_SESSION['last_id'], $reasons);
$stmt->execute();
}
$stmt->close();
unset($_SESSION['token']);
header("location: dash.php");
exit();

Related Links

How to give a custom name to the PDF being downloaded using FPDF library?
DNS with subdomain doesn't work properly
SMPP: the UDH (User Data Header) appears in the body of sms
Compare 2 arrays and retrieve the differences using PHP
wordpress making a static and dynamic link
for each if true fetch mysqli result
PHP, CodeIgniter, MySQL - SELECT - overloading database
How do I enable member login to a specific directory including sub-directories in wordpress
Uploading mp4 files using PHP
Move files in php with move_uploaded_file [closed]
Count Items in Array PHP
Radio button with same id in Laravel 4.1.x
php mail() to multiple recipients - dont share email adresses
Dynamic page text from dropdown using MS SQL, AJAX, PHP and JSON
Related Products SQL Query
how to solve Non Static method in php 5.5 yii framework

Categories

HOME
php
oracle
spring-data
webview
windows-8.1
barcode-scanner
selenium-builder
dry
filesize
ontology
implicit-conversion
sympy
specifications
wine
parsley.js
documentum
webstore
complexity-theory
squid
mahout-recommender
async-await
exe
adminlte
angular2-highcharts
fido-u2f
altium-designer
qa
hevc
iwebbrowser2
monitor
db2-luw
amazon-kms
grails-2.5
keyboard-layout
landscape
django-smart-selects
selenide
assemblies
best-buy-api
grails-spring-security
contactless-smartcard
spooler
apache-toree
destroy
axis
intellij-lombok-plugin
return-type
libreoffice-writer
photoswipe
nonlinear-functions
head.js
sharpdx
gestures
iptv
gnome-terminal
nrf51
visual-studio-monaco
coovachilli
declarative
eclemma
yii2-model
wicked-pdf
codecharge
jemdoc
teamviewer
miniconda
bjam
vdm-sl
relational-model
abstract-factory
subfolder
htmltidy
createfile
nhibernate-criteria
lifetime
n-tier-architecture
dnsjava
window-resize
object-code
xcode6.3.1
category-theory
rpg
network-printers
jini
hyperloglog
mod-perl
typesafe-stack
google-code-prettify
ember-table
sunstudio
axacropdf
icenium
server-name
glx
fork-join
kernel32
jquery-1.4
directshow.net
printqueue
pyjamas
ios-4.2
wmd-editor
fuzzy-comparison
database-cloning
version-control-migration
server-load

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App