Adding multiple $_Posts from form into MYSQL DB - Using a loop?!

Hi,

The following code adds extra rows of text boxes to add further data.
But im having issue with looping through them and adding them into my database.

Is my mothod correct as im getting the error "Invalid argument supplied for foreach() "

below is my code

    <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.0.3.js"></script>
<?php
include "../include/db.php";
if (!empty($_POST['ok'])) {
    
    if (!empty($_POST['Loop'])) {
        $i = 0;
        echo "hi";
        foreach ($_POST['Loop'] as $r) {
            //add to database
            
            $dateFormated = split('/', $_POST['theDate' . $i]);
            $Date         = $dateFormated[2] . '-' . $dateFormated[1] . '-' . $dateFormated[0];
            // check if name only contains letters and whitespace
            
            $Sport = test_input($_POST["Sport" . $i]);
            // check if name only contains letters and whitespace
            $Pick  = test_input($_POST["Pick" . $i]);
            
            $Bookie = test_input($_POST["Bookie" . $i]);
            // check if name only contains letters and whitespace
            
            if (strpos($_POST["Odds" . $i], '/') !== false) {
                $Odds = round(fraction(test_input($_POST["Odds" . $i])), 2);
            } else {
                $Odds = test_input($_POST["Odds" . $i]);
            }
            // check if name only contains letters and whitespace
            $BackorLay = test_input($_POST["BackorLay" . $i]);
            // check if name only contains letters and whitespace
            
            $Stake = floatval(test_input($_POST["Stake" . $i]));
            // check if name only contains letters and whitespace
            
            
            
            $Com = test_input($_POST["Com" . $i]);
            // check if name only contains letters and whitespace
            
            
            $Outcome = test_input($_POST["Outcome" . $i]);
            // check if name only contains letters and whitespace
            
            
            
            $Reduction = test_input($_POST["Reduction" . $i]);
            // check if name only contains letters and whitespace
            
            
            
            $PlaceDiv = test_input($_POST["PlaceDiv" . $i]);
            // check if name only contains letters and whitespace
            if ($_POST['test' . $i] == 'EW') {
                $ew = "yes";
            } else {
                $ew = "no";
            }
            $i = $i + 1;
            
            $sql = "
INSERT INTO `Bets` 
( `Date`, `Sport`, `Pick`, `Bookie`, `Odds`, `BackorLay`, `Stake`, `ew`, `Com`, `Result`, `Reduction`, `PlaceDiv`)
VALUES
('$Date', '$Sport', '$Pick', '$Bookie', '$Odds', '$BackorLay', '$Stake', '$ew', '$Com', '$Outcome', '$Reduction', '$PlaceDiv')";
            
            $res = mysqli_query($db, $sql);
            if (!$res) {
                echo PHP_EOL . "FAIL: $sql";
                trigger_error(mysqli_error($db), E_USER_ERROR);
            }
            
            
        }
    }
}
?>
<div style="width:90%;margin:auto;">
    <h1>Add Bets</h1>
    <form method="post">
    <center>
<p>
    <input type="hidden" name="Loop" value="loop">
        
            <input id="theDate0" size="10" value="<?
