PHP creating checkbox in runtime | Newbie | | Join Date: Sep 2008
Posts: 3
| | |
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
|  | Moderator | | Join Date: Jun 2007 Location: York, England, with wolves.
Posts: 4,936
| | | 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
| | | 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: - <?php
-
$host="localhost"; // Host name
-
$username=""; // Mysql username
-
$password=""; // Mysql password
-
$db_name="test"; // Database name
-
$tbl_name="test_mysql"; // Table name
-
-
// Connect to server and select databse.
-
mysql_connect("$host", "$username", "$password")or die("cannot connect");
-
mysql_select_db("$db_name")or die("cannot select DB");
-
-
$sql="SELECT * FROM $tbl_name";
-
$result=mysql_query($sql);
-
-
$count=mysql_num_rows($result);
-
-
?>
-
<table width="400" border="0" cellspacing="1" cellpadding="0">
-
<tr>
-
<td><form name="form1" method="post" action="">
-
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
-
<tr>
-
<td bgcolor="#FFFFFF"> </td>
-
<td colspan="4" bgcolor="#FFFFFF"><strong>Delete multiple rows in mysql</strong> </td>
-
</tr>
-
<tr>
-
<td align="center" bgcolor="#FFFFFF">#</td>
-
<td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td>
-
<td align="center" bgcolor="#FFFFFF"><strong>Name</strong></td>
-
<td align="center" bgcolor="#FFFFFF"><strong>Lastname</strong></td>
-
<td align="center" bgcolor="#FFFFFF"><strong>Email</strong></td>
-
</tr>
-
<?php
-
while($rows=mysql_fetch_array($result)){
-
?>
-
<tr>
-
<td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"></td>
-
<td bgcolor="#FFFFFF"><? echo $rows['id']; ?></td>
-
<td bgcolor="#FFFFFF"><? echo $rows['name']; ?></td>
-
<td bgcolor="#FFFFFF"><? echo $rows['lastname']; ?></td>
-
<td bgcolor="#FFFFFF"><? echo $rows['email']; ?></td>
-
</tr>
-
<?php
-
}
-
?>
-
<tr>
-
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
-
</tr>
-
<?
-
// Check if delete button active, start this
-
if($delete){
-
for($i=0;$i<$count;$i++){
-
$del_id = $checkbox[$i];
-
$sql = "DELETE FROM $tbl_name WHERE id='$del_id'";
-
$result = mysql_query($sql);
-
}
-
-
// if successful redirect to delete_multiple.php
-
if($result){
-
echo "<meta http-equiv=\"refresh\" content=\"0;URL=delete_multiple.php\">";
-
}
-
}
-
mysql_close();
-
?>
-
</table>
-
</form>
-
</td>
-
</tr>
-
</table>
|  | Moderator | | Join Date: Nov 2006 Location: Iceland
Posts: 3,741
| | | re: PHP creating checkbox in runtime
Hi.
A few things I would like to point out. - 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. - 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.
- 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. - 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?
- 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.
- 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 |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,295 network members.
|