Solved

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

Posted on 2014-11-26
3
103 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
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

744 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

17 Experts available now in Live!

Get 1:1 Help Now