Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-03-2013, 12:03 PM   PM User | #1
JGav
New to the CF scene

 
Join Date: Feb 2013
Posts: 1
Thanks: 1
Thanked 0 Times in 0 Posts
JGav is an unknown quantity at this point
Question Need help using PHP arrays

Hello all, I am having awful problems trying to get the following piece of code to work.

PHP Code:
<?php
session_start
();

$con mysql_connect("localhost","root",""); 
if (!
$con

die(
'Could not connect: ' mysql_error()); 


mysql_select_db("roster"$con);  

$row_data = array();
foreach(
$_POST['user'] as $row=>$name

    
$name=mysql_real_escape_string($name); 
    
$mons=mysql_real_escape_string($_POST['MonStart'][$row]); 
    
$monf=mysql_real_escape_string($_POST['MonFinish'][$row]); 
    
$tues=mysql_real_escape_string($_POST['TueStart'][$row]); 
    
$tuef=mysql_real_escape_string($_POST['TueFinish'][$row]); 
    
$weds=mysql_real_escape_string($_POST['WedStart'][$row]); 
    
$wedf=mysql_real_escape_string($_POST['WedFinish'][$row]); 
    
$thurs=mysql_real_escape_string($_POST['ThurStart'][$row]); 
    
$thurf=mysql_real_escape_string($_POST['ThurFinish'][$row]); 
$fris=mysql_real_escape_string($_POST['FriStart'][$row]); 
    
$frif=mysql_real_escape_string($_POST['FriFinish'][$row]); 
    
$sats=mysql_real_escape_string($_POST['SatStart'][$row]); 
    
$satf=mysql_real_escape_string($_POST['SatFinish'][$row]); 
    
$suns=mysql_real_escape_string($_POST['SunStart'][$row]); 
    
$sunf=mysql_real_escape_string($_POST['SunFinish'][$row]); 
    
$week=mysql_real_escape_string($_POST['week'][$row]); 
    
$maker=mysql_real_escape_string($_POST['madeby'][$row]); 
    
$depa=mysql_real_escape_string($_POST['department'][$row]); 
}
mysql_query('SET foreign_key_checks = 0');
$sql=mysql_query("INSERT INTO roster(WeekStart, MonStart, MonFinish, TueStart, TueFinish, WedStart, WedFinish, ThurStart, ThurFinish, FriStart, FriFinish, 
SatStart, SatFinish, SunStart, SunFinish, UserID, ModifiedBy, DepartmentID)
VALUES('$week', '$mons', '$monf', '$tues', '$tuef', '$weds', '$wedf', '$thurs', '$thurf','$fris','$frif','$sats','$satf','$suns','$sunf','$name','$maker','$depa')"
)
or die(
mysql_error());
mysql_query('SET foreign_key_checks = 1');
$result mysql_query($sql); 


// if successfully updated. 
if($result){ 

echo 
"$row successfully inserted.<br/>"
echo 
"<BR>"
echo 
"Update Successful"
echo 
"<BR>"


else 

echo 
"ERROR";


?>
What I am trying to do is insert multiple records into the database using arrays. However only the last record is being entered into the database. Any help would be greatly appreciated!!
JGav is offline   Reply With Quote
Old 02-03-2013, 01:19 PM   PM User | #2
djm0219
Senior Coder

 
djm0219's Avatar
 
Join Date: Aug 2003
Location: Wake Forest, North Carolina
Posts: 1,229
Thanks: 2
Thanked 190 Times in 188 Posts
djm0219 is on a distinguished road
Your SQL insert would need to be within the foreach loop to have each row inserted. Only the last one is getting done now because your insert is outside of the foreach loop.
__________________
Dave .... HostMonster for all of your hosting needs
djm0219 is offline   Reply With Quote
Users who have thanked djm0219 for this post:
JGav (02-03-2013)
Reply

Bookmarks

Tags
arrays, insert, multiple, php, sql

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:04 PM.


Advertisement
Log in to turn off these ads.