0

I want to insert multiple data to mysql. My PHP code which is Array input field:

pag1.php

  <form method="post" action="page2.php">
     <input type="text" name="option_id[]" size="1">
     <input type="text" name="store_id[]" size="1">
     <button type="submit" name="action" value="add" >Add</button>
  </form>

page2.php

<?php
if(isset($_POST['action'])){

for ($i=0; $i < count($_POST['option_id']); $i++ ) {
$option_id = $_POST['option_id'][$i];
$store_id = $_POST['store_id'][$i];
// check if option id is exists or not 
         $result = mysql_query ("SELECT * FROM temp_cart WHERE option_id='$option_id' AND store_id='$store_id' ") or die (mysql_error());

         if (mysql_num_rows($result) == 0){
                     //insert query 
                 }
                 else {
                     //update query
                 }
        }
}
?>

For loop is not work.

Your help would be very much appreciated.

  • Please share your HTML – Happy Coding Aug 31 '15 at 17:13
  • 1
    update your question with full form code, Don't drop that in comments, thanks – Funk Forty Niner Aug 31 '15 at 17:15
  • do you realize someone could call your url with whatever they want with data, and [inject](http://stackoverflow.com/q/332365/1816093) ? – Drew Aug 31 '15 at 17:17
  • [Your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – Jay Blanchard Aug 31 '15 at 17:18
  • If you can, you should [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) [statements](http://php.net/manual/en/pdo.prepared-statements.php) instead, and consider using PDO, [it's really not hard](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Aug 31 '15 at 17:18
  • Please don't dump code in comments. Edit your original post to add any new information @AzizulIslam – Jay Blanchard Aug 31 '15 at 17:19
  • you are having only one textbox, that also array type. There should be multiple textbox if you are using array type. – Nana Partykar Aug 31 '15 at 17:25
  • 1
    This `if(isset($_POST['submit'])){...}` will never happen. *Ain't that right Sam?* @JayBlanchard – Funk Forty Niner Aug 31 '15 at 17:26
  • 1
    *Right-o Ralph!* Never really means never here @Fred-ii- – Jay Blanchard Aug 31 '15 at 17:27

0 Answers0