1

my form data im listed my table sample 10 records.

<td class='table-checkbox'><input type="checkbox" name="product[]" value="1" class='selectable-checkbox'></td>
    <td><div class="input-mini">
    <input type="text" id="spinnn"  name="quantity[]" value="5" class='spinner'/>
    </div>
    </td>
    </tr>

..... .....

my php code

$carino = $_POST['carino']; 
$quantity = $_POST['quantity']; 
$product = $_POST['product'];
foreach($product as $product){ 
foreach($quantity as $quantity ){ 
$sql = "INSERT INTO mytable (product,quantity,cno) VALUES ('$product','$quantity','$carino')";
mysql_query($sql);
}}

i want to be insert this data my table. but my foreach is wrong how can i do ?

product is unique

my table

product - quantity - cno
1            5        2   
2            10       2

http://pastie.org/private/nwrrinnxlrumt6p3kuyq#11,13-14,19

3 Answers 3

1

This:

<?php 

if(isset($_POST['submit'])) {

    $carino = "2"; 

    $adet = $_POST['adet'];
    $urunno = $_POST['urunno'];
    $total = count($_POST['adet']);

    echo '<hr>'; 

    foreach ($urunno as $checked)
            {
            $value = $checked - 1;
                echo "Value of Urunno: $checked:  Adet: $adet[$value] <br>";
                echo "INSERT INTO member_interests
                    (`urun`,`adet`,'carino')
                VALUES
                    ('$checked','$adet[$value]','$carino')<br>";
            }
    }
?>

<tr>
<form method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
<td class='table-checkbox'><input type="checkbox" name="urunno[]" value="1">Product One</td>
<input type="text" id="spinnn"  name="adet[]" class='spinner'/>
<td class='table-checkbox'><input type="checkbox" name="urunno[]" value="2">Product Two</td>
<input type="text" id="spinnn"  name="adet[]" class='spinner'/>
<td class='table-checkbox'><input type="checkbox" name="urunno[]" value="3">Product Three</td>
<input type="text" id="spinnn"  name="adet[]" class='spinner'/>
<td><div class="input-mini">
<input type="submit" name="submit" value="run" />
</form>
</div>
</td>
</tr>
</tbody>
</table>
</div>

Output:

Value of Urunno: 2: Adet: 2 
INSERT INTO member_interests (`urun`,`adet`,'carino') VALUES ('2','2','2')
Value of Urunno: 3: Adet: 3 
INSERT INTO member_interests (`urun`,`adet`,'carino') VALUES ('3','3','2')
Sign up to request clarification or add additional context in comments.

10 Comments

is wrong because, urunno must be unique i updated my question.
The is an example of working with the data, you had nested foreach loops and I wanted you to visually see what was happening. I changed it to a for loop for you, to show you what the result would be.
hello, i find a bug. e.g im listed two products. i selected two products no problem. but im select firstly product not problem. but im select only secondly product my adet valuse is 0 but urun id correct how can resolve thiş bug ?
if i m select all checkbox not making error. else i check two or one checkbox code not running, where is the problem ?
because you have to pass a value even if it is 0
|
1

The logic can be as follows (think of the code yourself, or search on the internet):

  • Make sure that all POST arrays are the same length (they should be)
  • Loop over the count from 0 to the length of the arrays
  • Insert the value at that point in each of the arrays into the database.

A quick tip: you are very susceptible to SQL injection. If this is production code, either use prepared queries, or use a PHP database wrapper like PDO to do it for you. The mysql_... functions are deprecated.

Comments

1
    Try this one  
      <?php
        //add the database connection
        ?>
        <form name="myform" action="">
        <table>
        <?php 
        $length=//how many time below check box repeat in this page
        for($i=0;$i<$length;$i++){?>
        <tr>
        <td class='table-checkbox'>
        <input type="checkbox" name="urunno[]" value="<?php echo $i;?>" class='selectable-checkbox'>
        </td>
        <td>c1k</td>
        <td>2147483647</td>
        <td>520</td>
        <td>435345345 A.Ş.</td>
        <td>
        <div class="input-mini">
        <input type="text" id="spinnn"  name="adet<?php echo $i;?>" class='spinner'/>
        </div>
        </td>
        </tr>
        <?php
        }?>
        </table>
        <input type="submit" name="submit" value="Submit">
        </form>
        <?php
        if(isset($_post['submit']))
        {
        $carino = $_POST['carino'];  //here this element not present in question, i am also follow this
        $adet = $_POST['adet']; 
        $urunno = $_POST['urunno'];
        $length=sizeof($urunno);
        for($i=0;$i<$length;$i++)
        {
          $urun1=$urunno[$i];
          $adet1=$adet[$urun1];
          $sql = "INSERT INTO table (urunno,adet,cno) VALUES ('$urun1','$adet1','$carino')";
          mysql_query($sql);

          //the above code insert sql work in every time //otherwise use batch insert
           //refrer this link http://stackoverflow.com/questions/12960569/mysql-bulk-insert-via-php

        }
}?>

2 Comments

if i m select all checkbox not making error. else i check two or one checkbox code not running, where is the problem ?
please check it this now ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.