多个动态文本域进入mysql db

时间:2014-01-07 15:09:04

标签: javascript php html mysql

有人可以解决我的问题吗?我试图从下面这些表格插入到数据库,但我不能让它工作 以下是完整形式先生:

  <html>
  <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title></title>

  <script type='text/javascript' src="jquery/jquery-1.8.3.js"></script>
  <script type="text/javascript" src="http://code.jquery.com/ui/1.9.2/jquery-ui.js">      </script>
  <link rel="stylesheet" type="text/css" href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css">

  <link rel="stylesheet" type="text/css" href="/css/result-light.css">

  <style type='text/css'>
    .dynatable {
            border: solid 1px #000;
            border-collapse: collapse;
        }

            .dynatable th,
            .dynatable td {
                border: solid 1px #000;
                padding: 2px 10px;
                width: 170px;
                text-align: center;
            }

            .dynatable .prototype {
                display: none;
            }
  </style>



<script type='text/javascript'>//<![CDATA[ 
$(function(){
$(document).ready(function () {
    var id = 0;
    // Add button functionality
    $("table.dynatable button.add").click(function () {
        id++;
        var master = $(this).closest("table.dynatable");
        // Get a new row based on the prototype row
        var prot = master.find("tr.prototype").clone();
        prot.attr("class", "")
        prot.find(".id").attr("value", id);
        master.find("tbody").append(prot);
    });

    // Remove button functionality
    $("table.dynatable button.remove").live("click", function () {
        $(this).parents("tr").remove();

    });

    $("table.dynatable button.addColumn").click(function () {
        var $this = $(this), $table = $this.closest('table')
        var columnName = window.prompt("Enter Column name", "");

        $('<th>' + columnName +'</th>').insertBefore($table.find('tr').first().find('th:last'))

        var idx = $(this).closest('td').index() + 1;
        $('<td><input type="text" name="col' + idx + '[]" value="" /</td>').insertBefore($table.find('tr:gt(0)').find('td:last'))
    });
});
});//]]>  

</script>


</head>
<body>

  <table class="dynatable">
    <thead>
        <tr>
            <th><button class="add">Add</button></th>
            <form method="post" action="blah.php">
            <th>Career Service/RA 1080(BOARD/BAR)</br>
            Under Special Laws/CES/CSEE</th>
            <th>Rating</th>
            <th>Date of Examination/</br>
            Conferment</th>
            <th>Place of Examination/ Conferment</th>
        </tr>

    </thead>
    <tbody>
        <tr class="prototype">
            <td><button class="remove">Remove</button></td>

            <td><input type="text" name="name[]" value="" /></td>
            <td><input type="text" name="rating[]" value="" /></td>
            <td><input type="date" name="date[]" value="" /></td>
            <td><input type="text" name="place[]" value="" /></td>


      </tr>


    </tbody>

</table>
<table width="972">
<tr align="center" >
        <td width="71" colspan="5"><input type="submit" name="submit"/></td>
        </tr>
         </form>
        </table>

</body>


</html>

这是我的PHP代码似乎无法正常工作。它插入一个空白数据,除了具有值但其余为空白数据的id。

PHP代码:

    <?php
    $host = 'Localhost';
    $username= 'root';
    $password= '';
    $db_name = 'bfp';

    $con = mysql_connect($host,$username,$password) or die ("Cant Connect");
    mysql_select_db($db_name) or die ("Cant Select DataBase");




    $k=2;//this is for the id value
    $name = $_POST['name'];
    foreach($name as $names) :
    $values=mysql_real_escape_string($names);
    $sql="INSERT INTO bfp_personnel_eligibility (id, eligibility_name, rating, doe, poe) VALUES ('{$k}','{$values}', '{$_POST['rating']}', '{$_POST['date']}', '{$_POST['place']}')";

    if (!mysql_query($sql,$con)){
       die('Error: ' . mysql_error());
    }
endforeach;



?>

1 个答案:

答案 0 :(得分:0)

试试这段代码:

$i = 0;
 foreach($name as $names) :
    $values=mysql_real_escape_string($names);
    $sql="INSERT INTO bfp_personnel_eligibility (id, eligibility_name, rating, doe, poe) VALUES ('{$k}','{$values}', '{$_POST['rating'][$i]}', '{$_POST['date'][$i]}', '{$_POST['place'][$i]}')";
 $i++;
    if (!mysql_query($sql,$con)){
       die('Error: ' . mysql_error());
    }
endforeach;

注意:您必须对要传递给查询的所有值应用mysql_real_escape_string

编辑: 像这样放置你的表格

 <body>
    <form method="post" action="blah.php">
    <table class="dynatable">
    .
    .
    .
    .

            </table>
     </form>
    </body>
相关问题