简体   繁体   English

将html表输入值添加到mysql数据库表

[英]Add html table input values to a mysql database table

I've searched far and wide for answers to this question but couldn't find any. 我已经搜索了很多关于该问题的答案,但找不到任何答案。

I want to add the content of a html table to a mysql database using php and javascript, where each row is a new record. 我想使用php和javascript将html表的内容添加到mysql数据库中,其中每一行都是一条新记录。

Here is a table example: 这是一个表格示例:

<table width="100%" border="1" cellspacing="0" cellpadding="0">
  <tr>
    <th scope="col">ID</th>
    <th scope="col">NAME</th>
    <th scope="col">EMAIL</th>

  </tr>
  <tr id="row">
    <td><input name="ID" type="text" value="20" /></td>
    <td><input name="NAME" type="text" value="Peter" /></td>
    <td><input name="EMAIL" type="text" value="peter@gmail.com" /></td>
  </tr>
  <tr id="row">
    <td><input name="ID" type="text" value="21" /></td>
    <td><input name="NAME" type="text" value="Jhon" /></td>
    <td><input name="EMAIL" type="text" value="jhon@yahoo.com" /></td>
  </tr>
  <tr id="row">
    <td><input name="ID" type="text" value="22" /></td>
    <td><input name="NAME" type="text" value="Mike" /></td>
    <td><input name="EMAIL" type="text" value="mike@hotmail.com" /></td>
  </tr>
</table>

<input name="Btn" type="button" onclick="addRecords()" value="Add"/>
</form>

And the javascript sample of what I tried: 和我尝试的javascript示例:

<script>
function addRecords()
{
    var row = document.getElementById("row")


    for (x in row)
    {
        $.post="<? $insertSQL = sprintf("INSERT INTO accounts (acc_id, acc_name, acc_email) VALUES (%s, %s)",
                       GetSQLValueString($_POST['ID'], "integer"),
                       GetSQLValueString($_POST['NAME'], "text"),
                       GetSQLValueString($_POST['EMAIL'], "text"));

  mysql_select_db($database_G3CSAdminControlPanel, $G3CSAdminControlPanel);
  $Result1 = mysql_query($insertSQL, $G3CSAdminControlPanel) or die(mysql_error()); ?>"
    }
}

</script>

What the above does, it only adds the last record in the table to the database. 上面所做的只是将表中的最后一条记录添加到数据库中。 I'm guessing my problem is with the php code and that each input should be put into an array as well and not just the rows, not sure how to do that though. 我猜我的问题是php代码,每个输入也应该放入数组中,而不仅仅是行,不确定该怎么做。

UPDATE The code I've shown is only a example of what I'm trying to do. 更新我显示的代码只是我要尝试执行的示例。 The id's for the rows has to stay the same, because I'm using a script which creates the table in my actual web page. 这些行的ID必须保持不变,因为我使用的脚本会在我的实际网页中创建表格。

Basically I want to do the following, but in PHP: 基本上,我想执行以下操作,但是在PHP中:

<script>
function addRecords()
{
    var row = document.getElementById("row");
    var n = 0;
    var q = 0;
    for (x in "row")
    {
        n++;
        var code = document.getElementsByName("ID")[q].value;
        var desc = document.getElementsByName("NAME")[q].value;
        var price = document.getElementsByName("EMAIL")[q].value;
        alert(code + ' ' + desc + ' ' + price);
        q++;
    }
    alert('There is ' + n + ' rows in the table');
}
</script>

The script returns the values of the inputs in each row. 该脚本返回每一行中输入的值。 I want to do something similar in PHP, but to add the values from the inputs in each rows as a record in a database table. 我想在PHP中做类似的事情,但要将来自每一行的输入值添加为数据库表中的记录。

So instead of doing a Javascript alert for each row it should execute a PHP script to insert a record of that row. 因此,与其对每行执行Javascript警报,不如执行PHP脚本以插入该行的记录。

You'll have to uniquely identify each row (ID0,EMAIL0, etc). 您必须唯一地标识每一行(ID0,EMAIL0等)。 This is easily done when building the table either via PHP or script. 通过PHP或脚本构建表时,很容易做到这一点。 Then in your PHP form code you do a foreach on $_GET and read each. 然后在您的PHP表单代码中对$ _GET进行foreach并读取它们。 The way you have it now, each subsequent ID, EMAIL, etc is over writing the previous one. 您现在拥有的方式,每个后续ID,EMAIL等都将覆盖前一个ID。

You have three fields to insert. 您要插入三个字段。 And there are a number of each. 每个都有很多。 So you need to use input fields' names as html array. 因此,您需要使用输入字段的名称作为html数组。 Otherwise upon form post, the last field will override prior ones. 否则在表单发布时,最后一个字段将覆盖先前的字段。

Specifically talking, edit your markup as follows: 具体来说,如下编辑标记:

<table width="100%" border="1" cellspacing="0" cellpadding="0">
  <tr>
    <th scope="col">ID</th>
    <th scope="col">NAME</th>
    <th scope="col">EMAIL</th>
  </tr>
  <tr id="row1">
    <td><input name="ID[]" type="text" value="22" /></td>
    <td><input name="NAME[]" type="text" value="Mike" /></td>
    <td><input name="EMAIL[]" type="text" value="mike@hotmail.com" /></td>
  </tr>
  <tr id="row2">
    <td><input name="ID[]" type="text" value="22" /></td>
    <td><input name="NAME[]" type="text" value="Mike" /></td>
    <td><input name="EMAIL[]" type="text" value="mike@hotmail.com" /></td>
  </tr>
  <tr id="row3">
    <td><input name="ID[]" type="text" value="22" /></td>
    <td><input name="NAME[]" type="text" value="Mike" /></td>
    <td><input name="EMAIL[]" type="text" value="mike@hotmail.com" /></td>
  </tr>
</table>

Rather than directly accessing your row, first get your table, then the rows. 而不是直接访问您的行,而是先获取表,然后再获取行。

var table = document.getElementById('myTable');
for (i=1; i < table.rows.length; i++) {
    var row = table.rows(i);
}

If you have multiple elements with the same id, such as: 如果您有多个具有相同ID的元素,例如:

 <tr id="row"> 

then the function: 然后函数:

document.getElementById("row")

will consider only the last one of them (this depending on the browser). 只会考虑其中的最后一个(具体取决于浏览器)。

To be really sure that you will really get all the elements you have to give them different ids. 要真正确保您将真正获得所有要为其赋予不同ID的元素。 like 喜欢

<tr id="row1">...
...
<tr id="row2">...
...
<tr id="row3">...
...

Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM