Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Feb 2013
    Posts
    1
    Thanks
    1
    Thanked 0 Times in 0 Posts

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

  • #2
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,294
    Thanks
    4
    Thanked 203 Times in 200 Posts
    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

  • Users who have thanked djm0219 for this post:

    JGav (02-03-2013)


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •