yaqoob
yaqoob

Reputation: 1062

Inserting multiple rows in array inserts only 1 record

i'm having problems with multiple inserting records into database. When i submit the form, only 1 record gets inserted. I'm not iterating the post data that's i know for sure. please help!

PHP CODE:

        $cid = $_POST['item_cid'];
        $pcode = $_POST['item_code'];
        $pname = $_POST['item_name'];
        $pprice = $_POST['item_price'];
        $pqty = $_POST['item_qty'];

for ($i = 0; $i < count($cid); $i++) {

        $cid = mysql_real_escape_string($cid[$i]);
        $pcode = mysql_real_escape_string($pcode[$i]);
        $pname = mysql_real_escape_string($pname[$i]);
        $pprice = mysql_real_escape_string($pprice[$i]);
        $pqty = mysql_real_escape_string($pqty[$i]);

mysqli_query($connection,"INSERT INTO orders (cid, ordprod_code, ordprod_name, ordprod_price, ordprod_qty, ord_date) VALUES ('$cid', '$pcode', '$pname', '$pprice', '$pqty', '')");
    }
echo 'Records inserted...'; 

}

?>

HTML CODE:

<input type="hidden" name="item_cid[0]" value="22795" />
<input type="hidden" name="item_code[0]" value="LS-985" />
<input type="hidden" name="item_name[0]" value="some Product title 1" />
<input type="hidden" name="item_price[0]" value="9999" />
<input type="hidden" name="item_qty[0]" value="2" />

<input type="hidden" name="item_cid[1]" value="22795" />
<input type="hidden" name="item_code[1]" value="SL-055" />
<input type="hidden" name="item_name[1]" value="some Product title x12" />
<input type="hidden" name="item_price[1]" value="9390" />
<input type="hidden" name="item_qty[1]" value="1" />

<input type="hidden" name="item_cid[2]" value="22795" />
<input type="hidden" name="item_code[2]" value="WR-656" />
<input type="hidden" name="item_name[2]" value="some Product title 392" />
<input type="hidden" name="item_price[2]" value="10000" />
<input type="hidden" name="item_qty[2]" value="6" />

and so on

I also tried the name arrays without the numbered sequence like this

<input type="hidden" name="item_cid[]" value="22795" />
<input type="hidden" name="item_code[]" value="WR-656" />
<input type="hidden" name="item_name[]" value="Some Title" />
<input type="hidden" name="item_price[]" value="10000" />
<input type="hidden" name="item_qty[]" value="6" />

but the result was same :(

Upvotes: 2

Views: 353

Answers (3)

nitigyan
nitigyan

Reputation: 494

When you do this $cid = mysql_real_escape_string($cid[$i]); you are inherently converting an array ($cid[]) into single value ($cid) by assigning array element to that variable itself. So when the first loop is complete, length of $cid will already become 1 always. That's why your loop only run once and insert one row. You should use different temporary variable.

Upvotes: 1

Narendrasingh Sisodia
Narendrasingh Sisodia

Reputation: 21437

You were mixing mysql and mysqli that might be causing an error

You can also use mysqli_real_escape_string within array_map function as

    $cid = array_map('mysqli_real_escape_string',$_POST['item_cid']);
    $pcode = array_map('mysqli_real_escape_string',$_POST['item_code']);
    $pname = array_map('mysqli_real_escape_string',$_POST['item_name']);
    $pprice = array_map('mysqli_real_escape_string',$_POST['item_price']);
    $pqty = array_map('mysqli_real_escape_string',$_POST['item_qty']);

you were using

   $cid = mysql_real_escape_string($_POST['item_cid']);
              ^^^

    foreach($_POST['item_cid'] as $key => $value) {

      $cid = mysqli_real_escape_string($connection,$value);
    $pcode = mysqli_real_escape_string($connection,$_POST['item_code'][$key]);
    $pname = mysqli_real_escape_string($connection,$_POST['item_name'][$key]);
    $pprice = mysqli_real_escape_string($connection,$_POST['item_price'][$key]);
    $pqty = mysqli_real_escape_string($connection,$_POST['item_qty'][$key]);  

      $sql = "INSERT INTO orders (cid, ordprod_code, ordprod_name, ordprod_price, ordprod_qty, ord_date) VALUES ('$value', '$pcode', '$pname', '$pprice', '$pqty', '')";
     if ($connection->query($sql) === TRUE) {
        echo "New record created successfully";
     } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
     }
    }

Check if it works and let me know if any further changes required

Upvotes: 1

Muhammad Ahmed
Muhammad Ahmed

Reputation: 481

Send and hidden variable in your form and send total count from it. and in get it by $_POST['hiddenval'];

start your for loop from 0 to that hdden value count. and use like $cid = mysql_real_escape_string($_POST['item_cid'.$i]); here $i is for loop counter. you will get your all records in similar way.

Upvotes: 1

Related Questions