动态输入表单和带代码的php代码

时间:2016-01-25 23:30:07

标签: php mysql

我有一个带动态输入的表单。我应该如何从输入到php添加一个数组,然后添加到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[] - 是动态添加输入(因为用户可以有一些以前的工作)

那么,问题是如何从这些输入中获取数据并在第二个操作中将它们添加到tb_job_skills中?

2 个答案:

答案 0 :(得分:0)

$_POST['pre_job']本身就是一个数组。如果您只想在数据库中插入1行,则需要将其内嵌到以逗号分隔的字符串中。

但是,更好的方法是将每列分隔到自己的表中,并使用for循环插入行。

不推荐使用

mysql_query

mysql_query 已弃用,并且您没有受到sql注入的保护;从不信任用户输入。

答案 1 :(得分:0)

您可以循环运行表单中的每个数据。您还必须注意mysql已经deprecated,因此建议至少使用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 */

另请注意,您必须在mysqli中建立连接才能使上述内容正常工作:

/* 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();
}