5 Replies - 1585 Views - Last Post: 10 November 2014 - 08:25 PM Rate Topic: -----

#1 suha_91   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 09-November 14

how to insert data from multiple checkbox values into database?

Posted 09 November 2014 - 11:40 PM

Hi, all. Currently i am facing some problem with my coding. I am used this code to insert data from multiple checkbox values into database. but its still not happen anything. Sorry, i am new to php. Please can anyone help me on this? I would be more appreciate if i get to solve this problem. Thank you so much. :)

<script type="text/javascript">
var currentEnabled = null;
function enableElement(elem) {
if (currentEnabled) {
currentEnabled.disabled = true;
}
elem.disabled = false;
currentEnabled = elem;
}
</script>
<body background="images/form.jpg" bgcolour="#CC66FF">
<?php 
error_reporting(0);
session_start();
include('includes/header.html');
include('dbcon.php');
// Get values from form
if(isset($_POST['submit'])) {
$fdate = date('Y-m-d');
$ref_no = $_POST['ref_no'];
$from = $_POST['from'];
$copy = $_POST['copy'];
$description = implode(',',$_POST['description']);
$buyer = $_POST['buyer'];
$ref = $_POST['ref'];
$potential_annual = $_POST['potential_annual'];
$target_price = $_POST['target_price'];
$market_app = $_POST['market_app'];
$instruct = implode(',', $_POST['instruct']);
$comp_colour = $_POST['comp_colour'];
$pattern = implode(',', $_POST['pattern']);
$sole_colour = $_POST['sole_colour'];
$calender_pattern = $_POST['calender_pattern'];
$heel_type = $_POST['heel_type'];
$heel_colour = implode(',', $_POST['heel_colour']);
$moul_sole = $_POST['moul_sole'];
$top_fox = $_POST['top_fox'];
$top_colour = implode(',', $_POST['top_colour']);
$bottom_fox = $_POST['bottom_fox'];
$bottom_colour = implode(',',$_POST['bottom_colour']);
$last_type = $_POST['last_type'];
$boots_height = $_POST['boots_height'];
$size = $_POST['size'];
$total_pairs = $_POST['total_pairs'];
$date_required = $_POST['date_required'];
$lin_material = $_POST['lin_material'];
$cuff_material = implode(',', $_POST['cuff_material']);
$lace_colour = implode(',',$_POST['lace_colour']);
$brand = $_POST['brand'];
$colour = $_POST['colour'];
$lacquer_finishing = implode(',', $_POST['lacquer_finishing']);
$safety = implode(',', $_POST['safety']);
$features = implode(',', $_POST['features']);
$attachment = $_POST['attachment'];
$remarks = $_POST['remarks'];
$department = $_POST['department'];
$pair = $_POST['pair'];
$request_by = $_POST['request_by'];
$approval_status = $_POST['approval_status'];
$query = "INSERT INTO requisition ( fdate, ref_no, from, copy, description, buyer, ref, potential_annual, target_price, market_app, instruct, comp_colour, pattern, sole_colour, calender_pattern, heel_type, heel_colour, moul_sole, top_fox, top_colour, bottom_fox, bottom_colour, last_type, boots_height, size, total_pairs, date_required, lin_material, cuff_material, lace_colour, brand, colour, lacquer_finishing, safety, features, attachment, remarks, dept_name, pair, request_by, approval_status ) VALUES ('$fdate', '$ref_no', '$from', '$copy', '$description', '$buyer', '$ref', '$potential_annual', '$target_price', '$market_app', '$instruct', '$comp_colour', '$pattern', '$sole_colour', '$calender_pattern', '$heel_type', '$heel_colour', '$moul_sole', '$top_fox', '$top_colour', '$bottom_fox', '$bottom_colour', '$last_type', '$boots_height', '$size', '$total_pairs', '$date_required', '$lin_material', '$cuff_material', '$lace_colour', '$brand', '$colour', '$lacquer_finishing', '$safety', '$features', '$attachment', '$remarks', '$department', '$pair', '$request_by', 'pending')";
 $result=mysql_query($sql);
        if($result)
        {
             echo "Requisition Successfully Added!";
        }
        else
        {
             echo "failed";
        }
    }
