2016-06-26 79 views
0

我有3行。哪些是動態創建的。如果我們點擊添加更多那麼所有三個字段都是動態創建的。它正常工作。動態行插入表中通過php

但我想通過mysql查詢動態地將這些字段插入到表中。我嘗試過,但它不起作用。請幫助任何人解決它。謝謝。

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> 
    <script type="text/javascript"> 
    var rowCount = 1; 

    window.addMoreRows = function(frm) { 
    rowCount++; 
     var recRow = '<div id="rowCount' + rowCount + '"><div class="col-sm-3 text-left">College Name:</div><div class = "col-sm-9"><input type="text" name = "collegename[]" placeholder = "College Name" class = "form-control"/></div><div class = "col-sm-3 text-left" > Affiliated University: </div><div class = "col-sm-9" ><input type = "text" name = "univ[]" placeholder = "Affiliated University" class = "form-control"/> </div><div class = "col-sm-3 text-left" > Course Name: </div> <div class = "col-sm-9" ><input type = "text" name = "cname[]" placeholder = "Course Name" class = "form-control"/></div><div class = "col-sm-3 text-left" > Major & Subject: </div><div class = "col-sm-9"><input type = "text" name = "msub[]" placeholder = "Major Subject" class = "form-control"/></div><div class = "col-sm-3 text-left"> Final & Percentage: </div><div class = "col-sm-9"><input type = "text" name = "fper[]" placeholder = "Final Percentage" class = "form-control"/></div><a href = "javascript:void(0);" onclick = "removeRow (' + rowCount + ');" >Delete</a></div> '; 
$('#addedRows').append(recRow); 
} 
window.removeRow = function(removeNum) { 
    $('#rowCount'+removeNum).remove(); 
} 
    </script> 
    <?php 
    extract($_POST); 
    if (isset($_POST['submit_val'])) { 
    if ($_POST['dynfields']) { 
    foreach (array_keys($dynfields) as $key) { 
    $txt = $dynfields[$key]; 
    $txt1 = $univ[$key]; 
    $txt2 = $cname[$key]; 
    $txt3 = $msub[$key]; 
$txt4 = $fper[$key]; 
    $query = mysql_query("INSERT INTO dynamicfield VALUES ('','','$txt','$txt1','$txt2','$txt3','$txt4')"); 

    } 
    } 
    echo "<i><h2><strong>" . count($_POST['dynfields']) . "</strong> Hobbies Added</h2></i>"; 
    mysql_close(); 
    } 
    ?> 
     <form action="" method="post" id="addedRows"> 
     <h5 align="left"><u style="color:#449D44;">Other Studies</u>-<span style="font:normal 12px agency, arial; color:blue; text-decoration:underline; cursor:pointer;" onclick="addMoreRows(this.form);">Add More Other Studies</span></h5> 
    <span id="rowId"> 
    <div class="col-sm-3 text-left">College&nbsp;Name: </div> 
     <div class="col-sm-9"><input type="text" name="dynfields[]" placeholder="College Name" class="form-control" /></div> 

<div class="col-sm-3 text-left">Affiliated&nbsp;University: </div> 
<div class="col-sm-9"><input type="text" name="univ[]" placeholder="Affiliated University" class="form-control" /></div> 
<div class="col-sm-3 text-left">Course&nbsp;Name: </div> 
<div class="col-sm-9"><input type="text" name="cname[]" placeholder="Course Name" class="form-control" /></div> 
<div class="col-sm-3 text-left">Major&nbsp;Subject: </div> 
<div class="col-sm-9"><input type="text" name="msub[]" placeholder="Major Subject" class="form-control" /></div> 
<div class="col-sm-3 text-left">Final&nbsp;Percentage: </div> 
<div class="col-sm-9"><input type="text" name="fper[]" placeholder="Final Percentage" class="form-control" /></div> 
</span> 
<input type="submit" name="submit_val" value="Submit" /> 

+0

什麼是列名? – C2486

+0

姓名,郵箱,手機號碼 – user3526766

+0

在查詢中使用列名,查看我的答案 – C2486

回答

0

我找不到,你叫你的輸入字段。

根據要從$ _POST參數中檢索的數組命名您提交的輸入字段。

<input name="dynfields[]"/> 

括號告訴PHP,提交的值是數組的一部分。 在你編輯的問題中,HTML會是這樣的。

<tr> 
    <td><input name="dynfields[]" type="text" value="" size="17%"/></td> 
    <td><input name="dynfields1[]" type="text" value="" /></td> 
    <td><input name="dynfields2[]" type="text" value="" /></td> 
</tr> 
<tr> 
    <td><input name="dynfields[]" type="text" value="" size="17%"/></td> 
    <td><input name="dynfields1[]" type="text" value="" /></td> 
    <td><input name="dynfields2[]" type="text" value="" /></td> 
</tr> 
(...) 

提交,你應該能夠訪問3個單獨的陣列形式後:

$_POST['dynfields']; 
$_POST['dynfields1']; 
$_POST['dynfields2']; 
+0

我已更新。請看一下。謝謝。 – user3526766

+0

更多..更新..讓我檢查 – elfwyn

+0

我已更新..它正在運行。但提交後,只有0,0,0,0,0進入桌面。 – user3526766