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 423,538 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 4,292 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: 6113 | 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

  #51  
Feb 20th, 2008
Originally Posted by nav33n View Post
As I told you already, If you want to 'update' checkboxes, ie., If you want to delete a checkbox, you have to delete all the checkboxes and then insert only those checkboxes which are checked. I don't know how you can 'add' checkboxes.


I think we are still confused, I don’t want to add a checkbox like that at all.

I have 1 table called team, with 2 columns.

Id team
1 BR
2 GB
3 FR



the only purpose this table serves is to be printed on the form dynamically instead of hard coding the checkboxes.

  1. $result = mysql_db_query(team, $sql);
  2.  
  3. while ($rec = mysql_fetch_array($result))
  4. {
  5. print "\n<input type='checkbox' name=team[]' value='".$rec['id']."' > ".$rec['team']." </input>";



So all I want to do is update what’s on the team table, so the form can dynamically show what’s on the table, I know I can just add the values on phpmyadmin but I want to make a admin page so admin can change what appears on the forms.


id team
1 BR
2 GB
3 FR



cheers naveen
Last edited by peter_budo : Feb 22nd, 2008 at 7:41 am. Reason: Please use [code] tags for even partial code
Reply With Quote  
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,098
Reputation: nav33n has a spectacular aura about nav33n has a spectacular aura about 
Rep Power: 8
Solved Threads: 240
nav33n's Avatar
nav33n nav33n is offline Offline
Posting Sensei

Re: inserting checkbox values in mysql +PHP

  #52  
Feb 20th, 2008
Thats very simple.
1. To add a team: Have a textbox where the user can enter a team and submit it.
2. To update/delete a team, list all the teams as you are doing here. By default, check all the checkboxes. The user can remove a team by clearing the checkbox and click on submit. When the user clicks on submit, remove all the records from team and insert only those which are checked. So, in the end, the table will have only those teams which were checked. I hope this is what you are saying.
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

  #53  
Feb 21st, 2008
Originally Posted by nav33n View Post
Thats very simple.
1. To add a team: Have a textbox where the user can enter a team and submit it.
2. To update/delete a team, list all the teams as you are doing here. By default, check all the checkboxes. The user can remove a team by clearing the checkbox and click on submit. When the user clicks on submit, remove all the records from team and insert only those which are checked. So, in the end, the table will have only those teams which were checked. I hope this is what you are saying.



Ok, i tried this but I have an error on deleteting and updating, when user submits, it should delete all the unset checkboxes.
  1. <form id="form1" name="form1" method="post" action="result.php">
  2.  
  3. team : <input type="text" name="team" /><br />
  4.  
  5. <br>
  6.  
  7. <?php
  8. $sql = 'SELECT * From team';
  9.  
  10. $con = mysql_connect("localhost","root","talk21") or die;
  11. $result = mysql_db_query(lastr, $sql);
  12.  
  13. while ($rec = mysql_fetch_array($result))
  14. {
  15. print "\n<input type='checkbox' name=team[]' value='".$rec['id']."'checked> ".$rec['team']." </input>";
  16. }
  17. if(isset($_POST['submit'])){
  18. }
  19. if(unset($_GET['id'])) {
  20. $q="delete from team where team[]='$id'";
  21. mysql_query($q);
  22. }
  23. ?>
  24. <input type="submit" name="Submit" value="Register" />
  25. </label>
  26. </form>
Last edited by peter_budo : Feb 22nd, 2008 at 7:42 am. Reason: Please use [code] tags
Reply With Quote  
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,098
Reputation: nav33n has a spectacular aura about nav33n has a spectacular aura about 
Rep Power: 8
Solved Threads: 240
nav33n's Avatar
nav33n nav33n is offline Offline
Posting Sensei

Re: inserting checkbox values in mysql +PHP

  #54  
Feb 21st, 2008
That wont work because it wouldn't know what are the unset fields. Why don't u delete all the records and insert only those which are checked ?
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

  #55  
Feb 21st, 2008
Originally Posted by nav33n View Post
Thats very simple.
1. To add a team: Have a textbox where the user can enter a team and submit it.
2. To update/delete a team, list all the teams as you are doing here. By default, check all the checkboxes. The user can remove a team by clearing the checkbox and click on submit. When the user clicks on submit, remove all the records from team and insert only those which are checked. So, in the end, the table will have only those teams which were checked. I hope this is what you are saying.

Originally Posted by nav33n View Post
That wont work because it wouldn't know what are the unset fields. Why don't u delete all the records and insert only those which are checked ?



This is as close as i come to what you have mentioned above, the code is error free but does not do anything. I am looping trough the checkboxes and trying to remove the ckecked checkboxes.

  1. <form id="form1" name="form1" method="post" action="edit.php">
  2.  
  3. team: <input type="text" name="team" /><br />
  4. <br>
  5. <?php
  6. $sql = 'SELECT * From team';
  7. $con = mysql_connect("localhost","root","talk21") or die;
  8. $result = mysql_db_query(lastr, $sql);
  9. while ($rec = mysql_fetch_array($result))
  10. {
  11. print "\n<input type='checkbox' name=team[]' value='".$rec['id']."'checked> ".$rec['team']." </input>";
  12. }
  13. ?>
  14. <?
  15. if(isset($_POST['delete'])){
  16. }
  17. $id = $_GET['id'];
  18. (!isset($_GET['id']))
  19. {
  20. foreach($_GET['team'] as $value)
  21. {
  22.  
  23. $delete = "DELETE FROM team WHERE id='$id' ";
  24. mysql_query($delete);
  25. mysql_close();
  26.  
  27.  
  28. echo "Entry deleted";
  29. ?>
  30. <input type="submit" name="Submit" value="submit" />
  31. </label>
  32. </form>
Last edited by peter_budo : Feb 22nd, 2008 at 7:43 am. Reason: Please use [code] tags
Reply With Quote  
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,098
Reputation: nav33n has a spectacular aura about nav33n has a spectacular aura about 
Rep Power: 8
Solved Threads: 240
nav33n's Avatar
nav33n nav33n is offline Offline
Posting Sensei

Re: inserting checkbox values in mysql +PHP

  #56  
Feb 21st, 2008
No. I didn't mean this. Well, Imagine I have 5 records in my table which resemble the checkboxes like above.
id | team
1 | team1
2 | team2
3 | team3
4 | team4
5 | team5
  1. if(isset($_POST['submit'])){
  2. $q="delete from team"; //delete all the teams
  3. mysql_query($q);
  4. foreach($_POST['team'] as $value){
  5. $q="insert into table (team) values ('$value')";
  6. 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.
  7. }
  8. }
  9. //I list these teams in my form.
  10. $q="select id,team from table";
  11. $res=mysql_query($q);
  12. echo "<form method='post' action='check.php'>";
  13. while($row=mysql_fetch_array($res)){
  14. echo "<input type='checkbox' name='team[]' value='$id' checked>".$team."<br />"; //this will list all the teams in the database
  15. }
  16. echo "<input type='submit' name='submit' value='submit'>";
  17. echo "</form>";
I dont think I can think more than that.
Last edited by nav33n : Feb 21st, 2008 at 11:55 am.
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

  #57  
Feb 22nd, 2008
There are errors in the code,

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



  1. <form id="form1" name="form1" method="post" action="edit.php">
  2.  
  3. team: <input type="text" name="team" /><br />
  4. <br>
  5. <?php
  6. $sql = 'SELECT * From team';
  7. $con = mysql_connect("localhost","root","talk21") or die;
  8. $result = mysql_db_query(lastr, $sql);
  9. while ($rec = mysql_fetch_array($result))
  10. {
  11. print "\n<input type='checkbox' name=team[]' value='".$rec['id']."'checked> ".$rec['team']." </input>";
  12. }
  13. if(isset($_POST['submit'])){
  14. $q="delete from team"; //delete all the teams
  15. mysql_query($q);
  16. foreach($_POST['team'] as $value){
  17. $q="insert into team (team) values ('$value')";
  18. 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.
  19. }
  20. }
  21. //I list these teams in my form.
  22. $q="select id,team from table";
  23. $res=mysql_query($q);
  24. echo "<form method='post' action='check.php'>";
  25. while($row=mysql_fetch_array($res)){
  26. echo "<input type='checkbox' name='team[]' value='$id' checked>".$team."<br />"; //this will list all the teams in the database
  27. }
  28. echo "<input type='submit' name='submit' value='submit'>";
  29. echo "</form>";

i tried even diffrent ways, to delete,
  1. if(isset($_POST['delete'])){
  2. }
  3. $id = $_GET['id'];
  4. (!isset($_GET['id']))
  5. foreach($_GET['team'] as $value)
  6. {
  7. $team = $_REQUEST['team']; //'countries' is the name of the combobox
  8. for ($i=0; $i<sizeof($team); $i++)
  9. {
  10. if (isset($team[$i]))
  11. {
  12. $delete = "DELETE FROM team WHERE id='$id' ";
  13. mysql_query($delete);
  14. mysql_close();

Isnt there an easir way to delete a checkbox??
Last edited by peter_budo : Feb 22nd, 2008 at 7:44 am. Reason: Please use [code] tags
Reply With Quote  
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,098
Reputation: nav33n has a spectacular aura about nav33n has a spectacular aura about 
Rep Power: 8
Solved Threads: 240
nav33n's Avatar
nav33n nav33n is offline Offline
Posting Sensei

Re: inserting checkbox values in mysql +PHP

  #58  
Feb 22nd, 2008
Isnt there an easir way to delete a checkbox??
Deleting the checkbox which is checked or deleting the checkbox which isn't checked ? I am not sure about the second case, but deleting records when checked works just like how you do while inserting.
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

  #59  
Feb 22nd, 2008
For Now, all i want to do is delete a checkbox, in mysql
$delete = "DELETE FROM team WHERE id='$id' ";
 mysql_query($delete);
 mysql_close();


this code u looks good but has has errors.
  1. form id="form1" name="form1" method="post" action="edit.php">
  2.  
  3. team: <input type="text" name="team" /><br />
  4. <br>
  5. <?php
  6. $sql = 'SELECT * From team';
  7. $con = mysql_connect("localhost","root","talk21") or die;
  8. $result = mysql_db_query(lastr, $sql);
  9. while ($rec = mysql_fetch_array($result))
  10. {
  11. print "\n<input type='checkbox' name=team[]' value='".$rec['id']."'checked> ".$rec['team']." </input>";
  12. }
  13. if(isset($_POST['submit'])){
  14. $q="delete from team"; //delete all the teams
  15. mysql_query($q);
  16. foreach($_POST['team'] as $value){
  17. $q="insert into team (team) values ('$value')";
  18. 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.
  19. }
  20. }
  21. //I list these teams in my form.
  22. $q="select id,team from table";
  23. $res=mysql_query($q);
  24. echo "<form method='post' action='check.php'>";
  25. while($row=mysql_fetch_array($res)){
  26. echo "<input type='checkbox' name='team[]' value='$id' checked>".$team."<br />"; //this will list all the teams in the database
  27. }
  28. echo "<input type='submit' name='submit' value='submit'>";
  29. echo "</form>";
Last edited by peter_budo : Feb 22nd, 2008 at 8:00 am. Reason: Please use [code] tags
Reply With Quote  
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,098
Reputation: nav33n has a spectacular aura about nav33n has a spectacular aura about 
Rep Power: 8
Solved Threads: 240
nav33n's Avatar
nav33n nav33n is offline Offline
Posting Sensei

Re: inserting checkbox values in mysql +PHP

  #60  
Feb 22nd, 2008
What error exactly ?
Dude, list all the teams. Let the user select the checkboxes and then click on delete. Go back to page 1 and check the example I have given. Do the same, but use delete query. And instead of passing the team name as value, pass the id.
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  
Reply

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

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

 

Thread Tools Display Modes

Similar Threads
Other Threads in the PHP Forum

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