?>
<table width="1028" height="1181" border="2" align="center" cellpadding="2" cellspacing="3" cols="2" bgcolor="#C0C0C0">
  <td width="1012" height="20"><center><font color="#0000FF"><strong>SAMPLE REQUISITION FORM</strong></font></center>
  <tr>
<form action="form.php" method="post">
<br>
<tr>
                <td height="20"><b><strong>Filed Date :</span></div>
                </strong>
 <input name="fdate" type="text" id="day" value="<?php echo date('Y-m-d'); ?>" size="10" maxlength="15"  readonly />
                <b> <strong>Reference No :</strong><input name="ref_no" type="text" id="ref_no" value="" size="10" maxlength="10" /></td>
          </tr>
<tr>
<td height="20"><strong><b>From :</strong>
  <input type="text" name="from" size="28" value=""/>  
   <b> <strong>Copy :</strong>
   <input type="text" name="copy" size="28" value=""/>
   <b> <strong>To : <em>Product Department</em></strong></td></tr>
  <tr>
<td height="30"><b><strong>Description :
      </strong>
  <label><br>
      <input name = "description[]" type = "checkbox" value ="safety" onclick="enableElement(this.form.elements['inp1']);"  />
      SAFETY </label>
    <label> 
      <input name = "description[]" type = "checkbox" value = "work" onclick="enableElement(this.form.elements['inp2']);" />
      WORK </label>
    <label> 
      <input name= "description[]" type ="checkbox" value="yachting" onclick="enableElement(this.form.elements['inp3']);"/>
      YACHTING </label>
    <label> 
      <input name="description[]" type ="checkbox" value="golf" onclick="enableElement(this.form.elements['inp4']);"/>
      GOLF </label>
    <label> 
      <input name="description[]" type = "checkbox" value="hiking" onclick="enableElement(this.form.elements['inp5']);"/>
      HIKING </label>
    <label> 
      <input name="description[]" type="checkbox" value="ladies" onclick="enableElement(this.form.elements['inp6']);"/>
      LADIES</label>
      <input name="description[]" type="checkbox" value="children" onclick="enableElement(this.form.elements['inp7']);"/>      
      CHILDREN</label>
    <label> 
      <input name="description[]" type="checkbox" value="hunting" onclick="enableElement(this.form.elements['inp8']);"/>
      HUNTING </label>
    <br>
    <label> 
      <input name="description[]" type="checkbox" value="wader" onclick="enableElement(this.form.elements['inp9']);"/>
      WADER </label>
      <label>
    <input type="checkbox" name="description[]" value ="others" onclick="enableElement(this.form.elements['inp10']);" />OTHERS :
<input type="text" name="inp10" size="30" disabled="disabled"/>
</label>
</td></tr>
<tr>
<td height "30"><strong><b>Buyer (Company Address/ Country) :</strong>
  <textarea name="buyer" type="text" size="30" maxlength="30"></textarea>
  <strong><b> Reference :</strong>
  <input name="ref" type="text"  value="" size="30" maxlength="30"  />
  </tr>
<tr><td height="20"><strong><b>Potential Annual Volume :</strong>
  <input type="text" name="potential_annual" size="20" value=""/>
      <b><strong>Target Price :</strong>
      <input type="text" name="target_price" size="20" value=""/> 
  <b> <strong>Market Application :</strong>  <input type="text" name="market_app" size="30" value=""/></td>
  </tr>
<tr>
<td height="57"><b><strong>Instruction :
</strong>
  <label><br>
  <input name = "instruct[]" type = "checkbox" value ="safety" onclick="enableElement(this.form.elements['inp1']);" />
      REFER FAX/ LETTER ATTACHED FOR FULL 
      DETAILS. </label> 
  <label>
      <input name="instruct[]" type="checkbox" value="duplicate" onclick="enableElement(this.form.elements['inp2']);"/> 
      DUPLICATE 
      <input name="instruct[]" type="checkbox" value="new" onclick="enableElement(this.form.elements['inp3']);"/>
