473,385 Members | 1,487 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

PHP creating checkbox in runtime

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
Nov 2 '08 #1
3 2559
Markus
6,050 Expert 4TB
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.
Nov 2 '08 #2
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>
Nov 2 '08 #3
Atli
5,058 Expert 4TB
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
Nov 3 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: DJ Dev | last post by:
Hi, I am stuck at a problem for 3 days now. I create runtime datagrids depending on the user selections and they may vary from 2-10 depending on user selection at runtime. The datagrids are...
9
by: Rekha | last post by:
The data is filled in datagrid. I want to know how to add a checkbox column in datagrid? In the runtime, checkbox is checked then instead of checkbox value the (caseID )column value is retained....
2
by: somaskarthic | last post by:
Hi In my php code , i dynamically created table rows on button click event. Each row contain 3 selectboxes, 7 checkboxes . On each click of these elements i need to submit the form and save the...
4
by: =?Utf-8?B?VG9kZCBKYXNwZXJz?= | last post by:
Hey guys, Is there ANY way to accomplish this: (see below)? Basically, I want to have a loop (a < 3 is just for testing purposes, it will be an underermined amount). In this loop, I want to be...
9
by: Sebarry | last post by:
Hi, I've put together a little test page to create checkboxes each time a button is clicked. The check button is created and should be appended to the form, but when the post is submitted the...
1
by: febad | last post by:
Hello, I will like to change the value of a check box at runtime. I have a database with a field "multiple_print_select", which stores a value of "1" when it is checked so that the record can be...
14
by: tdahsu | last post by:
I have twenty-five checkboxes I need to create (don't ask): self.checkbox1 = ... self.checkbox2 = ... .. .. .. self.checkbox25 = ... Right now, my code has 25 lines in it, one for each...
1
by: iderocks | last post by:
Hi All, I created a dynamic checkbox in ASP .Net inside a Button1_Click event method (outside the page_load event) and performed the event handling method for the CheckedChanged event and when I...
2
by: divyac | last post by:
I have developed an address book using php and mysql.I have all the contacts list in a table with check boxes.Now that i want to create mailing labels for the checked contacts...i.e.,the arrangements...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.