Solved

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

Posted on 2014-11-26
3
105 Views
Last Modified: 2015-01-02
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

0
Comment
Question by:runnerjp2005
3 Comments
 
LVL 33

Accepted Solution

by:
Slick812 earned 500 total points
ID: 40467247
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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now