NEW PRODUCT DEVELOPMENT</label>
   <label><br>
  </label>
  <label> 
     <input name="instruct[]" type="checkbox" value="art" onclick="enableElement(this.form.elements['inp4']);"/>
      MODIFICATION OF ART/MODEL : 
      <input type="text" name="inp4" size="30" disabled="disabled"/>
    </label>
  <label><br>
        <input name="instruct[]" type="checkbox" value="other" onclick="enableElement(this.form.elements['inp5']);"/>
      OTHERS :  <input type="text" name="inp5" size="30" disabled="disabled"/>
  </label></td>
</tr>
 <tr><td height="20"><center><font color="#0000FF"> 
   <strong><em>UPPER </em></strong></font>
 </center></td></tr> 
<tr>
  <td height="58"><b><strong>Compound / Colour :</strong>
    <input type="text" name="comp_colour" size="30" value=""/>  <br><b><strong>Pattern :  
        </strong>
    <label>
      <input name="pattern[]" type="checkbox" value="kid" onclick="enableElement(this.form.elements['inp1']);"/>KID </label>
      <label> 
        <input name="pattern[]" type="checkbox" value="nappa" onclick="enableElement(this.form.elements['inp2']);"/>NAPPA</label>
      <label> 
        <input name="pattern[]" type="checkbox" value="bull" onclick="enableElement(this.form.elements['inp3']);"/>BULL</label>
      <label> 
        <input name="pattern[]" type="checkbox" value="plain" onclick="enableElement(this.form.elements['inp4']);"/>PLAIN </label>
       <label>
         <input name="pattern[]" type="checkbox" value="others" onclick="enableElement(this.form.elements['inp5']);"/>
         OTHERS (Please Specify): 
         <input type="text" name="inp5" size="15" disabled="disabled"/>
       </label></td>
    </tr>
      <tr><td height="20"><center><font color="#0000FF"> 
        <strong><em>SOLE </em></strong></font>
      </center></td></tr>
    <tr>
  <td height="20"><b><strong>Sole Compound / Colour :</strong>
    <input type="text" name="sole_colour" size="30" value=""/>
<b> <strong>Calender Roll/Pattern : </strong>
<input type ="text" name="calender_pattern" size="30" value=""/></td></tr>
<tr>
  <td height="20"><strong><b>Heel Types :</strong>
    <input type="text" name="heel_type" size="30" value=""/>
<br><b><strong>Colour :</strong>
<label>
  <input name="heel_colour[]" type="checkbox" value="same_as_sole" onclick="enableElement(this.form.elements['inp1']);"/>Same as Sole </label>
<label>
<input name="heel_colour[]" type="checkbox" value="other" onclick="enableElement(this.form.elements['inp2']);" />OTHERS (Please Specify) : 
<input type="text" name="inp2" size="28" disabled="disabled"/>
</label></td></tr>
<tr>
<td height="20"><b><strong>Moulded Sole Type :</strong>  <input type="text" name="moul_sole" size="30" value=""/> </td>
</tr>
<tr> 
<td height="20"><b><strong>Top Foxing Pattern/Roll :</strong>
  <input type="text" name="top_fox" size="30" value=""/>
<br><b> <strong> Colour: </strong>
<label>
<input name="top_colour[]" type="checkbox" value="same_as_upper" onclick="enableElement(this.form.elements['inp1']);"/>Same as Upper</label>
<label>
<input name="top_colour[]" type="checkbox" value="other" onclick="enableElement(this.form.elements['inp2']);" />OTHERS (Please Specify) : <input type="text" name="inp2" size="28" disabled="disabled"/></label>
</td>
</tr>
<tr>
<td height="20"><strong><b>Bottom Foxing Pattern/Roll :</strong>
  <input type="text" name="bottom_fox" size="30" value=""/>
<br><b><strong>Colour : 
</strong>
<label>
  <input name="bottom_colour[]" type="checkbox" value="upper/sole" onclick="enableElement(this.form.elements['inp1']);"/> Same as Upper/Sole</label>
