2

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 :(

3
  • You are getting $cid(all variable) by post method which retrieve one data Commented Apr 3, 2015 at 10:08
  • 2
    visit-stackoverflow.com/questions/21511434/… Commented Apr 3, 2015 at 10:09
  • @SagarNaliyapara thank you, i have found the answer i was looking for. Commented Apr 3, 2015 at 11:33

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

7 Comments

i'm using mysqli :(
Notice: Use of undefined constant mysqli_real_escape_string - assumed 'mysqli_real_escape_string'
Warning: mysqli_real_escape_string() expects exactly 2 parameters, 1 given
@yaqoob I have updated my answer I had an error I forgot to place inverted commas
|
1

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.

2 Comments

While Searching for my problem, i have found this same solution somewhere but didn't know how to count all the input fields.
Thanks Muhammad Ahmed, i have found the answer.
1

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.

Comments

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.