User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 361,916 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,528 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 4660 | Replies: 66
Reply
Join Date: Feb 2008
Posts: 37
Reputation: asadalim1 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
asadalim1 asadalim1 is offline Offline
Light Poster

Re: inserting checkbox values in mysql +PHP

  #61  
Feb 22nd, 2008
[quote=nav33n;542446]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>";
	
Reply With Quote  
Join Date: Nov 2007
Location: Bangalore, India
Posts: 2,866
Reputation: nav33n has a spectacular aura about nav33n has a spectacular aura about 
Rep Power: 7
Solved Threads: 206
nav33n's Avatar
nav33n nav33n is offline Offline
Posting Maven

Re: inserting checkbox values in mysql +PHP

  #62  
Feb 22nd, 2008
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.
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

*PM asking for help will be ignored*
Reply With Quote  
Join Date: Feb 2008
Posts: 37
Reputation: asadalim1 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
asadalim1 asadalim1 is offline Offline
Light Poster

Re: inserting checkbox values in mysql +PHP

  #63  
Feb 22nd, 2008
1st error, i think something is missing from the foreach loop,


2nd error, the table team is populated? so it must be something else
Reply With Quote  
Join Date: Nov 2007
Location: Bangalore, India
Posts: 2,866
Reputation: nav33n has a spectacular aura about nav33n has a spectacular aura about 
Rep Power: 7
Solved Threads: 206
nav33n's Avatar
nav33n nav33n is offline Offline
Posting Maven

Re: inserting checkbox values in mysql +PHP

  #64  
Feb 22nd, 2008
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.
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

*PM asking for help will be ignored*
Reply With Quote  
Join Date: Feb 2008
Posts: 37
Reputation: asadalim1 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
asadalim1 asadalim1 is offline Offline
Light Poster

Re: inserting checkbox values in mysql +PHP

  #65  
Feb 22nd, 2008
Originally Posted by nav33n View Post
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['team'] as $value){ // this is an aray .

Also line 25 i dnt understand there is no syntax error and the table has records.
Reply With Quote  
Join Date: Nov 2007
Location: Bangalore, India
Posts: 2,866
Reputation: nav33n has a spectacular aura about nav33n has a spectacular aura about 
Rep Power: 7
Solved Threads: 206
nav33n's Avatar
nav33n nav33n is offline Offline
Posting Maven

Re: inserting checkbox values in mysql +PHP

  #66  
Feb 22nd, 2008
print the values of team before foreach.
print_r($_POST['team']); If its an array, it ll print the whole array. else it ll print only 1 value.
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

*PM asking for help will be ignored*
Reply With Quote  
Join Date: Feb 2008
Posts: 37
Reputation: asadalim1 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
asadalim1 asadalim1 is offline Offline
Light Poster

Re: inserting checkbox values in mysql +PHP

  #67  
Feb 25th, 2008
Originally Posted by nav33n View Post
print the values of team before foreach.
print_r($_POST['team']); 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['team'])){ // see if checkbox is checked

2. foreach($_POST['team'] as $value){ // loop trough the checkboxes

3. $sql = "DELETE FROM team WHERE id={$_POST['team']}"; //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.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb PHP Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the PHP Forum

All times are GMT -4. The time now is 10:05 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC