CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   PHP (http://www.codingforums.com/forumdisplay.php?f=6)
-   -   Need help using PHP arrays (http://www.codingforums.com/showthread.php?t=286955)

JGav 02-03-2013 12:03 PM

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!!

djm0219 02-03-2013 01:19 PM

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.


All times are GMT +1. The time now is 10:28 AM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.