javascript - PHP 将文本文件数组添加到数据库中

标签 javascript php mysql arrays

大家好,几天来我一直在 table 上敲头试图解决这个问题,我完全被难住了。

索引.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Add and Delete rows dynamically with textboxes using jQuery | Media Milan</title>
    <script src="jquery-1.4.4.min.js"></script>
<script src="custom.js"></script>
</head>
<body>
    <div id="page_container">
        <div class="form_container">
            <h3>Add and Delete rows dynamically with textboxes using jQuery:</h3>
            <form action="addemp.php"  method="post">
                <table id="expense_table" cellspacing="0" cellpadding="0">
                    <thead>
                        <tr>
                            <th>CompanyName</th>
                            <th>Employee</th>
                            <th>Phonenum</th>
                            <th>Address1</th>
                            <th>Address2</th>
                            <th>City</th>
                            <th>State</th>
                            <th>Zip</th>
                            <th>&nbsp;</th>
                        </tr>
                     </thead>
                     <tbody>
                         <tr>
                             <td><input type="text" name="CompanyName_01"  /></td>
                             <td><input type="text" name="Employee_01"  /></td>
                             <td><input type="text" name="Phonenum_01"  /></td>
                             <td><input type="text" name="Address1_01"  /></td>
                             <td><input type="text" name="Address2_01"  /></td>
                             <td><input type="text" name="City_01"  /></td>
                             <td><input type="text" name="State_01"  /></td>
                             <td><input type="text" name="Zip_01"  /></td>
                             <td>&nbsp;</td>
                         </tr>
                     </tbody>
                 </table>
                <INPUT type="submit">
            </form>
            <input type="button" value="Add Row" id="add_ExpenseRow" />
        </div> <!-- END form_container -->
        <div class="clearfix"></div>
    </div>
</body>
</html>

自定义.js

$(function(){
    // GET ID OF last row and increment it by one
    var $lastChar =1, $newRow;
    $get_lastID = function(){
        var $id = $('#expense_table tr:last-child td:first-child input').attr("name");
        $lastChar = parseInt($id.substr($id.length - 2), 10);
        //console.log('GET id: ' + $lastChar + ' | $id :'+$id);
        $lastChar = $lastChar + 1;
        $newRow = "<tr> \
                       <td><input type='text' name='CompanyName_0"+$lastChar+"' /></td> \
                       <td><input type='text' name='Employee_0"+$lastChar+"' /></td> \
                       <td><input type='text' name='Phonenum_0"+$lastChar+"' /></td> \
                       <td><input type='text' name='Address1_0"+$lastChar+"' /></td> \
                       <td><input type='text' name='Address2_0"+$lastChar+"' /></td> \
                       <td><input type='text' name='City_0"+$lastChar+"' /></td> \
                       <td><input type='text' name='State_0"+$lastChar+"' /></td> \
                       <td><input type='text' name='Zip_0"+$lastChar+"' /></td> \
                       <td><input type='button' value='Delete' class='del_ExpenseRow' /></td> \
                   </tr>"
        return $newRow;
    }

    // ***** -- START ADDING NEW ROWS
    $('#add_ExpenseRow').live("click", function(){
        if($('#expense_table tr').size() <= 9){
            $get_lastID();
            $('#expense_table tbody').append($newRow);
        }else{
                alert("Reached Maximum Rows!");
        };
    });

    $(".del_ExpenseRow").live("click", function(){
        $(this).closest('tr').remove();
        $lastChar = $lastChar-2;
    });
});

addemp.php 这是我完全迷失的地方

for ($j = 0; $j < count($_POST['CompanyName_01']); $j++) {
    $CompanyName = $_POST['CompanyName_01'][$j];
    $Employee = $_POST['Employee_01'][$j];
    $Phonenum = $_POST['Phonenum_01'][$j];
    $Address1 = $_POST['Address1_01'][$j];
    $Address2 = $_POST['Address2_01'][$j];
    $City = $_POST['City_01'][$j];
    $State = $_POST['State_01'][$j];
    $Zip = $_POST['Zip_01'][$j];

    $con=mysqli_connect("localhost","USER","password","DATABASE");
    // Check connection
    if (mysqli_connect_errno())
    {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    $sql="INSERT INTO EmployeeInfo (CompanyName, Employee, Phonenum, Address1, Address2, City, State, Zip)
    VALUES('$CompanyName','$Employee','$Phonenum','$Address1','$Address2','$City','$State','$Zip')";

    if (!mysqli_query($con,$sql))
    {
        die('Error: ' . mysqli_error($con));
    }
    echo "1 record added";
}//end of the for loop

我需要帮助构建 php 代码以输入数据库。 我尝试了许多不同的方法来读取和写入数据,但卡住了。

我尝试更改动态创建的文本字段和数组,但是我无法更改 custom.js 代码以使其立即工作。

代码运行时我的思考过程:

index.php 创建 CompanyName_01 、 Employee_01 。等等

然后,当您从 custom.js 中点击“添加”时,会创建附加的

公司名称_02、员工_02。等等

公司名称_03、员工_03。等等

所以我认为在我的 addemp.php 中我可以用 while 循环构建一个数组

<br>
$companyname_array CompanyName_0[i]<br>
$employee_array Employee_0[i]<br>

等等

然后使用创建的变量添加到我的数据库

但是我无法获得任何我正在尝试工作的代码。

我哪里出错了。

任何帮助都会很棒。

最佳答案

你的 for 循环语法有点不对:)

<?php
    for ($j = 1; $j < count($_POST['CompanyName_0']); $j++) {
        $CompanyName = $_POST['CompanyName_0$j'];
        $Employee = $_POST['Employee_0$j'];
        $Phonenum = $_POST['Phonenum_0$j'];
        $Address1 = $_POST['Address1_0$j'];
        $Address2 = $_POST['Address2_0$j'];
        $City = $_POST['City_0$j'];
        $State = $_POST['State_0$j'];
        $Zip = $_POST['Zip_0$j'];
    }
?>

或者

<?php
    for ($j = 1; $j < count($_POST['CompanyName_0']); $j++) {
        $CompanyName = $_POST['CompanyName_0'.$j];
        $Employee = $_POST['Employee_0'.$j];
        $Phonenum = $_POST['Phonenum_0'.$j];
        $Address1 = $_POST['Address1_0'.$j];
        $Address2 = $_POST['Address2_0'.$j];
        $City = $_POST['City_0'.$j];
        $State = $_POST['State_0'.$j];
        $Zip = $_POST['Zip_0'.$j];
    }
?>

每次循环运行时,再检查一下与mysql的连接,将其放在循环之前。我强烈建议使用 stmt->execute();用于循环条目

关于javascript - PHP 将文本文件数组添加到数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22312497/

相关文章:

javascript按钮在其他按钮中重复操作

javascript - 复选框默认图标不会随 jQuery 更新

javascript - 如何在 Javascript 中为 if 语句添加公差

php - codeigniter phpexcel 错误 ZipArchive::getFromName():无效或未初始化的 Zip 对象

php - 更新特定行textarea和mysql

java - 如何编写 Java 程序来对 Aurora MySQL 数据库进行身份验证?

MySQL REGEXP 语句不应该返回 true?

javascript - XML3D getLocalBoundingBox 给出 "this.renderNode.getObjectSpaceBoundingBox is not a function"错误

php - 高可用性 API - 这个解决方案好吗?

php - 如何显示已确认的好友