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 397,165 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 3,449 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

inserting checkbox values in mysql +PHP

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: 238
nav33n's Avatar
nav33n nav33n is offline Offline
Posting Sensei

Re: inserting checkbox values in mysql +PHP

  #39  
Feb 15th, 2008
  1. <?php
  2. $conn=mysql_connect("localhost","root");
  3. mysql_select_db("test");
  4. $operation=$_POST['operation'];
  5. $id=$_POST['id'];
  6. if(isset($_POST['submit'])) {
  7. $team=implode(",",$_POST['team']);
  8. $name=$_POST['name'];
  9. $q="insert into table1 (name, team) values ('$name','$team')";
  10. mysql_query($q);
  11. }
  12. if($operation=="delete"){
  13. $q="delete from table1 where counter='$id'";
  14. mysql_query($q);
  15. }
  16. if(isset($_POST['update'])){
  17. $name=$_POST['name'];
  18. $team=implode(",",$_POST['team']);
  19. $q="update table1 set name='$name', team='$team' where counter='$id'";
  20. mysql_query($q);
  21. }
  22. ?>
  23. <html>
  24. <body>
  25. <form method="post" name="insertform" action="checkbox1.php">
  26. Enter name: <input type="text" name="name" /><br />
  27. <input type="checkbox" name="team[]" value="AG">Argentina <br />
  28. <input type="checkbox" name="team[]" value="BR">Brazil <br />
  29. <input type="checkbox" name="team[]" value="GE">Germany <br />
  30. <input type="checkbox" name="team[]" value="EN">England <br />
  31. <input type="submit" name="submit" value="submit">
  32. </form>
  33. <hr>
  34. <form method="post" name="editform" action="checkbox1.php">
  35. <input type="hidden" name="id">
  36. <input type="hidden" name="operation">
  37. <?php
  38. $q="select counter,name from table1";
  39. $result=mysql_query($q);
  40. while($row=mysql_fetch_array($result)){
  41. $name=$row['name'];
  42. $id=$row['counter'];
  43. echo $name ."<input type=\"button\" name=\"edit\" value=\"edit\" onclick=\"javascript: document.editform.operation.value='edit';document.editform.id.value='$id';document.editform.submit();\"><input type=\"button\" name=\"delete\" value=\"Delete\" onclick=\"javascript: document.editform.operation.value='delete';document.editform.id.value='$id'; document.editform.submit();\"><br />";
  44. }
  45. ?>
  46. </form>
  47. <hr>
  48. <?php
  49. if($operation=="edit") {
  50. $id=$_POST['id'];
  51. ?>
  52. <form method="post" name="updateform" action="checkbox1.php">
  53. <input type="hidden" name="id" value="<?php echo $id; ?>">
  54. <?php
  55. $q="select * from table1 where counter='$id'";
  56. $result=mysql_query($q);
  57. $row=mysql_fetch_array($result);
  58. $name=$row['name'];
  59. $team=$row['team'];
  60. $teamarray=explode(",",$team);
  61. echo "<input type=\"text\" name=\"name\" value=\"$name\"><br />";
  62. ?>
  63. <input type="checkbox" name="team[]" value="AG" <?php if(in_array("AG",$teamarray)){ echo "checked"; } ?>>Argentina <br />
  64. <input type="checkbox" name="team[]" value="BR" <?php if(in_array("BR",$teamarray)){ echo "checked"; } ?>>Brazil <br />
  65. <input type="checkbox" name="team[]" value="GE" <?php if(in_array("GE",$teamarray)){ echo "checked"; } ?>>Germany <br />
  66. <input type="checkbox" name="team[]" value="EN" <?php if(in_array("EN",$teamarray)){ echo "checked"; } ?>>England <br />
  67. <input type="submit" name="update" value="update">
  68. <?php
  69. }
  70. ?>
  71. </body>
  72. </html>
Well, Here is the code. The code is lil confusing (and untidy) but I am in no mood to write any better code. I am doing everything in 1 page. You can insert, edit and delete a record. Hope this will fix your problem.

Cheers,
Naveen
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  
All times are GMT -4. The time now is 3:50 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC