What error exactly ?

here are the errors,
Warning: Invalid argument supplied for foreach() in C:\wamp\www\dynamic\edit.php on line 16

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\wamp\www\dynamic\edit.php on line 25

<form id="form1" name="form1" method="post" action="edit.php">
   
   team: <input type="text" name="team" /><br />
    <br>
        <?php
    $sql = 'SELECT * From team';
    $con = mysql_connect("localhost","root","talk21") or die;
    $result = mysql_db_query(lastr, $sql); 
       while ($rec = mysql_fetch_array($result)) 
    {
       print "\n<input type='checkbox'  name=team[]' value='".$rec['id']."'checked> ".$rec['team']." </input>"; 
 }
 if(isset($_POST['submit'])){
  $q="delete from team"; //delete all the teams
  mysql_query($q);
  foreach($_POST['team'] as $value){
    $q="insert into team (team) values ('$value')";
mysql_query($q); //so by the end, the table will have records which were checked and all the records which were unchecked will be removed.
 }
}
//I list these teams in my form. 
$q="select id,team from table";
$res=mysql_query($q);
echo "<form method='post' action='check.php'>";
while($row=mysql_fetch_array($res)){
  echo "<input type='checkbox' name='team[]' value='$id' checked>".$team."<br />"; //this will list all the teams in the database
}
echo "<input type='submit' name='submit' value='submit'>";
echo "</form>";

1st error means, the value passed to foreach is not an array. Check if you are passing array to foreach loop.
2nd error means, the query is returning empty result.

1st error, i think something is missing from the foreach loop,


2nd error, the table team is populated? so it must be something else

As I said, foreach will give this error if you aren't giving an array as input. And there are so many queries. Check out what's on line 25. That query isn't working.

As I said, foreach will give this error if you aren't giving an array as input. And there are so many queries. Check out what's on line 25. That query isn't working.

I dnt understand team[] is an array on line 11,
foreach($_POST as $value){ // this is an aray .

Also line 25 i dnt understand there is no syntax error and the table has records.

print the values of team before foreach.
print_r($_POST); If its an array, it ll print the whole array. else it ll print only 1 value.

print the values of team before foreach.
print_r($_POST); If its an array, it ll print the whole array. else it ll print only 1 value.

I have simplified the coding to just delete the selected checkbox.

the flow is to check if checkbox is checked and Delete when submit button is click.

1. if(!isset($_POST)){ // see if checkbox is checked

2. foreach($_POST as $value){ // loop trough the checkboxes

3. $sql = "DELETE FROM team WHERE id={$_POST}"; //delete selected teams

<form id="form1" name="form1" method="post" action="edit.php">

        <?php
    $sql = 'SELECT * From team';
    $con = mysql_connect("localhost","root","talk21") or die;
    $result = mysql_db_query(lastr, $sql); 
       while ($rec = mysql_fetch_array($result)) 
    {
      print "\n<input type='checkbox' name=team[]' value='".$rec['id']."' > ".$rec['team']." </input>";     
}
  if(!isset($_POST['team'])){
 }
  foreach($_POST['team'] as $value){
  }
if(isset($_POST['submit'])){
}
  $sql = "DELETE FROM team WHERE id={$_POST['team']}"; //delete selected teams
  mysql_query($q);
  
   echo "<input type='submit' name='submit' value='submit'>";   
?>

There are no errors in the code above, however it doesn’t do anything.:(

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.