The first thing to know is that the HTML checkboxes will return the result if it is set, and will not return anything if it is not checked. Therefore, in your html it is important to increase your values:
<td align="right">Available Sizes</td> <td> <input type="checkbox" name="sizes[]" value="1" /> Sizes 2-6<br /> <input type="checkbox" name="sizes[]" value="2" /> Sizes 6-10 <br /> <input type="checkbox" name="sizes[]" value="3" /> Sizes 10-14<br /> <input type="checkbox" name="sizes[]" value="4" /> Sizes 14-18 <br /> <input type="checkbox" name="sizes[]" value="5" /> Sizes 18-22<br /> <input type="checkbox" name="sizes[]" value="6" /> Sizes 22-26 <br /> </td>
A fully normalized database schema will look something like this:
//Note that 'names' and 'collections are overloaded //words and should be avoided table BRANDS id INT AUTOINCREMENT collectionname VARCHAR table SIZES id INT AUTOINCREMENT sizecategory VARCHAR //ie 'Sizes 18-22' table COLOURS id INT AUTOINCREMENT colourname VARCHAR table INVENTORY id INT AUTOINCREMENT brandid INT FOREIGN KEY (BRAND) imageurl VARCHAR table INVENTORY_SIZES inventoryid INT FOREIGN KEY sizeid FOREIGN KEY table INVENTORY_COLOURS inventoryid INT FOREIGN KEY colourid FOREIGN KEY
Your BRAND
, COLOURS
and SIZES
tables should be correctly populated with your available parameters, and you could theoretically dynamically load your page with data from these tables. The id of each row in these tables should end with the values ββin your checkbox cells.
//get your inputs $query_values = array(); $query_values[':brandid'] = $dress_name = $_POST['Dress_name'];//note that you should change the value in your options here to the unique ids in your database. $query_values[':sizeid'] = getSize($_POST[]); $query_values[':colourid'] = getColour($_POST[]); $query_values[':quantity'] = $_POST['quantity']; $query_values[':imageurl'] = $_POST['imageurl']; //Prepare and execute your sql //Note that PDO will handle escaping problematic inputs. $sql = "INSERT INTO INVENTORY ('brandid', 'imageurl') VALUES (:brandid, :sizeid, :colourid, :quantity, :imageurl)"; executeInsert($sql, $query_values); $inventoryid = mysql_insert_id(); saveSizes($_POST['sizes'], $inventoryid); saveColours($_POST['colours'], $inventoryid); function saveSizes($size_array, $inventoryid) { $sql = "INSERT INTO INVENTORY_SIZES ('inventoryid', 'sizeid') VALUES (:inventoryid, :sizeid)"; foreach ($size_array as $sizeid) { $query_values = array(); $query_values[':inventoryid'] = $inventoryid; $query_values[':sizeid'] = $sizeid; executeInsert($sql, $query_values); } } function saveColours($colour_array) { $sql = "INSERT INTO INVENTORY_COLOURS ('inventoryid', 'colourid') VALUES (:inventoryid, :colourid)"; foreach ($size_array as $sizeid) { $query_values = array(); $query_values[':inventoryid'] = $inventoryid; $query_values[':colourid'] = $colourid; executeInsert($sql, $query_values); } } function executeInsert($sql, $query_values) { $query = $conn->prepare($sql); $query->execute($query_values); }
Be sure to use PHP PDO to protect against security issues. I am sure that some of them could be abstracted / cleaned better, but this should do the job. (Although I am also sure that there are small errors, as I do it coldly and without testing.)