Results 1 to 12 of 12
  1. #1
    Join Date
    Feb 2002
    Location
    New York
    Posts
    791

    need help with php/mysql foreach loop

    Hello,

    I am having a problem I am hoping someone can help. I have used the below code before and had it working with no issues. However my issue right now is the below foreach loop is only inserting the first record into the mysql table and not all the values in the array. I have confirmed the two array has more then one value in them. Yes only the virst value from each array is being interested into mysql.


    PHP Code:
    here is my form 

    <form method='POST' action='process.php'
    shift <input type='hidden' name='shiftid[]' value='$shiftid'><br /> 
    Your Rank <select name='shiftrank[]'><option value='1'>1</option><option value='2'>2</option
    PHP Code:
    foreach($_POST["shiftid"] AS $key => $val) {  
         
    $shiftid $val
         
    $shiftrank $_POST['shiftrank'][$key];      
           echo 
    "shiftid is $shiftid and rank is $shiftrank<br />"
         
    //$updatesql = mysql_query("INSERT into employee_shiftbids SET Shiftid='$shiftid', Shiftrank='$shiftrank', Bidstatus='sent', Employeerank='0'");                 
         

    Also I know I should be using mysql_escape_string() on my $_POST data I took it out during trouble shooting so no need to point out about sql injection etc. So what is wrong with ym foreach loop ??
    Thanks all for youre time and assistance
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    just a programmer

  2. #2
    Join Date
    Jan 2004
    Location
    NJ, USA
    Posts
    288
    Quote Originally Posted by sessionmedia View Post
    Hello,

    I am having a problem I am hoping someone can help. I have used the below code before and had it working with no issues. However my issue right now is the below foreach loop is only inserting the first record into the mysql table and not all the values in the array. I have confirmed the two array has more then one value in them. Yes only the virst value from each array is being interested into mysql.


    PHP Code:
    here is my form 

    <form method='POST' action='process.php'
    shift <input type='hidden' name='shiftid[]' value='$shiftid'><br /> 
    Your Rank <select name='shiftrank[]'><option value='1'>1</option><option value='2'>2</option
    PHP Code:
    foreach($_POST["shiftid"] AS $key => $val) {  
         
    $shiftid $val
         
    $shiftrank $_POST['shiftrank'][$key];      
           echo 
    "shiftid is $shiftid and rank is $shiftrank<br />"
         
    //$updatesql = mysql_query("INSERT into employee_shiftbids SET Shiftid='$shiftid', Shiftrank='$shiftrank', Bidstatus='sent', Employeerank='0'");                 
         

    Also I know I should be using mysql_escape_string() on my $_POST data I took it out during trouble shooting so no need to point out about sql injection etc. So what is wrong with ym foreach loop ??
    Thanks all for youre time and assistance
    What do you get when you do this:

    PHP Code:
    var_dump($_POST); 

  3. #3
    Your INSERT statement seems to be wrong, and resembles an UPDATE query instead Try this:

    PHP Code:
    $updatesql mysql_query("INSERT into employee_shiftbids (Shiftid, Shiftrank, Bidstatus, Employeerank) VALUES('$shiftid', '$shiftrank', 'sent', '0')"); 

  4. #4
    Join Date
    Feb 2003
    Location
    AR
    Posts
    2,381
    Quote Originally Posted by JulesR View Post
    Your INSERT statement seems to be wrong, and resembles an UPDATE query instead Try this:

    PHP Code:
    $updatesql mysql_query("INSERT into employee_shiftbids (Shiftid, Shiftrank, Bidstatus, Employeerank) VALUES('$shiftid', '$shiftrank', 'sent', '0')"); 
    Not done much MySQL work have you? The syntax used by the original poster is perfectly valid and is my preferred way to write insert queries as well - That way they transform easily into UPDATE queries.

  5. #5
    Quote Originally Posted by kcackler View Post
    Not done much MySQL work have you? The syntax used by the original poster is perfectly valid and is my preferred way to write insert queries as well - That way they transform easily into UPDATE queries.
    I've done more MySQL work than you could possibly imagine. The syntax I posted is the "correct" way, as written within the MySQL documentation itself. Perhaps you should read it?

    EDIT: Whilst your way does indeed work, it's more akin to UPDATE statements.

  6. #6
    Join Date
    Feb 2003
    Location
    AR
    Posts
    2,381
    No need to get your panties in a twist...What you stated originally was incorrect and I was just making sure that the original poster realized his syntax was in no way wrong.

    Just because his syntax was not part of the original SQL specification does not make it incorrect. Many people prefer the alternative syntax because it doesn't leave you counting columns and values in your query to make sure everything matches up.

    Different strokes for different folks, I guess.

    PS, JulesR - It's pretty good practice to just go ahead and escape all of your table and column names...You never know what's going to turn into a MySQL reserved word in the future

    /out
    Last edited by ThatScriptGuy; 07-22-2009 at 01:23 AM.

  7. #7
    Quote Originally Posted by kcackler View Post
    No need to get your panties in a twist...What you stated originally was incorrect and I was just making sure that the original poster realized his way was not necessarily incorrect.
    My panties aren't in a twist, you're the one who began the personal attacks, am I not allowed to defend myself? It's incorrect in the way that it's not correct SQL syntax. This is exactly why pretty much every MySQL tutorial and INSERT documentation you'll see online doesn't use the SET= syntax, because it's an extension instead. Yes, it's usable, yes it works, but it's most certainly not the preferred way to insert data.

    Just because his syntax was not part of the original SQL specification does not make it incorrect. Many people prefer the alternative syntax because it doesn't leave you counting columns and values in your query to make sure everything matches up.
    Granted, but it's a bad practice to get into.

    Different strokes for different folks, I guess.
    Agreed.

    PS, JulesR - It's pretty good practice to just go ahead and escape all of your table and column names...You never know what's going to turn into a MySQL reserved word in the future
    Sorry, but it's amusing you telling me about bad practice whilst not using correct SQL syntax

    Fair point, though.

  8. #8
    Join Date
    Feb 2003
    Location
    AR
    Posts
    2,381
    My final reply here - If it's listed in the OFFICIAL MySQL documentation as a valid syntax, I fail to see the error using said syntax. Since the INSERT...SET syntax is listed as valid in the MySQL documentation, I'm going to go ahead and keep on using it, no matter how impure of a developer it makes me. And if I ever need to port any of my applications to a DB server that doesn't support that syntax, I'll just go ahead and update the 3 lines in my database wrapper that it would take to accommodate that fact, and be done with it.

  9. #9
    That's your prerogative, but the issue here really isn't your own personal usage, it's the advocation of your methods when they have significant limitations over the correct SQL syntax. Anyone reading this thread should be aware of the differences in syntax, and why one is hugely and publically preferred instead of another.

    Whatever works for you I guess.

  10. #10
    Join Date
    Jun 2009
    Posts
    35
    You can give foreach loop in form as:

    PHP Code:
    foreach ($shiftid as $id) {
    echo 
    '
     <input type=\'hidden\' name=\'shiftid[]\' value='
    .$id.'>';

    Hope it helps.

  11. #11
    Join Date
    Feb 2002
    Location
    New York
    Posts
    791

    my var_dump()

    Thank you all.

    My internet has been down so I was not able to get back her as fast as I liked. Anyway yes the query in my OP was an edit I made based on what I was told via a reply for this same post I made on devshed.com however yes I normally don't write my insert queries using the update format.

    I have done a var_dump() and get this. Which tells me the duplicate 0 key values is the issue but how do I fix it ?? I will review everyones post and see if the answer is here.
    I just wanted to respond first

    PHP Code:
    array(4) { ["shiftid"]=> array(25) { [0]=> string(2"60" [1]=> string(2"61" [2]=> string(2"62" [3]=> string(2"63" [4]=> string(2"64" [5]=> string(2"65" [6]=> string(2"66" [7]=> string(2"67" [8]=> string(2"68" [9]=> string(2"69" [10]=> string(2"70" [11]=> string(2"71" [12]=> string(2"72" [13]=> string(2"73" [14]=> string(2"74" [15]=> string(2"75" [16]=> string(2"76" [17]=> string(2"77" [18]=> string(2"78" [19]=> string(2"79" [20]=> string(2"80" [21]=> string(2"81" [22]=> string(2"82" [23]=> string(2"83" [24]=> string(2"84" } ["shiftdesc"]=> array(25) { [0]=> string(9"1700-0130" [1]=> string(9"1600-0030" [2]=> string(9"1400-2230" [3]=> string(34"Sun: 1000-1830, Mon-Thu: 1330-2200" [4]=> string(9"0830-1700" [5]=> string(9"0600-1430" [6]=> string(9"1800-0230" [7]=> string(9"0800-1630" [8]=> string(9"2200-0630" [9]=> string(34"Tue-Fri: 1530-0000, Sat: 1800-0230" [10]=> string(9"1730-0200" [11]=> string(9"1700-0130" [12]=> string(9"1430-2300" [13]=> string(9"1400-2230" [14]=> string(9"1400-2230" [15]=> string(9"1200-2245" [16]=> string(9"1200-2245" [17]=> string(9"1100-2145" [18]=> string(9"0600-1430" [19]=> string(9"0600-1645" [20]=> string(9"0900-1730" [21]=> string(34"Tue-Fri: 1330-2200, Sat: 1000-1830" [22]=> string(9"1730-0200" [23]=> string(9"2200-0630" [24]=> string(9"1100-2145" } ["shiftrank"]=> array(25) { [0]=> string(1"1" [1]=> string(1"2" [2]=> string(1"3" [3]=> string(1"4" [4]=> string(1"5" [5]=> string(1"6" [6]=> string(1"7" [7]=> string(1"8" [8]=> string(1"9" [9]=> string(2"10" [10]=> string(2"11" [11]=> string(2"12" [12]=> string(2"13" [13]=> string(2"14" [14]=> string(2"15" [15]=> string(2"16" [16]=> string(2"17" [17]=> string(2"18" [18]=> string(2"19" [19]=> string(2"20" [20]=> string(2"21" [21]=> string(2"22" [22]=> string(2"23" [23]=> string(2"24" [24]=> string(2"25" } ["submit"]=> string(6"submit" Duplicate entry '0' for key 2 
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    just a programmer

  12. #12
    Join Date
    Feb 2002
    Location
    New York
    Posts
    791
    Thank you for your suggestion Neeseema MM

    However still does not work. All these replies and just to complain about what format my sql is in. I am surprised no one has volunteered the same amount of energy into helping me with a solution to my issue.

    so here is my query (changed to make the hard core coders happy)
    PHP Code:
    foreach($_POST['shiftid'] AS $key => $val) { 
         
    $shiftid $val;
         
    $shiftrank $_POST['shiftrank'][$key];     
        
    $updatesql mysql_query("INSERT into employee_shiftbids(Groupwiseid, Shiftid, Shiftrank, Bidstatus, Employeerank) VALUES('$groupwiseid','$shiftid','$shiftid','sent','0')") or die(mysql_error());                
         } 
    here is the full html form I know it is long but to sum it up it is getting a list of all schedules from a database (the unique id associated with each schedule, schedule description, specific days, and then is allowing the end user to assign a number 1-25 or 1-28 (based on who the user is) to each specific schedule
    PHP Code:

    $row_color 
    "#C1E0FF";
    //$color1 = "#C1E0FF";  
    //$color2 = "#CFCFCF"; 
    //$row_count = 1;
    echo "<form method='POST' action='process_shiftbid.php'>";
    echo 
    "<table cellspacing='1' cellpadding='3' border='0' ><tr><td background='../images/title_repeater.jpg'><font size='2'>Shift</font></td><td background='../images/title_repeater.jpg'><font size='2'>Sun</font></td>";
    echo 
    "<td background='../images/title_repeater.jpg'><font size='2'>Mon</font></td><td background='../images/title_repeater.jpg'><font size='2'>Tues</font></td><td background='../images/title_repeater.jpg'><font size='2'>Wed</font></td>";
    echo 
    "<td background='../images/title_repeater.jpg'><font size='2'>Thur</font></td><td background='../images/title_repeater.jpg'><font size='2'>Fri</font></td><td background='../images/title_repeater.jpg'><font size='2'>Sat</font></td>";
    echo 
    "<td background='../images/title_repeater.jpg'><font size='2'>Shift Ranking</font></td></tr>";

    $getshifts mysql_query("Select * from shiftbids where Roleid='$RoleID' and Shiftenabled='yes' order by Shiftid asc");
    $numofshifts mysql_num_rows($getshifts);
    if(
    $numofshifts =='0'){
    echo 
    "<tr><td colspan='11'><br /><br /><center>Their Are No Open Shifts To Bid On</center><br /><br /></td></tr>";
    }
    while(
    $shiftrow mysql_fetch_array($getshifts))
    {
    extract($shiftrow);
    //$row_color = ($row_count % 2) ? $color1 : $color2; 
    echo "<tr bgcolor='$row_color'><td><input type='hidden' name='shiftid[]' value='$Shiftid'><input type='hidden' name='shiftdesc[]' value='$Shiftdesc'><font size='2'>$Shiftdesc</font></td><td>";
    if(
    $Sun=='yes'){
    echo 
    "<img src='../images/check.jpg'>";
    }else{
    echo 
    "Off";
    }
    echo 
    "</td><td>";
    if
    (
    $Mon =='yes'){
    echo 
    "<img src='../images/check.jpg'>";
    }else{
    echo 
    "Off";
    }
    echo 
    "</td><td>";
    if(
    $Tue =='yes'){
    echo 
    "<img src='../images/check.jpg'>";
    }else{
    echo 
    "Off";
    }
    echo 
    "</td><td>";
    if(
    $Wed =='yes'){
    echo 
    "<img src='../images/check.jpg'>";
    }else{
    echo 
    "Off";
    }
    echo 
    "</td><td>";
    if(
    $Thur =='yes'){
    echo 
    "<img src='../images/check.jpg'>";
    }else{
    echo 
    "Off";
    }
    echo 
    "</td><td>";
    if(
    $Fri =='yes'){
    echo 
    "<img src='../images/check.jpg'>";
    }else{
    echo 
    "Off";
    }
    echo 
    "</td><td>";
    if(
    $Sat =='yes'){
    echo 
    "<img src='../images/check.jpg'>";
    }else{
    echo 
    "Off";
    }
    echo 
    "</td><td><select name='shiftrank[]'><option></option>";
    //if leads
    if($RoleID=='3'){
    echo 
    "<option value='1'>1</option><option value='2'>2</option><option value='3'>3</option><option value='4'>4</option><option value='5'>5</option>";
    echo 
    "<option value='6'>6</option><option value='7'>7</option><option value='8'>8</option><option value='9'>9</option><option value='10'>10</option><option value='11'>11</option>";
    echo 
    "<option value='12'>12</option><option value='13'>13</option><option value='14'>14</option><option value='15'>15</option><option value='16'>16</option><option value='17'>17</option>";
    echo 
    "<option value='18'>18</option><option value='19'>19</option><option value='20'>20</option><option value='21'>21</option><option value='22'>22</option>";
    echo 
    "<option value='23'>23</option><option value='24'>24</option><option value='25'>25</option><option value='26'>26</option><option value='27'>27</option><option value='28'>28</option>";
    }if(
    $RoleID=='2'){
    //if supervisor
    echo "<option value='1'>1</option><option value='2'>2</option><option value='3'>3</option><option value='4'>4</option><option value='5'>5</option>";
    echo 
    "<option value='6'>6</option><option value='7'>7</option><option value='8'>8</option><option value='9'>9</option><option value='10'>10</option><option value='11'>11</option>";
    echo 
    "<option value='12'>12</option><option value='13'>13</option><option value='14'>14</option><option value='15'>15</option><option value='16'>16</option><option value='17'>17</option>";
    echo 
    "<option value='18'>18</option><option value='19'>19</option><option value='20'>20</option><option value='21'>21</option><option value='22'>22</option>";
    echo 
    "<option value='23'>23</option><option value='24'>24</option><option value='25'>25</option>";
    }
    echo 
    "</select></td></tr>";
    //$row_count++; 
    }
    echo 
    "</table><br /><br /><center><input type='submit' name='submit' value='submit'></center></form>"
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    just a programmer

Similar Threads

  1. Using Foreach/Loop on a query
    By acctman in forum Programming Discussion
    Replies: 5
    Last Post: 09-02-2008, 11:05 AM
  2. Problem with foreach loop
    By P-nut in forum Programming Discussion
    Replies: 4
    Last Post: 08-09-2007, 09:43 PM
  3. using a foreach() loop
    By mjfroggy in forum Programming Discussion
    Replies: 8
    Last Post: 06-23-2007, 01:24 PM
  4. need help omtimizing a php mysql while loop
    By mikey1090 in forum Programming Discussion
    Replies: 12
    Last Post: 08-30-2006, 07:52 AM
  5. Running a loop inside of another loop?
    By i.make.sense in forum Programming Discussion
    Replies: 12
    Last Post: 05-02-2006, 02:28 PM

Posting Permissions

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