Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have form in which multiple places can be added with jquery. Whole div id="stores" that can be added dynamically (cloned) is below. There is no limitation on number of places and every place can have multiple categories and subcategories. Now I need to insert that data in mysql and dont know how to do it. For example, can categories and subcategories be in same field? Category inside brackets followed by subcategories separated with comma or something and that for every category so I can list them after (or that cannot be done?). I dont know php that much so there is better solution probably. Thank you

    <p>Places</p>
     <div id="stores" class="clone">
       <label>Name</label><input type="text" name="name[]" id="name">
       <label>Address</label><input type="text" name="adr[]" id="adr">
       <label>City</label><input type="text" name="city[]" id="city">
       <label>Contact person</label><input type="text" name="con[]" id="con">
       <label>Telephone</label><input type="text" name="tel[]" id="tel">
       <label>E-mail</label><input type="text" name="mail[]" id="mail">
       <label>Products (separate with comma)</label><textarea name="prod[]"></textarea>
       <span class="catsubcat">Select category and subcategory</span>
         <div class="catleft">
           <h2><input type="checkbox" name="cat[]" value="Cat1">Cat1</h2>
             <input type="checkbox" name="subcat[]" value="Subcat1"> Subcat1<br/>
             <input type="checkbox" name="subcat[]" value="Subcat2"> Subcat2
         </div>
         <div class="catleft">
           <h2><input type="checkbox" name="cat[]" value="Cat2">Cat2</h2>
             <input type="checkbox" name="subcat[]" value="Subcat1"> Subcat1<br/>
             <input type="checkbox" name="subcat[]" value="Subcat2"> Subcat2
         </div>
         <div class="catleft">
           <h2><input type="checkbox" name="cat[]" value="Cat3">Cat3</h2>
             <input type="checkbox" name="subcat[]" value="Subcat1"> Subcat1<br/>
             <input type="checkbox" name="subcat[]" value="Subcat2"> Subcat2
         </div>
    </div>

UPDATED PHP - with this code i get all categories and subcategories inserted in one field separated with comma. First categories then subcategories. Can I somehow insert it in a way that it could be displayed properly later, with every subcategory under its category. For example instead of inserting like this in field:
category1,category2,subcategory1,subcategory1,subcategory2,subcategory2
can it be inserted like this:
(category1)subcategory1,subcategory2,(category2)subcategory1,subcategory2,

    extract($_POST);
    $n = count($name);
    $ct = $_POST['cat'];
    $sct = $_POST['subcat'];
    $result = array_merge((array)$ct, (array)$sct);
    $category = implode(",", $result); 
    for ($i = 0; $i < $n; $i++) {
        $query = 'INSERT INTO places (name, address, city, contact, tel, mail, products, category) VALUES (\'' . $name[$i] . '\', \'' . $adr[$i] . '\', \'' . $city[$i] . '\', \'' . $con[$i] . '\', \'' . $tel[$i] . '\', \'' . $mail[$i] . '\', \'' . $prod[$i] . '\', \'' . $category. '\')';}
share|improve this question
 
What does your db structure look like? What exactly is the problem? You should easily be able to either iterate over the arrays or implode needed results –  kingkero Dec 4 '13 at 19:46
 
I updated question with php code. I know its really bad but I need some directions –  Yesian _ Dec 4 '13 at 19:52
add comment

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.