<label>
<input name="bottom_colour[]" type="checkbox" value="other" onclick="enableElement(this.form.elements['inp2']);"/> 
OTHERS (Please Specify) : 
<input type="text" name="inp2" size="28" value="" disabled="disabled"/></label> </td>
<tr>
<td height="20"><b><strong>Last Type :</strong>
  <input type="text" name="last_type" size="20" value=""/>
<b> <strong>Boots Height :</strong>
<input type="text" name="height" size="10" value=""/>
<b> <strong>Size :</strong>
<input type="text" name="size" size="10" value=""/> 
 <b> <strong>Total Pairs : </strong>
 <input type="text" name="total_pairs" size="10" value=""/>
<br><b><strong>Date Required :</strong> <input name="date_required" type="date" value=""/></td>
</tr>
<tr>
                <td height="20"><b><strong>Lining Material/ Colour :</strong> </span></div>
                <input name="lin_material" type="text"  value="" size="30"/></td>
<tr>
<td height="20"><b><strong>Cuff Material/Colour :
</strong>
  <label>
<input name="cuff_material[]" type="checkbox" value="n/a" onclick="enableElement(this.form.elements['inp1']);"/> N/A </label>
<label>
<input name="cuff_material[]" type="checkbox" value="" onclick="enableElement(this.form.elements['inp2']);"/><input type="text" name="inp2" size="30" value="" disabled="disabled"/></label>
</td></tr>
<tr>
<td height="20"><b><strong>Lace/Colour :</strong>
<label>
  <input name="lace_colour[]" type="checkbox" value="n/a" onclick="enableElement(this.form.elements['inp1']);"/> N/A </label>
<label>
<input name="lace_colour[]" type="checkbox" value="" onclick="enableElement(this.form.elements['inp2']);"/><input type="text" name="inp2" size="30" value="" disabled="disabled"/></label>
</td></tr>
  <tr>
    <td height="20"><b><strong>Brand :</strong>   <input type="text" name="brand" size="30" value=""/> 
      <b><strong>Colour :</strong>   <input type="text" name="color" size="30" value=""/></td>
    </tr>
<tr>
<td height="20"><b><strong>Lacquer Finishing :</strong>
<label> 
  <input name = "lacquer_finishing[]" type = "checkbox" value ="gloss" onclick="enableElement(this.form.elements['inp1']);"/>
  GLOSS </label>
    <label> 
      <input name = "lacquer_finishing[]" type = "checkbox" value = "dull" onclick="enableElement(this.form.elements['inp2']);"/>
      DULL </label>
    </td></tr>
<tr>
<td height="20"><b><strong>Safety Standards to Confirm :</strong>
<label><input name="safety[]" type="checkbox" value="n/a" onclick="enableElement(this.form.elements['inp1']);"/>N/A</label>
<label><input name="safety[]" type="checkbox" value="en-" onclick="enableElement(this.form.elements['inp2']);"/>EN-
<input type="text" name="inp2" disabled="disabled" size="10"></label>
<label><input name="safety[]" type="checkbox" value="csa" onclick="enableElement(this.form.elements['inp3']);"/>CSA</label>
<label><input name="safety[]" type="checkbox" value="astm" onclick="enableElement(this.form.elements['inp4']);"/>ASTM</label>
<label><input name="safety[]" type="checkbox" value="sirim" onclick="enableElement(this.form.elements['inp5']);"/>SIRIM</label>
<label>
<input name="safety[]" type="checkbox" value="other" onclick="enableElement(this.form.elements['inp6']);"/>Others:<input type="text" name="inp6" size="10" disabled="disabled"/>
</label></td>
</tr>
<tr>
<td height="75"><b><strong>NotWithStanding 15.0 Above, Other Features Required :
</strong>
  <label><br>
<input name="features[]" type="checkbox" value="none" onclick="enableElement(this.form.elements['inp1']);"/>NONE</label>
<label>
<input name="features[]" type="checkbox" value="steel_toe_cap" onclick="enableElement(this.form.elements['inp2']);"/>STEEL TOE CAP</label>
<label>
<input name="features[]" type="checkbox" value="steel_mid_sole" onclick="enableElement(this.form.elements['inp3']);"/>STEEL MID SOLE</label>
<label>
<input name="features[]" type="checkbox" value="steel_shank" onclick="enableElement(this.form.elements['inp4']);"/>STEEL SHANK</label>
<label>
<input name="features[]" type="checkbox" value="anti_static" onclick="enableElement(this.form.elements['inp5']);"/>ANTI-STATIC</label>
<label>
  <input name="features[]" type="checkbox" value="fire_retardant" onclick="enableElement(this.form.elements['inp6']);"/>
  FIRE RETARDANT</label>
