Connecting Tech Pros Worldwide Help | Site Map

PHP creating checkbox in runtime

Newbie
 
Join Date: Sep 2008
Posts: 3
#1: Nov 2 '08
Hi,

I have a database and i want, when i will retrieve data from that database every row shd have a checkbox at the left of the row, when i select that checkbox, the row will be selected.

How i can populate it in runtime?

Pls help
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,936
#2: Nov 2 '08

re: PHP creating checkbox in runtime


I couldn't make sense of your question. Do you want the row highlighting when the select box is checked? or?

Also, post the code you have tried along with any errors or additional information - anything that will help us.
Newbie
 
Join Date: Sep 2008
Posts: 3
#3: Nov 2 '08

re: PHP creating checkbox in runtime


Quote:

Originally Posted by Markus

I couldn't make sense of your question. Do you want the row highlighting when the select box is checked? or?

Also, post the code you have tried along with any errors or additional information - anything that will help us.


this is the code, it populates checkbox in runtime, but any row or multiple rows dont delete when press the "Delete " button.

pls say, what wrong with the code:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $host="localhost"; // Host name
  3. $username=""; // Mysql username
  4. $password=""; // Mysql password
  5. $db_name="test"; // Database name
  6. $tbl_name="test_mysql"; // Table name
  7.  
  8. // Connect to server and select databse.
  9. mysql_connect("$host", "$username", "$password")or die("cannot connect");
  10. mysql_select_db("$db_name")or die("cannot select DB");
  11.  
  12. $sql="SELECT * FROM $tbl_name";
  13. $result=mysql_query($sql);
  14.  
  15. $count=mysql_num_rows($result);
  16.  
  17. ?>
  18. <table width="400" border="0" cellspacing="1" cellpadding="0">
  19. <tr>
  20. <td><form name="form1" method="post" action="">
  21. <table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
  22. <tr>
  23. <td bgcolor="#FFFFFF">&nbsp;</td>
  24. <td colspan="4" bgcolor="#FFFFFF"><strong>Delete multiple rows in mysql</strong> </td>
  25. </tr>
  26. <tr>
  27. <td align="center" bgcolor="#FFFFFF">#</td>
  28. <td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td>
  29. <td align="center" bgcolor="#FFFFFF"><strong>Name</strong></td>
  30. <td align="center" bgcolor="#FFFFFF"><strong>Lastname</strong></td>
  31. <td align="center" bgcolor="#FFFFFF"><strong>Email</strong></td>
  32. </tr>
  33. <?php
  34. while($rows=mysql_fetch_array($result)){
  35. ?>
  36. <tr>
  37. <td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"></td>
  38. <td bgcolor="#FFFFFF"><? echo $rows['id']; ?></td>
  39. <td bgcolor="#FFFFFF"><? echo $rows['name']; ?></td>
  40. <td bgcolor="#FFFFFF"><? echo $rows['lastname']; ?></td>
  41. <td bgcolor="#FFFFFF"><? echo $rows['email']; ?></td>
  42. </tr>
  43. <?php
  44. }
  45. ?>
  46. <tr>
  47. <td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
  48. </tr>
  49. <?
  50. // Check if delete button active, start this
  51. if($delete){
  52. for($i=0;$i<$count;$i++){
  53. $del_id = $checkbox[$i];
  54. $sql = "DELETE FROM $tbl_name WHERE id='$del_id'";
  55. $result = mysql_query($sql);
  56. }
  57.  
  58. // if successful redirect to delete_multiple.php
  59. if($result){
  60. echo "<meta http-equiv=\"refresh\" content=\"0;URL=delete_multiple.php\">";
  61. }
  62. }
  63. mysql_close();
  64. ?>
  65. </table>
  66. </form>
  67. </td>
  68. </tr>
  69. </table>
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,741
#4: Nov 3 '08

re: PHP creating checkbox in runtime


Hi.

A few things I would like to point out.
  1. Never (I repeat: NEVER!) use any user input in a SQL query without validating that it is in fact what you expect it to be.
    If you were using that code you just posted, I could clear out your table using a simple telnet client and 5 lines of text.
    If you expect the input to be a number, use the is_numeric function to make sure that it is.
    If it should be a string, make sure you run it through mysql_real_escape_string before using it.
    If it is something else... find some way to verify that it is in fact what it is supposed to be.
  2. Always use a LIMIT clause in a DELETE or UPDATE statement where possible.
    This makes sure that if by some unexpected phenomenon (like say, unexpected data!) your WHERE clause fails to limit the query to the rows that should be affected, your entire table won't be lost.
  3. Always use the full PHP tags, rather than the short-tag version.
    That is; use <?php ... ?> rather than <? ... ?>
    The latter is not enabled by default and will cause the PHP code to be sent to the client as plain text when it isn't enabled.
  4. On line 51 of your code, you use a $delete variable in an if statement to see if the DELETE statements should be issued.
    I can not find where this variable is set?
  5. On line 52, you use the $count variable to loop through your checkboxes. This variable was set earlier in the code, using a completely different data set.
    You should rather count the number of checkboxes you actually got and use that number.
  6. On line 53, you seem to use a variable $checkboxes to access the checkboxes sent by the submitted forms.
    This will only work if register_globals is enabled, which it is not by default.
    Relying on this directive is highly discouraged, as it has been deprecated as of PHP5, and will be completely removed in PHP6.
    Use the $_POST and $_GET super-globals instead to fetch the submitted data.
That's all I can think up right now.
Let us know what that turns up.

O, and please use [code] tags when posting your code examples. (See How to ask a question)

[code] ... Code goes here... [/code]

Thank you.
Moderator
Reply