TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 08-30-2010, 07:03 PM   #1 (permalink)
The Visitor
Newcomer 
 
Join Date: Aug 2010
Posts: 2
Thanks: 0
OpenBSD is on a distinguished road
Default Multiple CheckBox

Hi,
I have Any Form For registering user. how to insert multiple rows in mysql for each user.

my form :
<input type = "text" name = "color" size = "10"><br>
<input type = "checkbox" name = colors[] value = "red">Red
<input type = "checkbox" name = colors[] value = "blue">Blue
<input type = "checkbox" name = colors[] value = "green">Green
<input type = "checkbox" name = colors[] value = "megenta">Megenta

how to insert multiple rows in mysql for each user.
MY mysql table is :
|id|name|lastname|color|date|desc

and how to show color list for each user .
example :
user 1 : red - blue
user 2 : blue - green . . etc
thanks.
OpenBSD is offline  
Reply With Quote
Old 08-30-2010, 08:44 PM   #2 (permalink)
The Addict
 
tony's Avatar
 
Join Date: Aug 2008
Posts: 336
Thanks: 8
tony is on a distinguished road
Default

So you want to stored an array of selected colors in 1 field.
I would say store the array as a string in the field. Something like this:

php Code:
<?php
//to store it coming from the post info of the submitted form.
//$color var is the array from the choices, preferably sanitized.
$colors = join(';', $color);
$db = new mysqli('localhost','user','password','app_db');
if (mysqli_connect_errno()) {
    trigger_error("Can't connect to MySQL Server. Errorcode: %s\n", mysqli_connect_error());
}

if($db->query("INSERT INTO users (name,lastname,color,date,desc) VALUES ($name, $last, $colors, NOW(), $description)")) {
    echo "The user was inserted!";
} else {
    trigger_error("There was an error trying to save user $name");
}

$link->close();

?>

Then when you want to show the list, just query the db and split by the ';' string and you would get back the array of colors.
tony is offline  
Reply With Quote
Old 08-30-2010, 11:12 PM   #3 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

The only problem with storing serialized or imploded arrays in your database is that you complicate searching on those fields by a factor of ten. It goes up from there depending on the type of information being stored; in this case you could do a LIKE search and still get relatively accurate information, though in the process you eat your own performance.

mysql Code:
CREATE TABLE `user_colors` (
`user_id` BIGINT( 20 ) UNSIGNED NOT NULL,
`color` ENUM( 'red','blue','green','magenta' ) NOT NULL,
INDEX ( `user_id` ),
) ENGINE = MYISAM

The beginnings of a user meta information table; you could easily switch to an NVP setup or simply create additional columns as necessary. Now with two simple queries you could easily pull up the information for a specific user, or all users who list the color blue, or combine both searches in one. Update your enum field to add more colors, or switch it for a varchar if the need becomes too great.
__________________
uʍop ǝpısdn sı ɹǝʇndɯoɔ ɹnoʎ sıɥʇ pɐǝɹ uɐɔ noʎ ɟı
delayedinsanity is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
inserting multiple checkbox selections on one row sarmenhb Absolute Beginners 3 01-10-2009 07:32 PM
A serious curl checkbox problem bedri Advanced PHP Programming 1 05-22-2008 04:25 PM
Multiple Checkbox problems riscphree General 15 01-17-2008 06:18 PM
how to make the value of checkbox 1 or 0 meshi General 2 10-16-2007 07:25 AM


All times are GMT. The time now is 07:13 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design