<label>
  <br>
  <input name="features[]" type="checkbox" value="oil_resistant_sole_upper" onclick="enableElement(this.form.elements['inp7']);"/>OIL RESISTANT SOLE & UPPER</label>
<label>
  <input name="features[]" type="checkbox" value="chainsaw_protection" onclick="enableElement(this.form.elements['inp8']);"/>
CHAINSAW PROTECTION</label>
<label>
  <input name="features[]" type="checkbox" value="anti_slip_sole" onclick="enableElement(this.form.elements['inp9']);"/>
  ANTI-SLIP SOLE</label>
<label>
  <input name="features[]" type="checkbox" value="electric_shock_resistant" onclick="enableElement(this.form.elements['inp10']);"/>ELECTRIC SHOCK RESISTANT</label>
<label>
  <br>
  <input name="features[]" type="checkbox" value="other" onclick="enableElement(this.form.elements['inp11']);"/>
  OTHERS (please specify) :
<input type="text" name="inp11" size="38" disabled="disabled"/></label>
</td></tr>
<tr>
<td height="20"><b><strong>Sketches/Drawings Attached :
</strong>
  <label> 
      <input name = "attachment[]" type = "radio" value ="yes" checked/>YES</label>
      <label>
      <input name="attachment[]" type="radio" value="no"/>NO</label>
</td>      
</tr>
<tr>
  <td height="45"><b><strong>Other Description/Remarks :</strong>  <textarea name="remarks" type="text" rows="5" cols="40"></textarea></td>
</tr>
<tr>
<td height="20"><center><font color="#0000FF"><em><strong>#Sample Distribution</strong></em></font>
</center></td> 
</tr>
<tr>
<td height="20"><strong><strong>TO :</strong><select name = "department" >
  <option selected> Department </option>
  <option> Marketing Department</option>
  <option> Technical Department </option>
  <option> Product Development Section</option>
</select> 
  <strong><b> Pairs :</strong>
  <input type="text" name="pair" size="10"/>
  <br>