echo date(" d/m/Y ");
?>" name="theDate0" type="text">
       

            <select id="Sport0" name="Sport0">
                <option>Horse Racing</option>
                <option>Football</option>
                <option>Greyhounds</option>
                <option>NFL</option>
            </select>
       

            <input id="Pick0" name="Pick0" type="text">
       


            <select id="Bookie0" name="Bookie0">
                <option>Bet365</option>
                <option>Betfred</option>
                <option>BetVictor</option>
                <option>Boylesports</option>
                <option>Bwin</option>
                <option>Centrebet</option>
                <option>Coral</option>
                <option>Ladbrokes</option>
                <option>Paddy Power</option>
                <option>Pinnacle Sports</option>
                <option>SBOBET</option>
                <option>Sky Bet</option>
                <option>Stan James</option>
                <option>unibet</option>
                <option>William Hill</option>
            </select>
      

            <input id="Odds0" name="Odds0" size="3" type="text">
       

            <select id="BackorLay0" name="BackorLay0">
                <option>Back</option>
                <option>Lay</option>
            </select>
       

            <input id="Stake0" name="Stake0" size="3" type="text">E/W<input name="EW0" ID="EW0" value="EW" type="checkbox" />
     

            <select id="Com0" name="Com0">
                <option>0</option>
                <option>1</option>
                <option>2</option>
                <option>3</option>
                <option>4</option>
                <option>5</option>
            </select>
    

            <select id="Outcome0" name="Outcome0">
                <option>Unknown</option>
                <option>Win</option>
                <option>Loss</option>
                <option>P</option>
                <option>E/W Win</option>
                <option>Void</option>
            </select>
       

            <select id="PlaceDiv0" name="PlaceDiv0">
                <option>5</option>
                <option>4</option>
            </select>
     
     <input onclick="addRow(this.form);" type="button" value="Add row" /> </p>

<div id="itemRows">
    </div>            <p><input type="submit" name="ok" value="Save Changes"></p>
        </center>    
    </form>
</div>
<script type="text/javascript">
var rowNum = 0;
function addRow(frm) {
    rowNum ++;

    var row = '<p id="rowNum'+rowNum+'"><input id="theDate'+rowNum+'" size="10" value="<?
echo date(" d/m/Y ");
?>" name="theDate'+rowNum+'" type="text"><select id="Sport'+rowNum+'" name="Sport'+rowNum+'"><option>Horse Racing</option><option>Football</option><option>Greyhounds</option><option>NFL</option></select><input id="Pick'+rowNum+'" name="Pick'+rowNum+'" type="text"></td><td><select id="Bookie'+rowNum+'" name="Bookie'+rowNum+'"><option>Bet365</option><option>Betfred</option><option>BetVictor</option><option>Boylesports</option><option>Bwin</option><option>Centrebet</option><option>Coral</option><option>Ladbrokes</option><option>Paddy Power</option><option>Pinnacle Sports</option><option>SBOBET</option><option>Sky Bet</option><option>Stan James</option><option>unibet</option><option>William Hill</option></select><input id="Odds'+rowNum+'" name="Odds'+rowNum+'" size="3" type="text"><select id="BackorLay'+rowNum+'" name="BackorLay'+rowNum+'"><option>Back</option><option>Lay</option></select><input id="Stake'+rowNum+'" name="Stake'+rowNum+'" size="3" type="text">E/W<input name="EW'+rowNum+'" ID="EW'+rowNum+'" value="EW" type="checkbox" /><select id="Com'+rowNum+'" name="Com'+rowNum+'"><option>0</option><option>1</option><option>2</option><option>3</option><option>4</option><option>5</option></select><select id="Outcome'+rowNum+'" name="Outcome'+rowNum+'"><option>Unknown</option><option>Win</option><option>Loss</option><option>P</option><option>E/W Win</option><option>Void</option></select><select id="PlaceDiv'+rowNum+'" name="PlaceDiv'+rowNum+'"><option>5</option><option>4</option></select><input type="button" value="Remove" onclick="removeRow('+rowNum+');"></p>';

    
    jQuery('#itemRows').append(row);
    frm.add_qty.value = '';
    frm.add_name.value = '';
}
function removeRow(rnum) {
    jQuery('#rowNum'+rnum).remove();
}
</script>
</body>    
</html>

Open in new window

runnerjp2005Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Slick812Commented:
greetings  runnerjp2005, , your "foreach" line as -
         foreach ($_POST['Loop'] as $r) {
is NOT appropriate for the  input you have as -
         <input type="hidden" name="Loop" value="loop">

and cannot work, also you never use the $r variable in that foreach loop, You should just remove that foreach loop

it looks like many of your code should work without it.

ask questions if you need more information.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.