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

Behat change result status when add javascript annotation
NSS certificate nickname is unknown to PHP run by Apache
Getting Actual URL From Facebook Insight Post level Exports
Unable to delete with checkboxes and update codeignitrer
Image Corrupt or Truncated on DIV Background
Problems integrating PayPal IPN in PHP
Selecting Domestic will show Domestic Tour and International will show International
fopen: failed to open stream: Permission denied in PHP on Mac [duplicate]
Order and sort result mysql or using php array funciton
PhpStorm isset function not working?
dynamic table header from mongodb results in yii/php
Woocommerce display multiple error messages/notifications
How to get the relation in laravel
FPDF Error, Fatal error: Maximum function nesting level of '100' reached, aborting
CodeIgniter Parent Declare
Customize static wordpress pages correctly

Categories

HOME
office365
visual-studio
webview
protocol-buffers
xcode
yaml
clearcase
whmcs
android-emulator
qooxdoo
elk-stack
supercollider
cname
orange-api
phonegap-build
cloudsim
multiple-tables
infrared
pfobject
generator
mime-types
web-config-transform
python-appium
marquee
cube
sales
bitbake
openbr
credit-card
progid
lxml
serversocket
ejml
opal-framework
word-cloud
code-push
music21
tinyioc
roracle
knockout-2.0
password-hash
entropy
datediff
watir-webdriver
libharu
dcount
nsrunloop
spring-mongo
unsigned
mnesia
i2b2
gdl
outlook.com
textpattern
directx-9
blank-line
search-form
domino-designer-eclipse
swagger-maven-plugin
unidata
erlog
shoes
kotlin-android-extensions
operands
squeezebox
miglayout
sql-view
aero
spiral
tfs-sdk
cyrillic
live-wallpaper
dropshadow
navigationcontroller
quartz-core
groovy-console
rtd
qcar-sdk
active-record-query
explicit
ccnet-config
bucket
server-load
rendering-engine
rootkit

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