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

sinan
  • 55
  • 1
  • 8

3 Answers3

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.

Hidde
  • 11,493
  • 8
  • 43
  • 68
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')
Skewled
  • 783
  • 4
  • 12
  • is wrong because, urunno must be unique i updated my question. – sinan Apr 16 '14 at 16:49
  • 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. – Skewled Apr 16 '14 at 16:59
  • 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 ? – sinan Apr 17 '14 at 15:07
  • if i m select all checkbox not making error. else i check two or one checkbox code not running, where is the problem ? – sinan Apr 17 '14 at 16:22
  • because you have to pass a value even if it is 0 – Skewled Apr 17 '14 at 16:42
  • http://s13.postimg.org/lqht3g9yf/img1.png my save form and my table http://s8.postimg.org/6zazuoy51/img2.png – sinan Apr 17 '14 at 17:07
  • thank you. i writed urunno 2 value 9 but form saved is urunid 3 value 9 :)) db must be : urunid :2 value:9 ---- urunid:3 value:6 my problem is very hardly. thanks for reply me. – sinan Apr 17 '14 at 17:16
  • So your table had the incorrect column ID is what you are saying? So you are ok and it is working as intended? – Skewled Apr 17 '14 at 17:29
  • no is wrong. http://s13.postimg.org/lqht3g9yf/img1.png im selected urunno:2 and 3. and . i write urunno 2 value 9 and urunno 3 value 6 okey. and i saved. after i check my table http://s8.postimg.org/6zazuoy51/img2.png is wrong urunid3 value:9 . must be correct value 6. and urunno2 value 0 is correct value 9 .... understand me ? – sinan Apr 17 '14 at 17:54
  • @sinan please see the updated answer, this resolves the issue that you were having. I will leave the error checking to you, check that the array has data in it or you will have an undefined index. – Skewled Apr 17 '14 at 19:25
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

        }
}?>
Anand Somasekhar
  • 596
  • 1
  • 11
  • 20