<strong><b> Requested By :</strong>  <input type="text" name="request_by" size="30" value=""/></td>
</tr>
<tr>
<tr>
</tr>
<tr>
<td>
<label>
<input name="notify" type="checkbox" value="notify" checked/> 
<em><strong>Send notification for approval. </strong></em><strong></strong></label>
(Make sure it's always checked!)</td>
</tr>
<tr>
<td height="30"><center><input type = "submit" value = "Submit Requisition"  name="Submit" /></center></td>
<input type="hidden" name="submitted" />
</tr>
</form>
</tr>
</table>
<?php
echo "<br>";echo "<br>";echo "<br>";echo "<br>";echo "<br>";echo "<br>";
include('includes/footer.html');
?>
</body>
</html>



Is This A Good Question/Topic? 0
  • +

Replies To: how to insert data from multiple checkbox values into database?

#2 CTphpnwb   User is online

  • D.I.C Lover
  • member icon

Reputation: 3798
  • View blog
  • Posts: 13,753
  • Joined: 08-August 08

Re: how to insert data from multiple checkbox values into database?

Posted 10 November 2014 - 07:04 AM

You need to use better tutorials. For example:
If a tutorial has mysql_ anywhere in it, ignore the tutorial completely. Only use PDO or MySQLi prepared statements.
$x = $_POST['x']; is bad. In this case, it leaves you with about 30 lines of unnecessary code.
You need to use an indent style to help make your code readable.
Using tables for formatting is bad.
Beginners should NOT mix HTML and PHP in the same file.
Was This Post Helpful? 1
  • +
  • -

#3 suha_91   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 09-November 14

Re: how to insert data from multiple checkbox values into database?

Posted 10 November 2014 - 05:55 PM

Hello, CTphpnwb . Thank you for your reply. Now i gt changed my insert query as like this. but i still facing the same problem. its nothing happen to database and it also did not show any error message. I don't know what is the problem is might be. Is there any other solutions for this? Thank you for your time. :)

<?php
define('DEBUG', true);  // Change to false in production to prevent errors from showing to visitors!!
require_once("dbcon.php");
// Get values from form
if(isset($_POST['submit']))
{
    $row = array(
        date('Y-m-d'),
        $_POST['ref_no'],
        $_POST['from'],
        $_POST['copy'],
        implode(',',$_POST['description']),
        $_POST['buyer'],
        $_POST['ref'],
        $_POST['potential_annual'],
        $_POST['target_price'],
        $_POST['market_app'],
        implode(',', $_POST['instruct']),
        $_POST['comp_colour'],
        implode(',', $_POST['pattern']),
        $_POST['sole_colour'],
        $_POST['calender_pattern'],
        $_POST['heel_type'],
        implode(',', $_POST['heel_colour']),
        $_POST['moul_sole'],
        $_POST['top_fox'],
        implode(',', $_POST['top_colour']),
        $_POST['bottom_fox'],
        implode(',',$_POST['bottom_colour']),
        $_POST['last_type'],
        $_POST['boots_height'],
        $_POST['size'],
        $_POST['total_pairs'],
        $_POST['date_required'],
        $_POST['lin_material'],
        implode(',', $_POST['cuff_material']),
        implode(',',$_POST['lace_colour']),
        $_POST['brand'],
        $_POST['colour'],
        implode(',', $_POST['lacquer_finishing']),
        implode(',', $_POST['safety']),
        implode(',', $_POST['features']),
        $_POST['attachment'],
        $_POST['remarks'],
        $_POST['department'],
        $_POST['pair'],
        $_POST['request_by'],
        'pending',
    );
    // Make N placeholders (the ? symbol is replaced by prepare with your value), where N == number of fields in the row to insert
    $tokens = array_fill('?', count($row));
    $query = 'INSERT INTO requisition ( fdate, ref_no, from, copy, description, buyer, ref, potential_annual, target_price, market_app, instruct, comp_colour, pattern, sole_colour, calender_pattern, heel_type, heel_colour, moul_sole, top_fox, top_colour, bottom_fox, bottom_colour, last_type, boots_height, size, total_pairs, date_required, lin_material, cuff_material, lace_colour, brand, colour, lacquer_finishing, safety, features, attachment, remarks, dept_name, pair, request_by, approval_status ) VALUES ('.implode(',', $tokens).')';
    $result = db_query($sql, $row);
    if($result)
    {
         echo "Requisition Successfully Added!";
    }
    else
    {
        if(DEBUG) {
            echo "<b>MySQL error:</b><br/>";
            print_r($pdo->errorInfo());
        } else {
            echo "<b>Sorry, we could not process your request</b><br/>";
        }
    }
}
?>


Was This Post Helpful? 0
  • +
  • -

#4 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2585
  • View blog
  • Posts: 10,355
  • Joined: 03-December 12

Re: how to insert data from multiple checkbox values into database?

Posted 10 November 2014 - 06:06 PM

What is in dbcon.php? db_query is not a standard function. If using PDO it should resemble $pdo->prepare( $query );
Was This Post Helpful? 0
  • +
  • -

#5 suha_91   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 09-November 14

Re: how to insert data from multiple checkbox values into database?

Posted 10 November 2014 - 06:35 PM

Hi,astonecipher.Thank you for your reply. dbcon.php is database connection where i connect my database with php. If db_query not a standard fuction, can i get a better suggestion to replace with it? Thank you. :)
Was This Post Helpful? 0
  • +
  • -

#6 CTphpnwb   User is online

  • D.I.C Lover
  • member icon

Reputation: 3798
  • View blog
  • Posts: 13,753
  • Joined: 08-August 08

Re: how to insert data from multiple checkbox values into database?

Posted 10 November 2014 - 08:25 PM

In post # 2 I provided three links. You should read and study each of them, as well as this one.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1