PHP MYSQL将动态数组插入数据库

时间:2015-09-12 06:44:26

标签: php html mysql

动态HTML表单(带有一些js帮助)和下面的PHP脚本成功地将用户输入的值插入MySQL,除了表单中的所有行都放在数据库的一行中。我做错了什么?

动态表单HTML:

<table id="dataTable" class="form" border="4">
    <tbody  style="font-size:8pt">
    <th>
        <td align="center">Company</td>
        <td align="center">Project</td>
        <td align="center">Sub-Project</td>
        <td align="center">Change From</td>
        <td align="center">Change To</td>
        <td align="center">Activity</td>
        <td align="center">Responsible</td>
        <td align="center">Dur</td>
    </th>
    <tr  >
        <p>
        <td><input type="checkbox" required="required" name="chk[]" checked="checked" /></td>
        <td><input style="width:100px" type="text" readonly="readonly" name="coa[]" value="<?php echo $co; ?>">
        </td>
        <td>
            <select name="Projectname[]" style="font-size:10pt">
                <option selected="selected" required="required">Select project</option>
                <?php
                    foreach($proj as $item){
                ?>
                <option value="<?php echo $item; ?>"><?php echo $item; ?></option>
                <?php
                    }                           
                    mysqli_close($conn);
                ?>
            </select>   
        </td>
        <td><input style="width:100px" type="text" required="required" name="Subproj[]"></td>
        <td><input style="width:130px" type="text" required="required" name="Changefrom[]"></td>
        <td><input style="width:130px" type="text" required="required" name="Changeto[]"></td>
        <td><input style="width:300px" type="text" required="required" name="Activity[]"></td>                      
        <td><input style="width:90px" type="text" required="required" name="Resp[]"></td>
        <td><input type="text" required="required" class="small"  name="Durest[]"></td>
        </p>
    </tr>
    </tbody>
</table>

PHP脚本:

<?php 
    include("../../db_conn_ci_i.php");
    if(isset($_POST)==true && empty($_POST)==false){
    $co=$_POST['co'];               
    $chkbox = $_POST['chk'];
    $Projectname=$_POST['Projectname'];
    $Subproj=$_POST['Subproj'];         
    $Changefrom=$_POST['Changefrom'];
    $Changeto=$_POST['Changeto'];   
    $Activity=$_POST['Activity'];   
    $Resp=$_POST['Resp'];
    $Durest=$_POST['Durest'];
    }
    $pco=implode(',',$co);
    $pa=implode(',',$Projectname);
    $pb=implode(',',$Subproj);
    $c=implode(',',$Changefrom);
    $d=implode(',',$Changeto);
    $e=implode(',',$Activity);
    $f=implode(',',$Resp);
    $g=implode(',',$Durest);

    $sql=" INSERT INTO projects (co,Projectname,Subproj,Changefrom,Changeto,Activity,Resp,Durest) 
    VALUES ('.$pco.','.$pa.','.$pb.','.$c.','.$d.','.$e.','.$f.','.$g.')    ";
    $query = mysqli_query($conn,$sql);

etc,etc
?>

有什么建议吗? 谢谢。

1 个答案:

答案 0 :(得分:2)

您添加了一个额外的列但未为其添加值。由于您要插入七个包含八个值的列,这就是为什么它不适合您。您的插入查询如下所示。

$sql=" INSERT INTO projects (Projectname,Subproj,Changefrom,Changeto,Activity,Resp,Durest) 
    VALUES ('.$pa.','.$pb.','.$c.','.$d.','.$e.','.$f.','.$g.')  ";

        $query = mysqli_query($conn,$sql);