如何将动态表数组中的值插入MySQL?

时间:2019-10-30 22:05:54

标签: php html mysql

我试图将我从表中的数组输入的值插入到MySQL数据库中。 这是我的代码:

在函数提交中,我创建了一个函数,用于将用Javascript编写的表数组中添加的值插入数据库。 PHP的问题是,通过调用名为cal的输入类型月份和名为pag的输入类型编号将这些值插入数据库中,来插入数据库。

  <html lang="en">
  <head>
  <meta charset="UTF-8">
  <title>Ficha de Socio</title>
<body onload="createTable()">
    <input type="button" id="addRow" value="Añadir pago" onclick="addRow()" />

  </p>

  <!--THE CONTAINER WHERE WE'll ADD THE DYNAMIC TABLE-->
  <div id="cont"></div>

  <p><input type="submit" id="bt" value="Guarda los cambios" onclick="submit()" /></p>


 </form>`
Here is the Javascript Code to create the dynamic table. ```As you can see this code generates a table with a button to add rows. All I want to do is to insert all the rows added in my database.
I am trying to insert 'cal'  name declared in the addRow function but with no luck
Can anyone please help

`</body>

<script>
  // ARRAY FOR HEADER.
  var arrHead = new Array();
  arrHead = ['', 'Mes del año', 'Cantidad'];      // SIMPLY ADD OR REMOVE VALUES IN THE ARRAY FOR TABLE HEADERS.```

  // FIRST CREATE A TABLE STRUCTURE BY ADDING A FEW HEADERS AND
  // ADD THE TABLE TO YOUR WEB PAGE.
  function createTable() {
      var empTable = document.createElement('table');
      empTable.setAttribute('id', 'empTable');            // SET THE TABLE ID.

      var tr = empTable.insertRow(-1);

      for (var h = 0; h < arrHead.length; h++) {
          var th = document.createElement('th');          // TABLE HEADER.
          th.innerHTML = arrHead[h];
          tr.appendChild(th);
      }

      var div = document.getElementById('cont');
      div.appendChild(empTable);    // ADD THE TABLE TO YOUR WEB PAGE.
  }

  // ADD A NEW ROW TO THE TABLE.s
  function addRow() {
      var empTab = document.getElementById('empTable');

      var rowCnt = empTab.rows.length;        // GET TABLE ROW COUNT.
      var tr = empTab.insertRow(rowCnt);      // TABLE ROW.
      tr = empTab.insertRow(rowCnt);

      for (var c = 0; c < arrHead.length; c++) {
          var td = document.createElement('td');          // TABLE DEFINITION.
          td = tr.insertCell(c);

          if (c == 0) {           // FIRST COLUMN.
              // ADD A BUTTON.
              var button = document.createElement('input');

              // SET INPUT ATTRIBUTE.
              button.setAttribute('type', 'button');
              button.setAttribute('value', 'Borrar');

              // ADD THE BUTTON's 'onclick' EVENT.
              button.setAttribute('onclick', 'removeRow(this)');

              td.appendChild(button);
          }
          else if(c == 1) {
              // CREATE AND ADD TEXTBOX IN EACH CELL.
              var ele = document.createElement('input');
              ele.setAttribute('type', 'month');
              ele.setAttribute('value', 'current_date()');
              ele.setAttribute('name', 'cal');

              td.appendChild(ele);

          }

          else {
              // CREATE AND ADD TEXTBOX IN EACH CELL.
              var ele = document.createElement('input');
              ele.setAttribute('type', 'number');
              ele.setAttribute('value', '10');
              ele.setAttribute('name','pag');

              td.appendChild(ele);

          }


      }
  }

  // DELETE TABLE ROW.
  function removeRow(oButton) {
      <?php
  $link = mysqli_connect("localhost", "username", "password", "db");

  // Check connection
  if($link === false){
      die("ERROR: No se ha podido conectar. " . mysqli_connect_error());
                      }
 // $query = mysqli_query($link,"SELECT * FROM pago p, socio s where p.IdPago=s.Id");
 // $row = mysqli_fetch_array($query);

  $sql= "INSERT INTO pago (calendario, cantidad) values ('".$_POST["cal"]."','".$_POST["pag"]."')";

  if ($link->query($sql) === TRUE) {
      echo "New record created successfully";
          } else {
          echo "Error: " . $sql . "<br>" . $link->error;
          }

mysqli_close($link);
?>
  }

  // EXTRACT AND SUBMIT TABLE DATA.

</script>```
</html>```


0 个答案:

没有答案