General Programming Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingGeneral Programming Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old December 20th, 2003, 07:42 AM
geo geo is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Posts: 2 geo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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

Reply With Quote
  #2  
Old December 22nd, 2003, 09:34 AM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to dhouston
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:
 $blah=array("droppable table names here, whether from db or hard-coded");
foreach(
$_POST["check"] as $c){
    if(
in_array($blah,$c)){ //check parameter order
        
$drop_query="drop table " $c;
        
$result=mysql_query($drop_query,$db) or die(mysql_error());
    }


Reply With Quote
  #3  
Old December 25th, 2003, 07:26 AM
geo geo is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Posts: 2 geo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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?

Reply With Quote
  #4  
Old December 27th, 2003, 01:12 AM
Mike_r Mike_r is offline
ExoCrew
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Posts: 68 Mike_r User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
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.
__________________
ExoHelpDesk
ExoCrew Free Services

Reply With Quote
  #5  
Old December 29th, 2003, 08:35 AM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to dhouston
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....

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingGeneral Programming Help > problem with checkboxes


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT