使用数组保存银行行

时间:2017-10-20 13:28:29

标签: php html

我有一个动态表单,允许添加多个文本框。在将数据保存在数据库中时,它还会节省额外的空额外行。你能帮忙找到问题吗?

HTML:

<div class="col-xs-12 col-sm-6">
  <div class="form-group">
    <input name="skill[]" type="text" class="form-control" placeholder="Skill name, e.g. HTML">
  </div>
</div>

<div class="col-xs-12 col-sm-6">

  <div class="form-group">
    <div class="input-group">
      <input name="percent[]" type="text" class="form-control" placeholder="Skill proficiency, e.g. 90">
      <span class="input-group-addon">%</span>
    </div>
  </div>

</div>

<div class="col-xs-12 duplicateable-content">
  <div class="item-block">
    <div class="item-form">

      <button class="btn btn-danger btn-float btn-remove"><i class="ti-close"></i></button>

      <div class="row">
        <div class="col-xs-12 col-sm-6">
          <div class="form-group">
            <input name="skill[]" type="text" class="form-control" placeholder="Skill name, e.g. HTML">
          </div>
        </div>

        <div class="col-xs-12 col-sm-6">

          <div class="form-group">
            <div class="input-group">
              <input name="percent[]" type="text" class="form-control" placeholder="Skill proficiency, e.g. 90">
              <span class="input-group-addon">%</span>
            </div>
          </div>

        </div>
      </div>

    </div>
</div>   

这是我保存数据输入的php:

for ($i = 0; $i < count($_POST["skill"]); $i++) {
  $skill = $_POST["skill"][$i];
  $percent = $_POST["percent"][$i];

  $sql = "insert into tb_skill (skills,percent,user_id) values 
    ('$skill', '$percent', '$_SESSION[id]')";
  mysqli_query($con, $sql);
}

2 个答案:

答案 0 :(得分:0)

在将变量提交到数据库之前,在for循环中添加这些行。

$skill = trim($skill);
$percent = trim($percent);

答案 1 :(得分:0)

这可能对你有所帮助。

  $skill = $_POST['skill];
    $percent = $_POST['percent'];
    for ($i = 0; $i < = count($skill); $i++) {

      $sql = "insert into tb_skill (skills,percent,user_id) values 
        ('$skill[$i]', '$percent[$i]', '$_SESSION[id]')";
      mysqli_query($con, $sql);
    }