942,790 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 727
  • PHP RSS
Jun 12th, 2009
0

Help php+mysql+checkbox=Massive Headache

Expand Post »
Hello
can someone please help me, I am trying to add information to a database with checkbox. I have every thing working expect for the checkbox. I am fairly new to the php/sql side of web coding. Anyway I want to add information to the one field of a database, in this case fruit. When I click submit it show the information as array, but I want it to be displayed as the value, eg banana, apple not as Array.

PHP Syntax (Toggle Plain Text)
  1. <form action="script/test.php" method="post">
  2. <table>
  3. <TD><input type="checkbox" name="fruit[]" id='fruit[]' value='banana'>
  4. banana</TD>
  5. <TD><input type="checkbox" name="fruit[]" id='fruit[]' value='apple'>
  6. Apple</TD>
  7. </tr>
  8. <tr>
  9. <TD></TD>
  10. <TD><input type="checkbox" name="fruit[]" id='fruit[]' value='pine'>
  11. Pineapple</TD>
  12. <TD><input type="checkbox" name="fruit[]" id='fruit[]' value='other'> Other</TD>
  13. <tr></tr>
  14. <tr>
  15. </TABLE></form


Php code
php Syntax (Toggle Plain Text)
  1. <?php
  2. $con = mysql_connect("localhost","root","");
  3. if (!$con)
  4. {
  5. die('Could not connect: ' . mysql_error());
  6. }
  7.  
  8. mysql_select_db("test", $con);
  9.  
  10. $sql="INSERT INTO food (Fruit)
  11. VALUES
  12. ('$_POST[fruit]')";
  13.  
  14. if (!mysql_query($sql,$con))
  15. {
  16. die('Error: ' . mysql_error());
  17. }
  18. echo "information has been added";
  19.  
  20. mysql_close($con)
  21. ?>
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mccs is offline Offline
2 posts
since Jun 2009
Jun 12th, 2009
0

Re: Help php+mysql+checkbox=Massive Headache

hi

you need to use implode() php function for convert string
for e.g
PHP Syntax (Toggle Plain Text)
  1. $fruits=$_POST[fruit];
  2. $fruits_str=implode(',',$fruits);//it seprates value by comma
  3. $fruits_str=implode(' ',$fruits);//it seprates value by space
I hope this function help you to solve problem
Thanks
Reputation Points: 13
Solved Threads: 15
Junior Poster in Training
Tulsa is offline Offline
77 posts
since May 2009
Jun 12th, 2009
0

Re: Help php+mysql+checkbox=Massive Headache

Welcome mccs.

This is your first post and it is very impressive.

You missed Submit Button and also "fruits" is an array and its elements should be written into a table.

PHP Syntax (Toggle Plain Text)
  1. <?php
  2. $cmd=$_REQUEST["cmd"];
  3. // If submit button is pressed
  4. if(isset($cmd)) {
  5. $con = mysql_connect("localhost","root","");
  6. if (!$con) {
  7. die('Could not connect: ' . mysql_error());
  8. }
  9.  
  10. mysql_select_db("test", $con);
  11. $fruit=$_REQUEST["fruit"]; // array of checked box values
  12.  
  13. foreach($fruit as $i) {
  14. $sql="INSERT INTO food (Fruit) VALUES ('$i')";
  15. if (!mysql_query($sql,$con)) {
  16. die('Error: ' . mysql_error());
  17. }
  18. }
  19. echo "information has been added";
  20. mysql_close($con);
  21. }
  22. ?>
  23.  
  24. <form action="script/test.php" method="post">
  25. <table>
  26. <tr>
  27. <td>
  28. <input type="checkbox" name="fruit[]" id='fruit[]' value='banana'>
  29. banana
  30. </td>
  31. <tr>
  32. <td><input type="checkbox" name="fruit[]" id='fruit[]' value='apple'>
  33. Apple</td>
  34. </tr>
  35. <tr>
  36. <td></td>
  37. <td><input type="checkbox" name="fruit[]" id='fruit[]' value='pine'>
  38. Pineapple</td>
  39. <td><input type="checkbox" name="fruit[]" id='fruit[]' value='other'> Other</td>
  40. <tr></tr>
  41. <tr>
  42. <tr>
  43. <td>
  44. <input type="submit" name="cmd" value="Submit"/>
  45. </td>
  46. </tr>
  47. </table>
  48. </form>
Moderator
Reputation Points: 2134
Solved Threads: 1226
Posting Genius
adatapost is offline Offline
6,523 posts
since Oct 2008
Jun 12th, 2009
0

Re: Help php+mysql+checkbox=Massive Headache

Thanks for your help guys really appreciate the help and fast reply.

sorry for the messy post, it was driving me crazy, and I need to get away from the computer, I accidentally left out the submit button, due to having the checkbox half way in my code.

but thanks again for the help.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mccs is offline Offline
2 posts
since Jun 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Joomla basics?
Next Thread in PHP Forum Timeline: Working with an IDX feed





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC