Use while loop to update database content, from a form - php

My users have profile photos with captions. Each photo is stored under an auto incremented ID in a database table, so in order to display all relevant photos for an individual user I while look content relevant to their user number.
Because of the differing photo quantities between users, I am unsure of how to process the submitted form.
Is it possible to while loop content back into the database in order to update the table?
Code that produces form details:
while($row = mysql_fetch_array($result))
{
echo '<div style="width:180px; height:210px; float:left;"><img src="../assets/uploads/thumbnail_' . $row['photoLINK'] . '" alt="" class="profileImgsStretch"/>';
echo '<input type="text" name="caption' . $row['photoID'] . '" value="' . $row['photoCAPTION'] . '"></div>';
echo '<input type="hidden" name="picture" value="' . $row['photoID'] . '"></div>';
}

First you need to look at the structure of the database. if 1 user has many pictures you need to make a separate database table consisting of the user id and the photo id
your database structure should be like this
myDB
table 1 -Users
table 2 -user_and_picture
-user_id
-picture_id
table 3 -Pictures
You insert the new picture into the pictures table
Then retrieve that picture id
get the user id and the picture id and store it in the new table user_and_picture
When you want to retrieve the pictures that belong to lets say user 1 you run a query on table user_and_pictures
an example would be
SELECT user_id, user_picture_id
FROM user_and_picture
WHERE user_id = 1
The result of this query will give you all the pictures associated with user #1
after that make a query that gets the filenames of those pictures

In your html use:
<input type="text" name="caption[' . $row['photoID'] . ']" value="' . $row['photoCAPTION'] . '">
You may remove the hidden input field.
So that in your PHP file you may use:
$captions = $_GET['caption'];
foreach ($captions as $picture_id => $caption){
/*
VERY IMPORT TIP ABOUT HACKING: For each caption, check if the picture_id belongs to the user BEFORE making the update. Otherwise a hacker can just change the picture id's and add malicious captions for other users!
If the picture belongs to the uploading user, then continue.
*/
echo 'caption for picture_id: '.$picture_id.' = '.$caption.'<br />'
}

This is how form with data works:
your input field have element named name. its the variable name for your input which you receive in you back-end i.e PHP page.
the form variable name could be literal variable or could be an array.
so for example:
login:
<input type="text" name="username" />
<input type="text" name="password" />
to get it in PHP
$user = $_POST["username"];
$pass = $_POST["password"];
OR in array
<input type="text" name="loginData[username]" />
<input type="text" name="loginData[password]" />
to get it in PHP
$userData = $_POST["loginData"]; // $userData = Array(username, password)
OR
$user = $_POST["loginData[username]"];
$pass = $_POST["loginData[password]"];

Related

How to INSERT multiple rows with multiple values, but only for items that have checkboxes ticked next to them

I am coding a discography tool for a music database.
Artists are able to insert tracks, singles, EPs, and albums all into separate tables on the database.
Having tracks be in their own separate table allows the same tracks to be attached to multiple singles, EPs and albums while only requiring there to be one record for that track in the database.
Which means individual track pages can have an automatically generated list of links back to the Singles, EPs and albums that they appear on. Making navigating the database through the website a much smoother experience.
I have come to the point where I am coding a tool to attach any existing tracks in the database for a given artist onto an album page.
I am using another table in the database called 'trackconnections' to create the relational links between the track ids from the track table and the album id from the album table, with an additional column called albumtracknum available to be able to output the tracks in the right order when queried on the album page.
The code for the tool is behind a button labelled 'Attach existing track(s) to album'. The code for this tool is as follows:
if (isset($_POST['attachexistingtracktoalbum-submit'])) {
require "includes/db_connect.pdo.php";
$artistid = $_POST["artistid"];
$albumid = $_POST["albumid"];
echo '<strong>Select each track you would like to add to this album below and type in the track number you want it to have on the album in the box underneith the name of each selected track.</strong><br><br>';
$stmt = $pdo->query("SELECT * FROM track WHERE artist_id = '$artistid'
order by trackname");
while ($row = $stmt->fetch())
{
echo '<form action="includes/attachexistingtracktoalbum.inc.php" method = "post">';
echo '<div class="checkbox">';
echo '<label>';
echo "<input type='checkbox' name='trackid[]' value='".$row['id']."' />";
echo '<span class="cr"><i class="cr-icon glyphicon glyphicon-ok"></i></span>';
echo ' '.$row['trackname'];
echo '</label>';
echo "<br><label for='albumtracknum'>Track number:</label><br>
<input type='text' name='albumtracknum[]'>
<input type='hidden' name='albumid[]' value='".$albumid,"'>
<br><br>";
echo '</div>';
}
?>
<input type="hidden" name="albumidforreturn" value="<?php echo $albumid;?>">
<button type="submit" name="attachexistingtracktoalbum-submit">Attach track(s) to album</button>
<?php }
else {
header("Location: /index.php");
exit();
}?>
(NB: The post data here is not sanitised for the sql query as it has been passed along in a hidden form from the original album page)
This generates a page with all track names for the current artist available on a list with checkboxes, with each track name being followed by a data entry box to enter the track number for the album being added to.
Submission of the form then hands off to the following include code:
if (isset($_POST['attachexistingtracktoalbum-submit'])) {
require "db_connect.pdo.php";
$albumid = implode(',',$_POST['albumid']);
$trackid = implode(',',$_POST['trackid']);
$albumtracknum = implode(',',$_POST['albumtracknum']);
$albumidforreturn = $_POST['albumidforreturn'];
// echo 'albumid: '.$albumid.'<br>';
// echo 'trackid: '.$trackid.'<br>';
// echo 'albumtracknum: '.$albumtracknum.'<br>';
$sql = "INSERT INTO trackconnections (albumid, trackid, albumtracknum) VALUES (?,?,?);";
$stmt= $pdo->prepare($sql);
$stmt->execute([$albumid,$trackid,$albumtracknum]);
header("Location: ../albumdetail.php?albumid=$albumidforreturn");
exit();
}
else {
header("Location: ../index.php");
exit();
}
(NB: The commented out echos are there to test what the output is from the previous form)
The 2 problems I am having are that my 'Attach existing tracks to album' form submit:
1. Passes on too much data.
The generated form should only pass on the track ids, album number, and track numbers that have had their checkboxes ticked. For insertion into the 'trackconnections' table.
Instead it narrows down the ticked checkbox track ids only and then creates comma separated values for every available track to select, rather than just those actually selected.
Which leads to annoying outputs such as the following when passing on data from form to include:
albumid: 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4
trackid: 30,14
albumtracknum: ,2,3,,,,,,,,,,,,,,,,,,,,,
Where it should only read as:
albumid: 4,4
trackid: 30,14
albumtracknum: 2,3
Having too much data get passed through means that the row inserts won't be correct on multiple INSERTS once I do get this working, as they won't align with one another in the correct order.
2. The include only INSERTS 1 row to the 'trackconnections' table.
It seems I am misunderstanding how to add multiple rows to the database with my code here.
As having multiple checkboxes ticked on my 'Attach existing tracks to album' form only inserts 1 single row to the database on submission of the form each time.
Consistently the only track that gets added to the 'trackconnections' table is the first track with its checkbox ticked and, because of issue no. 1 above, the albumtracknum is always 0 unless I type a number into the first albumtracknum box on the available checklist.
I need to make tweaks to this code so that both problem 1 and 2 are addressed together, meaning that ticking the checkboxes & adding track numbers into each box following the track names actually adds multiple rows to the database along with their corresponding album track numbers.
I hope someone can help.
EDIT TO SHOW REFINED AND WORKING CODE:
New code for checkbox and textbox sections -
if (isset($_POST['attachexistingtracktoalbum-submit'])) {
require "includes/db_connect.pdo.php";
$artistid = $_POST["artistid"];
$albumid = $_POST["albumid"];
echo '<strong>Select each track you would like to add to this album below and type in the track number you want it to have on the album in the box underneith the name of each selected track.</strong><br><br>';
$stmt = $pdo->query("SELECT * FROM track WHERE artist_id = '$artistid'
order by trackname");
echo '<form action="includes/attachexistingtracktoalbum.inc.php" method = "post">';
while ($row = $stmt->fetch())
{
echo '<div class="checkbox">';
echo '<label>';
echo "<input type='checkbox' name='trackid[]' value='".$row['id']."' />";
echo '<span class="cr"><i class="cr-icon glyphicon glyphicon-ok"></i></span>';
echo ' '.$row['trackname'];
echo '</label>';
echo "<br><label for='albumtracknumber'>Track number:</label><br>
<input type='text' name='albumtracknumber_".$row['id']."'>
<input type='hidden' name='albumid[]' value='".$albumid,"'>
<br><br>";
echo '</div>';
}
?>
<input type="hidden" name="albumidforreturn" value="<?php echo $albumid;?>">
<button type="submit" name="attachexistingtracktoalbum-submit">Attach track(s) to album</button>
</form>
<?php }
else {
header("Location: /index.php");
exit();
}
New code for the include INSERT processing -
if (isset($_POST['attachexistingtracktoalbum-submit'])) {
require "db_connect.pdo.php";
$albumid = implode(',',$_POST['albumid']);
$trackid = implode(',',$_POST['trackid']);
$albumidforreturn = $_POST['albumidforreturn'];
foreach($_POST['trackid'] as $trackidloop) {
$albumtracknum = $_POST["albumtracknumber_{$trackidloop}"];
$sql = "INSERT INTO trackconnections (albumid, trackid, albumtracknum) VALUES (?,?,?);";
$stmt= $pdo->prepare($sql);
$stmt->execute([$albumid,$trackidloop,$albumtracknum]);
}
header("Location: ../albumdetail.php?albumid=$albumidforreturn");
exit();
}
else {
header("Location: ../index.php");
exit();
}
This isn't an entire solution but I see some problems:
You are looping through tracks and creating a new form for each one. The first problem is , you are missing the closing form tag. I guess the browser is automatically creating one, when it sees the next form start tag. ?? That's why you only get one single posted checkbox.
I would put all the track checkboxes into a single form. Then the posted trackid[] array will contain all the checked items.
[EDIT after your comment: The hidden fields albumid[] post the entire array, whereas the trackid[] checkboxes only post the actual checked boxes (HTML spec).
Instead of having albumid[], You could put the trackID and albumID together for the checkbox value, then parse them apart when you handle the post:
$value = $row['id']. ',' . $row['albumid'];
echo "<input type='checkbox' name='trackid[]' value='".$value."' />";
ALSO, the SQL, "INSERT INTO (..) .. VALUES (...) " only inserts one row.
It's easy to do that SQL in a loop for all the checked boxes.
foreach($_POST['trackid'] as $value) {
// parse the $value...
// SQL Insert...
}
EDIT 2: From my own comment:
Like hidden fields, input (text) field arrays also post the entire array (with empty values for blank inputs). (Again, this is not a PHP thing, it's a web browser standard to only post checked checkboxes and radio buttons. But ALL text and hidden INPUTs are posted.) So in your example, you need to code a mechanism to know which textbox goes with each checkbox. Quick and dirty...You could add a row index (0,1,2,3...) as another comma-separated number in your checkbox values, then you'll have the index into the posted textbox array. Alternatively, you could name the textboxes ' .. name="textinput_' . $row['trackid'] . '" ...' (not an array), then upon post, read them in your foreach loop with
$val = $_POST["textinput_{$trackid}"];

Inserting id into MYSQL table for multiple image upload(homestay)

I currently have a form that creates an item in a database then in the next step it allows the user to upload multiple images for that listing both sections work on their own but i need to be able to insert the $id from the first page into the query for the image upload to identify which listing it corresponds to
Here is the page to allow the user to upload multiple images
<input name="name" type="text" id="name" size="40" placeholder="Homestay's Name"/>
<input name="type" type="text" id="type" size="40" placeholder="Homestay's Type"/>
<input name="category" type="text" id="category" size="40" placeholder="Homestay's Category"/>
<input class='file' multiple="multiple" type="file" class="form-control" name="userfile[]" placeholder="Please choose your image">
here is the function
$path = pathinfo($_SERVER['PHP_SELF']);
for ($i = 0; $i < count ($_FILES['userfile']['name']); $i++)
{
$tmp_file = $_FILES['userfile']['tmp_name'][$i];
$filetype = $_FILES['userfile']['type'][$i];
$filesize = $_FILES['userfile']['size'][$i];
$filename = $_FILES['userfile']['name'][$i];
$destination = $path['dirname'] . '../data/' . $filename;
if (move_uploaded_file($tmp_file, $_SERVER['DOCUMENT_ROOT'] . $destination))
{
$name = $_POST['name'];
$type = $_POST['type'];
$category = $_POST['category'];
$sql="INSERT INTO homestay (name, type,category,)
VALUES
('$name', '$type','$category')";
$result = mysql_query ("insert into img_homestay (id, location,filetype, filename, filesize)
values ('".$id."', '" . $destination . "','" . $filetype ."','" . $filename . "'," . $filesize . ") ");
}
}
If I understood your question:
In the previous page you have the id from the list.
At the second page, put it into a hidden field like:
<input type="hidde" name="theId" value="<?php echo $_POST["idFromPrevPage"]; ?>" />
And then you can access it from $_POST["theId"]
EDIT:
Based on your comment, I think, you want to do something like this:
$sql = "INSERT INTO homestay (name, type,category) VALUES ('".mysql_real_escape_string($name) ."', "
. "'".mysql_real_escape_string($type)."','".mysql_real_escape_string($category)."')";
$res = mysql_query($sql);
$id = mysql_insert_id(); //This where you can get the last insert id.
NOTES
Anyway, you have several problems in your original code:
In your first insert query have an unwanted , character at the field list.
Do not use mysql_* functions, they are deprecated. Use mysqli_* functions or PDO!
Escape your data to avoid sql vulnerables or use prepared statements.
I think your best bet would be :
create the form to insert the db item
get the ID of the inserted item, and add it as a hidden element on the second step
once the second form gets back, you can easily see the ID you need.
Do note that this approach will send DB id's client side, but is the simplest option.
A second approach is to utilize session variables - simply store the item ID to the user session, and retrieve it on the second step. This is safer, but not a good approach if multiple forms can be submitted at the same time by the same account/person.
$_SESSION['the_id'] = $result->id;
and then
$id = $_SESSION['the_id'];
The best approach (in my opinion always) is to use a non-sensical token to the client side, and map it to the proper ID using some logic. A simple example is to generate a complex hash for the ID, and send that as a hidden field to the user when sending the form for step 2. Store the ID and hash in a database table.
When the user POSTs that back, use the hash to map it back to a normal ID, and destroy the entry.

Create update query for dynamic database

I am trying to update a row in a mysql database. To do this, I would use a simple query like this:
"UPDATE `contactinfo` SET `Company` = 'Google', WHERE `id` = '1';"
The problem is that the database is dynamic and users can add columns at any time. So I do not know the names of the columns. To find the names and create a form to post to the page that will actually do the mysql work uses this code:
<?php
$result = mysql_query("select * from contactinfo WHERE `id` = '".$rid."';");
if (!$result) {
die('Query failed: ' . mysql_error());
}
$i = 0;
while ($i < mysql_num_fields($result)) {
$meta = mysql_fetch_field($result, $i);
if (!$meta) {
echo "ERROR";
}
$name = $meta->name;
$r = mysql_fetch_array(mysql_query("select * from contactinfo WHERE `id` = '".$rid."';"));
$content = $r[$name];
if($name != 'id') {
echo "<tr><td align='center'><div align='left'>Edit ".$name."</div></td></tr>";
echo "<tr><td align='center'><input type='text' value='" . $content . "' /></td></tr>";
}
$i++;
}
mysql_free_result($result);
?>
This creates a nice little table with input boxes that allow the user to edit the content of the row that has been selected. The row id number($rid) is used to identify which row needs to be changed.
My question is, how can I get the new content for the row from the posted form and create a query to update it? I can't seem to figure out how to dynamically get the names of the form as well as the new content to the write the query.
If any clarification is needed just let me know and all help is appreciated.
Thanks.
The easiest way is to name the fields in the form exactly how the name of the fields in the database are.
Lets say you have this form
<form action="">
<input name="field[firstname]">
<input name="field[lastname]">
<input name="field[address]">
</form>
You should probably be able to create the form based on the fields names too, you are probably already doing this.
In the file that processes the response you can do something like this:
foreach($_POST['field'] as $field_name => $field_value) {
$sql_str[] = "{$field_name} = '{$field_value}'";
}
This just goes through the 'field' array that comes from post and puts the proper update text into another array.
Then just do a
mysql_query("UPDATE contactinfo SET ".implode(',', $sql_str)." WHERE `id` = '".$rid."';")
To put it into the database.
What you can do is add a column in the database with a bit flag and if the user is new or old, the flag will reflect it.
that way you can update the user to say if it is new or old.
hope this helps.
You might do well to investigate whether there is an ORM framework that you could use.
Anyway, the simplest way to do what you want is to pass the name of the field in the INPUT field.
$content = AddSlashes($content); // You may need HTMLEntities() here
$field = <<<FIELD
<input type="text" value="$content" />
FIELD;
Also, another useful trick to employ is to either supply an array of "protected" fields, or specify a prefix that makes the field unchangeable. For example, you almost certainly do not want a user to be able to change the primary keys.
So you could generate the form with
if ('id' == $meta->name or 'pk' == $meta->name or (0 === strpos($meta->name, 'pk_')))
{
// This is a primary key field
$html .= <<<PKFIELD
<input type="hidden" name="{$meta->name}" value="{$contents}" />
PKFIELD;
continue;
}
if (0 === strpos($meta->name, 'hf_'))
{
// hidden field, ignored (can't be changed)
continue;
}
if (0 === strpos($meta->name, 'ro_'))
{
// read-only field, shown but without even the name
$html .= <<<ROFIELD
<input type="text" readonly="readonly" class="readonly" value="{$contents}" />
ROFIELD;
continue;
}
Or you could use a fixed-size prefix and an array of templates:
$fieldtpls = array(
'pk_' => '<input type="hidden" name="{NAME}" value="{VALUE}" />',
'ta_' => '<textarea name="{NAME}">{VALUE}</textarea>',
'ro_' => '<input type="text" value="{VALUE}" readonly="readonly" />',
);
then you analyze each field and apply a default unless a known prefix is used.
Your users would then have to name the fields accordingly. Another possibility is to allow for a "meta-table" to hold the type and handling of each table and field, but in that case you would need to complicate a bit the user interface by creating a sort of "admin editor" for the Meta table:
table field type rw parameters
users name text yes
users id hidden no
users role text no
users notes area yes class:"widearea"
...and that way be dragons ORM frameworks.

PHP MYSQL Changing the photo on my database

I am very new to PHP;
ADD.PHP - I have a form that collects the following information 1. name 2. email 3. phone and picture
pictures are stored on a directory folder on my server and then the filename of that photo is stored on my sql table.
VIEW.PHP - all the data in mysql is being displayed in this page including the photo in tabular format including the id of every record. The id being display is a hyperlink in which when clicked you will be directed to a page wherein you can edit the record contents:
below is the code for my EDIT.PHP
<?php
// Connects to your Database
mysql_connect("localhost", "user1", "12345") or die(mysql_error()) ;
mysql_select_db("test") or die(mysql_error()) ;
// Check whether the value for jobnumber is transmitted
if (isset($_GET['id'])) {
// Put the value in a separate variable
$id = $_GET['id'];
// Query the database for the details of the chosen jobnumber
$result = mysql_query("select id, name, email,
phone, picture from employees where id = $id");
// Check result
// This shows the actual query sent to MySQL, and the error. Useful for debugging.
if (!$result) {
$message = "Invalid query: " . mysql_error() . "\n";
$message .= "Whole query: " . $query;
die($message);
}
// Attempting to print $result won't allow access to information in the resource
// One of the mysql result functions must be used
// See also mysql_result(), mysql_fetch_array(), mysql_fetch_row(),etc.
while ($row = mysql_fetch_array($result)) {
$name = $row['name'];
echo $name. "\n";
echo $row['email'] . "\n";
echo $row['phone'] . "\n";
echo "<img width=500px height=500px src=pics/" . $row['picture'].">" . "\n";
// form tag
echo '<form action="add2.php" method="POST">';
//display name
echo 'Name: <input type="text" name="name" value="';
echo $row['name'];
echo '"><br>';
//display email
echo 'email: <input type="text" name="email" value="';
echo $row['email'];
echo '"><br>';
//display phone
echo 'Phone: <input type="text" name="phone" value="';
echo $row['phone'];
echo '"><br>';
//display photo
echo 'Photo: <input type="text" name="photo" value="';
echo $row['picture'];
echo '"><br>';
echo '<input type="submit" value="Add">';
echo '</form>';
}
} else {
die("No valid data specified!");
}
?>
using this code, the test fields went well but the input box for the photo is blank and when i click the button the photo field in my database will be blank unless i uploaded a new photo?
how can the user change the existing photo? or retain the old photo if not being changed?
Assuming your employees.picture column stores the path to the image.
To upload a new photo, you're going to need to change a couple of things...
Your form needs to use the correct encoding type, ie
<form action="add2.php" method="post" enctype="multipart/form-data">
You also need to provide a "file" input element to accept the new photo, eg
<input type="file" name="photo">
To see if a new photo has been supplied, simply check (after detecting a valid POST request)
if ($_FILES['photo']['error'] == UPLOAD_ERR_OK) {
// file uploaded
}
see this link :http://www.w3schools.com/PHP/php_file_upload.asp
As to changing the existing photo, u should also save the name of the photo name in your db fom within the page add2.php
when any users wants to upload the photo for the second time, then a check should be made in add2.php to find whether a photo was previously uploaded. Then U can take a decision from the user from 2 yes, no buttons. If yes then UPDATE( not insert) the corresponding column in db table.
U can use jquery to take the decision and work accordingly. Any help needed with jquery?
IF it is for the first time that the uploading is gonna take place, then u can bypass the check process.
Clear?

How to save checkbox array in database?

I have this form:
<tr>
<td><input type="hidden" name="ledlamps" value="LED lamps">LED lamps:</td>
<td><input class="field checkbox" type="checkbox" name="box[]" value="3mm"/><label class="choice">3mm</label></td>
<td><input class="field checkbox" type="checkbox" name="box[]" value="5mm"/><label class="choice">5mm</label></td>
<td><input class="field checkbox" type="checkbox" name="box[]" value="8mm"/><label class="choice">8mm</label></td>
<td><input class="field checkbox" type="checkbox" name="box[]" value="10mm"/><label class="choice">10mm</label></td>
<td><input class="field checkbox" type="checkbox" name="box[]" value="Ovals"/><label class="choice">Ovals</label></td>
<td><input class="field checkbox" type="checkbox" name="box[]" value="Assembly LEDs"/><label class="choice">Assembly LEDs</label></td>
</tr>
and this php code:
$box=$_POST['box'];
$ledlamps = $_POST['ledlamps'];
if ($box != 0) {
echo "$ledlamps: ";
while (list ($key,$val) = #each ($box)) {
$val1 = "$val, ";
echo "$val1";
}
}
If I output with echo it displays in the way I want it:
Led lamps: 3mm, 5mm, 8mm (if i tick the respective checkboxes)
But I want to store this in a mysql table field. How do I do that?
Thanks for your help!
You can use implode() to join the $box array in to one string as follows:
$box=$_POST['box'];
$ledlamps = $_POST['ledlamps'];
$str = $ledlamps . ": " . implode(", ", $box);
Then $str should contain "Led lamps: 3mm, 5mm, 8mm" (depending on the fields you've checked).
This field can then be inserted in to whatever mysql column you need (assuming here that your query is properly escaped and input validated before attempting a db query..).
This way of storing the data will likely make it quite tricky to query the data again should you wish to pull it out again, so while it should suffice for simple printing to screen of something like a product history, you may be better investigating whether your db table structure could be made more accommodating. So if we assumed this was for a stock control system, something like:
category table:
- category_id
- category_name (LED lamps..)
stock table:
- item_id
- category_id (id for LED lamps..)
- item_description (3mm lamp, etc)
stock_selection table:
- transaction_id
- user_id
- date
stock_transaction table:
- transaction_id
- item_id
- change (number of items added/removed)
So once you receive back the checkboxes from your form, you create a new stock_selection record for the logged-in user. You then use the id from this new record and make 1 entry per checkbox in the stock_transaction table.
This would allow you in future to query your database to keep track of things like how many items have been taken out (or re-added, etc), who took them, etc, and may ultimately be a more future-proof approach, depending on your overall architecture.
Either store them in the database serialized as suggested by #Sarfraz, or comma separated, or in a separate table, joined with the primary table.
You could either:
$boxes = implode(",", $_POST['boxes']);
$id = intval($_GET['id']);
$sql = sprintf("UPDATE table SET box=%s WHERE id=%d", $boxes, $id);
$res = mysql_query($sql);
This would store box values in a comma-separated array, which you could then explode() when retrieving from the database.
Alternatively, you could set up a separate table if you want to go the proper route. That way you could have a many-to-one relationship, where one object (let's say car) can have many lamps.
Table: cars
- ID
- Name
Table: cars_lamps
- Car_ID
- Lamp_ID
Table: Lamps
- ID
- Name
I hope that makes sense.

Categories