2012-11-19 59 views
2

我在這裏有我的JS腳本,當用戶單擊按鈕'添加項目'時動態添加三個文本框。三個文本框保存在數據庫中時有不同的字段。但似乎在我的PHP代碼中,它並沒有從文本框中捕獲任何值。在數據庫中它只顯示'數組'。請幫幫我!謝謝!如何使用PHP將動態文本框保存到數據庫

<script language="javascript"> 
row_no=0; 
function addRow(tbl,row){ 
row_no++; 
if (row_no<=20){ 
    if (row_no<=20){ 
if (row_no>=10){ 
var textbox = row_no+'.)&nbsp;<input type="text" size = "50" maxlength= "50" name= "desk_user[]">';} 
if (row_no<10){ 
var textbox = row_no+'. )&nbsp;<input type="text" size = "20" maxlength= "50" name= "desk_user[]">';} 
var textbox2 = '<input type="text" size = "60" maxlength= "250" name= "desk_report[]">'; 
var textbox3 = '<input type="text" size = "60" maxlength= "250" name= "desk_action[]">'; 

var tbl = document.getElementById(tbl); 
var rowIndex = document.getElementById(row).value; 
var newRow = tbl.insertRow(row_no); 
var newCell = newRow.insertCell(0); 
newCell.innerHTML = textbox; 
var newCell = newRow.insertCell(1); 
newCell.innerHTML = textbox2; 
var newCell = newRow.insertCell(2); 
newCell.innerHTML = textbox3; 
} 
if (row_no>20){ 
alert ("Too Many Items. Limit of 20."); 
} 
} 
} 

</script> 

我的PHP代碼:

<?php 

$con = mysql_connect ("localhost","root","nasi") or die 
('cannot connect to database error: '.mysql_error()); 


if (isset($_POST['desk_user']) && 
isset($_POST['desk_report']) && 
isset($_POST['desk_action'])) 
{ 

$desk_user = $_POST['desk_user']; 
$desk_report = $_POST['desk_report']; 
$desk_action = $_POST['desk_action']; 

if (!empty($desk_user)&& !empty($desk_report)&& !empty($desk_action)) { 

mysql_select_db("csr", $con); 
$sql = "INSERT INTO `desktoplaptop` (`desk_user`,`desk_report`,`desk_action`) VALUES ('$_POST[desk_user]','$_POST[desk_report]','$_POST[desk_action]') "; 

if ($sql_run = mysql_query($sql)) { 
          echo 'ok.'; 
           } 
         else { 
            echo '*Sorry, we couldn\'t register you at this time. Try again later.'; 
            } 
} 

} 

?> 

回答

3

你得到,因爲你已經定義文本框的名稱作爲數組值數組,

<input type="text" size = "20" maxlength= "50" name= "desk_user[]"> 

修改這樣

<?php 

    $con = mysql_connect ("localhost","root","nasi") or die 
    ('cannot connect to database error: '.mysql_error()); 


    if (isset($_POST['desk_user']) && 
    isset($_POST['desk_report']) && 
    isset($_POST['desk_action'])) 
    { 

    $desk_user_arr = $_POST['desk_user'];//contains array value 
    $desk_report = $_POST['desk_report'];//contains array value 
    $desk_action = $_POST['desk_action'];//contains array value 
    foreach($desk_user_arr as $key=>$desk_user) { //Loop through arrays 
    if (!empty($desk_user) && !empty($desk_report[$key]) && !empty($desk_action[$key])) { 
    mysql_select_db("csr", $con); 
    $sql = "INSERT INTO `desktoplaptop` (`desk_user`,`desk_report`,`desk_action`) VALUES ('$desk_user','$desk_report[$key]','$desk_action[$key]') "; 

    if ($sql_run = mysql_query($sql)) { 
    echo 'ok.'; 
    } else { 
     echo '*Sorry, we couldn\'t register you at this time. Try again later.'; 
    } 
    } 
    } 
} 
?> 
您的代碼
相關問題