0

Im sorry if this is a stupid question but ive been stuck on this for hours, the closest answer I got was from this post but the issue is not resolved.

Im getting data from checkboxes from a simple form. The forms action takes user to next page, I get the values from checkboxes using

$event = $_Post['matchId'];
var_dump($event) confirms values has been received correctly(see image)

enter image description here

The value of $event is an array so im doing the following

foreach($event as $key => $eventId)
//some code

All is working in the forloop however as soon as I click submit the isset() function gets trigered I get error:

Undefined Index: matchId

Invalid Argument for forloop

Any idea what I am doing wrong here Please let me know if you require more info

update on requested isset code

    $event= $_POST['matchId'];
var_dump($event);
foreach($event as $key => $eventId){
$sql = "Select * FROM events WHERE event_id = $eventId";
$result = mysql_query($sql);

$i=0;//counter
while($row=mysql_fetch_array($result)){

    $team1 = $row['team1'];
    $team2 = $row['team2'];
    $id[$i]= $row['event_id']; 

    echo'<h3>'.$team1.' VS '.$team2.'</h3>';
    echo'<select name="id[]">';
            echo'<option value="'.$row['team1'].'">'.$team1.'</option>';
            echo'<option value="'.$row['team2'].'">'.$team2.'</option>';
            echo'</select>';    

            echo'By <select name="score[]">';
            echo'<option value="1">1</option>';
            echo'<option value="2">2</option>';
            echo'<option value="3">3</option>';
            echo'<option value="4">4</option>';
            echo'<option value="5">5</option>';
            echo'<option value="6">6</option>';
            echo'<option value="7">7</option>';
            echo'<option value="8">8</option>';
            echo'<option value="9">9</option>';
            echo'<option value="10">10</option>';
            echo'<option value="11">11</option>';
            echo'<option value="12">12</option>';
            echo'<option value="13">13</option>';
            echo'<option value="14">14</option>';
            echo'<option value="15">15</option>';
            echo'<option value="16">16</option>';
            echo'<option value="17">17</option>';
            echo'<option value="18">18</option>';
            echo'<option value="19">19</option>';
            echo'<option value="20">20</option>';
            echo'</select>';    
        $i++;
}//while
}//for
echo'</legend></fieldset>';
echo'<input type="submit" class="buttono" value="Submit" name="submit" />';
echo'</form>';
echo'</div><!--news-->';
if(isset($_POST['submit'])){    
$x=0;
foreach($_POST['id'] as $key => $winner){
    $score = $_POST['score'][$key];
    var_dump($score);
    $winnerb = $id[$key];
    echo($winner."-".$winnerb);
    $sql="INSERT INTO picks(member_nr,event_id,pick,score)
            VALUES('$userId','$winnerb','$winner','$score')";
            mysql_query($sql) or trigger_error(mysql_error()." in ".$sql);
}//for
}//end isset
13
  • sorry typo.....should read id Commented Apr 22, 2015 at 8:36
  • 4
    You should add more of the codes. Commented Apr 22, 2015 at 8:37
  • 2
    where is that isset? Commented Apr 22, 2015 at 8:38
  • @yergo please see updated Commented Apr 22, 2015 at 8:40
  • 1
    You should make up your mind, switching back and forth between id and matchId you are just wasting everybody's time. Commented Apr 22, 2015 at 8:53

2 Answers 2

0

The error is on this part -

$event= $_POST['matchId'];
var_dump($event);
foreach($event as $key => $eventId){
$sql = "Select * FROM events WHERE event_id = '$event'";
$result = mysql_query($sql);
.....

$_POST['matchId'] is not present. You should add a check for this like -

if (!empty($_POST['matchId'])) { //fetch & display data }
else { //display some error or something like this }

$event event is empty in this case as $_POST['matchId'] is missing. So it is giving the error regarding foreach.

Sign up to request clarification or add additional context in comments.

Comments

0

If no checkbox is set, $_POST['id'] will not be set so that will lead to that warning.

Also note that you cannot rely on the $key value unless you explicitly set that value of the key in the name attribute of your input element. The image of your var_dump() suggests that you are not doing that.

You also have an sql injection problem. You should switch to PDO or mysqli and use a prepared statement with bound variables.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.