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

时间:2014-03-10 22:15:03

标签: javascript php mysql arrays

大家好,我已经在桌子上打了几天试图解决这个问题,我完全被难倒了。

的index.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>

Custom.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点击添加时会创建额外的

CompanyName_02,Employee_02。等

CompanyName_03,Employee_03。等

所以我想在我的addemp.php中我可以构建一个带有while循环的数组

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

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

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

我哪里出错。

任何帮助都会很棒。

2 个答案:

答案 0 :(得分:0)

你的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'];
    }
?>

OR

<?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-&gt; execute();用于循环条目

答案 1 :(得分:0)

您需要将[]附加到input的字段名称中。因此; <input name="CompanyName[]">

然后$_POST['CompanyName']将包含一个包含所有表单字段的数组。您可以$_POST['CompanyName'][$j]访问哪些内容。

这样你就不需要在jQuery中对表单进行硬编码,所以现在你可以克隆第一行并将其附加到你的表中。

此外,您甚至循环连接,因此如果您将表单展开8次,则连接将进行8次,这实际上是不必要的。将连接放在循环上方。

使用mysqli_*是件好事,但我建议您使用预准备好的陈述,因为您的代码不安全。

表格通常不是构建表格的好主意。