|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
problem with checkboxes
Hello. I'm new in php and I have a problem with checkboxes
What I've done is this: <?php $show_query = "show tables"; $result = mysql_query($show_query,$db); $num_rows = mysql_num_rows($result); echo" There are currently $num_rows "; print "<table border=2>\n"; while ( $a_row = mysql_fetch_row( $result ) ) { foreach ( $a_row as $field ) echo "<input type = \"checkbox\" name = \"check[]\" value = \"$field\">$field<br>"; } ?> <input type = "submit" name = "delete" value ="delete"> With this I want to delete one or more tables from a database by checking them.I have tried this: delete.php $show_query = "show tables"; $result = mysql_query($show_query,$db); $num_rows = mysql_num_rows($result); while ($s_row = mysql_fetch_array($result)) { for ($i=0; $i<$num_rows; $i++) { if ($$s_row[$i] == on) { $drop_query = "drop table $s_row[$i]"; $result = mysql_query($drop_query,$db); } }} but it drops only one table at a time,plus I'm certain there is a better way to do it but I'm not aware of it. Thanks in advance for any help |
|
#2
|
||||
|
||||
|
Unless you've really got your mysql privileges set up tightly, you really don't want to allow people to drop tables through a Web form. For example, pretend I viewed the source on your document, saved it locally, added a checkbox with "users" or some other likely table name, checked it, and submitted the form. Voila -- your users table is gone. At any rate, you need to be very careful about validating what you allow people to drop.
To get to your actual question, it looks like your code is looking for a field whose name matches a table name from the database and will drop that table if the box sharing its name is checked. Problem is your checkbox is named check[], so this won't work. Maybe I'm misreading your code. If I were wreckless enough to do something like this, I'd probably go with something more like the following: PHP Code:
|
|
#3
|
|||
|
|||
|
Thank you for your comments.
I can't understant exactly how $_POST works. Instead I did this for($i = 0; $i < sizeof($check); $i++) { $drop_query = "drop table $check[$i]"; $result = mysql_query($drop_query,$db);} and it works fine. Another question(it may be stupid): If you want to have a database with information about some people, is it better to create a table for each person and store their data seperatly or create one or two tables(for example one table with the person's name only and the other table with the person's name and the other data) and store in all the data? |
|
#4
|
|||
|
|||
|
Its not the best to use $check there, instead you must use what dhouston posted.
$_POST is a superglobal and an array which contains all the information submitted through a form using action="post" ... If you dont want to use what dhouston posted, then use $_POST['check'] instead of just $check. |
|
#5
|
||||
|
||||
|
Geo, all the $_POST does is to scope the check variable. That is, if you were careless and accidentally named another variable (not received from form input) $check, you could run into problems. $_POST is an array of post variables, and using $_POST["check"] ensures that you're getting the value you intend to get. Additionally, for the sake of code portability, you should always use these superglobals, as some PHP setups don't allow you to use just $check, which could result in lots of tedious code reworking should you ever have to change servers.
As for tables, you definitely don't want a table per person, and really, there's no need to create two tables. In most cases, the less data duplication you have, the better. So you'd really want a table more like the following: id int(10) not null primary key auto_increment, fname varchar(15), lname varchar(25), email varchar(80), address varchar(40) etc.... |
![]() |
| Viewing: Dev Articles Community Forums > Programming > General Programming Help > problem with checkboxes |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|