how Do I Insert multiple values of Checkbox & textbox corresponding to it in database

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • priya555
    New Member
    • Apr 2016
    • 5

    how Do I Insert multiple values of Checkbox & textbox corresponding to it in database

    Code:
    <?php
    				    include("database.php");
    $query2="select prodcode,prodname,id from product";
    $result2=mysqli_query($con,$query2);
    echo"<table border='2' align='center' cellpadding='0' cellspacing='0' style='width:50%'>";
    echo"<tr>";
    echo"<thead>";
    echo"<th>"."ProductCode"."</th>";
    echo"<th>"."ProductName"."</th>";
    echo"<th>"."Quantity"."</th>";
    echo"</thead>";
    				while($row=mysqli_fetch_array($result2,MYSQL_ASSOC))
    {
    echo"<tr>";
    echo"<td>"."<input type='checkbox' id='chk' value='".$row['prodcode']."'name='checkbox1[]'/>".$row['prodcode']."</td>";
    echo"<td>".$row['prodname']."</td>";
    echo"<td>"."<input type='text' maxlength='4' id='quan' name='quantity[]'class ='quantity'/>"."</td>";
    echo"</tr>";
    }
    ?>
    <?php
    include("database.php");
    if(isset($_POST["btnsubmit"]))
    {
    $check=$_POST['checkbox1'];
    $quantity=$_POST["quantity"];
    
    for ($i=0; $i<sizeof($check);$i++) 
    {
    $query="INSERT INTO elevconfigdetails(id,prodcode,quantity)
    VALUES ('$id','".$check[$i]."','".$quantity."' )";
    mysqli_query($con,$query) or die ('Error updating database'.mysqli_error($con));
    echo "</br>Record is inserted.";
    }

    when user click on submit button checkbox values(which is checked) and textbox values should be insert into mysql table row by row.i am successful to insert multiple checkbox values but the related textbox value is not inserted into database.
    how i can fix this problem??
    any help plzzzzzzz??
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    $quantity is an array, but you treat it as string, which obviously won’t work.

    Comment

    • priya555
      New Member
      • Apr 2016
      • 5

      #3
      @Dormilich ya the name attribute of textbox is an array..i want to insert multiple textbox values into database

      Comment

      • priya555
        New Member
        • Apr 2016
        • 5

        #4
        i am having multiple checkboxes that are coming from database and i have given a field(textbox) with every checkbox...now suppose 10 checkboxes are coming from database..now i want to insert values in database only of those checkboxes which are checked along with their textbox values.in textbox user insert the quantity.when i click on submit button the checkbox values are getting inserted right but the corresponding textbox values are not getting inserted..help me to solve this problem

        Comment

        • Dormilich
          Recognized Expert Expert
          • Aug 2008
          • 8694

          #5
          the problem is that there is no relation between the checkboxes and the text boxes. this is the first problem to solve.

          I’d assign the text boxes using the product id. i.e.
          Code:
          <input type="number" min="0" name="qantity[{product-id}]" value="0">
          where {product-id} is the product id.

          then you can do in PHP
          Code:
          // using PDO for insert
          foreach ($products as $id) {
              $stmt->execute([
                  ':id' => $id, 
                  ':quantity' => $quantities[$id],
              ]);
          }

          Comment

          • priya555
            New Member
            • Apr 2016
            • 5

            #6
            @Dormilich thanks for reply..i am new in PHP.so dont know about PDO...please tell me your suggestion in (mysqli) so it is helpfull me to understand..how i can write the insert query.??
            means the id field auto-increment and the last_id field is need to repeated.so tell me that..

            Comment

            • Dormilich
              Recognized Expert Expert
              • Aug 2008
              • 8694

              #7
              I find mysqli prepared statements awfully complicated, so I stick to PDO.

              and for PDO you can check out the documentation: http://php.net/pdo

              Comment

              • priya555
                New Member
                • Apr 2016
                • 5

                #8
                okk.thank you Dormilich...... ....

                Comment

                Working...