PDA

View Full Version : need help with php/mysql foreach loop


MumbleVoice-Jen
22-07-2009, 03:23
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.



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>



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

ConceptLane
23-07-2009, 21:20
Have you tried echo'ing the mysql error after each mysql_query to see what the problem could be?
It sure looks like everything is correct, and if you are sure that the PHP script does see all the POSTed array members (i.e. that debug statement of echo'ing every id and rank is working), then it has to be a mysql issue.
Some key constraints perhaps?

By the way, I'd use

mysql_query("INSERT into employee_shiftbids(Shiftid, Shiftrank, Bidstatus, Employeerank) values('$shiftid', '$shiftrank', 'sent', '0')");