0

I have created a multiple input rows that allows addition and removal of rows using javascript.

Here is the code:

var field = 1;

function plan_fields() {

  field++;
  var objTo = document.getElementById('plan_fields')
  var divtest = document.createElement("div");
  divtest.setAttribute("class", "form-group removeclass" + field);
  var rdiv = 'removeclass' + field;
  divtest.innerHTML = '<div class="col-sm-4 nopadding"><div class="form-group"> <input type="text" class="form-control" id="plannumber" name="plannumber[' + field + ']" value="" placeholder="School name"></div></div><div class="col-sm-4 nopadding"><div class="form-group"> <input type="text" class="form-control" id="Degree" name="Degree[' + field + ']" value="" placeholder="Degree"></div></div><div class="col-sm-4 nopadding"><div class="form-group"><div class="input-group"> <select class="form-control" id="plandate" name="plandate[' + field + ']"><option value="">Date</option><option value="2015">2015</option><option value="2016">2016</option><option value="2017">2017</option><option value="2018">2018</option> </select><div class="input-group-btn"> <button class="btn btn-danger" type="button" onclick="remove_plan_fields(' + field + ');"> <span class="glyphicon glyphicon-minus" aria-hidden="true"></span> </button></div></div></div></div><div class="clear"></div>';

  objTo.appendChild(divtest)
}

function remove_plan_fields(rid) {
  $('.removeclass' + rid).remove();
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<form action="?create" method="post">
  <div class="panel-body">
    <div id="plan_fields">

    </div>
    <div class="col-sm-4 nopadding">
      <div class="form-group">
        <input type="text" class="form-control" id="plannumber" name="plannumber[0]" value="" placeholder="Plan Number">
      </div>
    </div>
    <div class="col-sm-4 nopadding">
      <div class="form-group">
        <input type="text" class="form-control" id="context" name="context[0]" value="" placeholder="Context represented on plan">
      </div>
    </div>


    <div class="col-sm-4 nopadding">
      <div class="form-group">
        <div class="input-group">
          <select class="form-control" id="plandate" name="plandate">
          
            <option value="">Date Completed</option>
            <option value="2015">2015</option>
            <option value="2016">2016</option>
            <option value="2017">2017</option>
            <option value="2018">2018</option>
          </select>
          <div class="input-group-btn">
            <button class="btn btn-success" type="button" onclick="plan_fields();"> <span class="glyphicon glyphicon-plus" aria-hidden="true"></span> </button>
          </div>
        </div>
      </div>
    </div>
    <div class="clear"></div>

  </div>

  <div class="form-group">
    <div class="col-md-9 col-sm-9 col-xs-12 col-md-offset-3">
      <button type="submit" value="submit" class="btn btn-success">Add Plan</button>
    </div>
  </div>

</form>

Now, I want to add the records of the multiple arrays but I am stuck here, so how do I fix my php part ?

<?php             
  $host     = "localhost";
  $username = "root";
  $password = "";
  $dbname   = "project";

  if (isset($_GET['create'])) {
  $link = mysqli_connect($host, $username, $password, $dbname)
      or die('Could not connect: ' . mysqli_error());

  mysqli_select_db($link, $dbname) 
      or die('Could not select database');

    foreach($_POST as $key => $value)
  {
      if (strstr($key, ''))
      {
          $x = str_replace('',NULL,$key);
          inserttag($value, $x);
      }
  }

  $plannumber= $_POST['plannumber[]'];
  $plan = $_POST['context[]'];
  $date = $_POST['plandate[]'];


  $query = "INSERT INTO plans
            VALUES ($plannumber,'$date','$plan')";
  $result = mysqli_query($link, $query) or die('Could not connect: ');

  $message = "Plan Added";
  echo "<script type='text/javascript'>alert('$message');window.location.href = 'plans.php';</script>";



  }


?>
Saugat Bhattarai
  • 2,614
  • 4
  • 24
  • 33
martinBibo
  • 15
  • 2
  • `plannumber` alone and not `plannumber[]`? – Roljhon Mar 11 '17 at 17:19
  • You are wide open to [SQL Injections](http://php.net/manual/en/security.database.sql-injection.php) and should really use [Prepared Statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) instead of concatenating your queries. Specially since you're not escaping the user inputs at all! – M. Eriksson Mar 11 '17 at 17:20
  • Possible duplicate of [Insert multiple rows with one query MySQL](http://stackoverflow.com/questions/12502032/insert-multiple-rows-with-one-query-mysql) - That answer works with MySQLi and Prepared Statements as well. – M. Eriksson Mar 11 '17 at 17:23

2 Answers2

0

If you want to store array in table then you can use serialize($array) function (replace $array with your array variable name ). It will return serialized data and then you can store that serialized array and i would highly recommend you prepared statement. Your code is highly insecure.

0

First you need a variable which contains array value then the array value implode a space separator, implode separator value is a string. then you can insert the separator value. Like this

$plannumber= $_POST['plannumber[]'];
$plannumber_separated = implode(" ", $plannumber);
$plan= $_POST['context[]'];
$plan_separated = implode(" ", $plan);
$query = "INSERT INTO plansVALUES ($plannumber_separated,$plan_separated)";
A.A Noman
  • 5,244
  • 9
  • 24
  • 46