0

I have a form with dynamic inputs. How should I add an array from the inputs to php and then add to sql?

<?
    if(isset($_POST['add'])){
    $name = sf($_POST['name']); //first action start
    $sec_name = sf($_POST['sec_name']); 
    mysql_query("INSERT INTO tb_job (name, sec_name)  VALUES ('$name', '$sec_name')"); 
    $lid = mysql_insert_id();// then goes second action and here is missing code for array. 
    mysql_query("INSERT INTO tb_job_skills (id_job, pre_job, date_start, date_end) VALUES ('$lid', '$job', '$date_start', '$date_end')");
    }
    ?>



<div class="form-field">
    <label for="name">First Name <span class="require"> *</span></label>
    <input class="form-control"  name="name" required="" type="text">
</div>
<div class="form-field">
    <label for="sec_name">Second Name <span class="require"> *</span></label>
    <input class="form-control"  name="sec_name" required="" type="text">
</div>
<div class="form-field">
    <label for="pre_job">Previous Job <span class="require"> *</span></label>
    <input class="form-control"  name="pre_job[]" required="" type="text">
</div>
<div class="form-field">
    <label for="date_start">First Name <span class="require"> *</span></label>
    <input class="form-control"  name="date_start[]" required="" type="text">
</div>
<div class="form-field">
    <label for="date_end">First Name <span class="require"> *</span></label>
    <input class="form-control"  name="date_end[]" required="" type="text">
</div>

pre_job[],date_start[],date_end[] - is dynamic adding inputs (cause user can has some previous jobs)

So, the question is how to get data from this inputs and add them into tb_job_skills in the second action?

davejal
  • 6,009
  • 10
  • 39
  • 82
Ya Studio
  • 75
  • 1
  • 2
  • 11
  • take a look, should give you something to start with http://stackoverflow.com/questions/1176352/pdo-prepared-inserts-multiple-rows-in-single-query – Kevin Jan 26 '16 at 00:12
  • @Ghost, the OP is still using mysql and you're sending a link to PDO. – davejal Jan 26 '16 at 00:16
  • Ya Studio, please stop using mysql and start using mysqli or pdo – davejal Jan 26 '16 at 00:17
  • I use PDO, but this is the code of my client and i don't want to change all code) i just need to help him with this update (dynamic inputs) – Ya Studio Jan 26 '16 at 00:24
  • @davejal why? would you prefer the OP to continue using mysql instead of PDO? the OP can benefit using the example/answers in the link – Kevin Jan 26 '16 at 02:28

2 Answers2

0

$_POST['pre_job'] will itself be an array. If you only want to insert 1 row into the database you will need to implode it into a comma-separated string.

However, a better way to do this is to separate each column out into its own table, and use a for loop to insert rows.


mysql_query is deprecated

mysql_query is deprecated, and you aren't protected against sql injection; never trust user input.

davejal
  • 6,009
  • 10
  • 39
  • 82
  • I need to add in DB all fields about job. as it's dynamic inputs (users can add a lot of values. i need to add in to DB all data from inputs. – Ya Studio Jan 26 '16 at 00:20
0

You can run each data from your form in a loop. You also have to note that mysql is already deprecated, so it is recommended to at least use mysqli prepared statement:

if(isset($_POST["add"])){

  /* START OF INSERTING NAME AND SEC_NAME */
  $stmt = $con->prepare("INSERT INTO tb_job (name, sec_name) VALUES (?, ?)"); /* PREPARE YOUR QUERY */
  $stmt->bind_param("ss", $_POST["name"], $_POST["sec_name"]); /* BIND THIS DATA TO YOUR QUERY */
  $stmt->execute(); /* EXECUTE THE QUERY */
  $lid = $stmt->insert_id; /* GET THE ID OF THE INSERTED NAME */
  $stmt->close(); /* CLOSE THE PREPARED QUERY */

  /* START OF LOOP; CHECK EACH JOB INPUT OF USER */
  for($x = 0; $x < count($_POST["pre_job"]); $x++){

    if(!empty($_POST["pre_job"][$x])){ /* CHECK IF PRE_JOB[N] IS NOT EMPTY */
      /* START INSERTING THE DYNAMIC INPUT JOB(S) */
      $stmt = $con->prepare("INSERT INTO tb_job_skills (id_job, pre_job, date_start, date_end) VALUES (?, ?, ?, ?)");
      $stmt->bind_param("isss", $lid, $_POST["pre_job"][$x], $_POST["date_start"][$x], $_POST["date_end"][$x]);
      $stmt->execute();
      $stmt->close();
    } /* END OF IF; CHECKING IF THE INPUT IS NOT EMPTY */

  } /* END OF FOR LOOP */

} /* END OF ISSET */

Also note that you have to establish your connection in mysqli in order for the above to work:

/* JUST REPLACE NECESSARY DATA */
$con = new mysqli("Host", "Username", "Password", "Database");

/* CHECK CONNECTION */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}
Logan Wayne
  • 6,001
  • 16
  • 31
  • 49