2017-05-03 92 views
0

這是我對同一問題的第二篇文章,在我的第一篇文章中,我使用的是mysql而不是mysqli,所以我根據建議做了一些更新。 我有一個由用戶通過javascript控制的可變字段集(1到20集)的表單。使用動態表單將一組或多組字段插入數據庫

沒有動態內容,一切正常。在將我的html表單從city更改爲city[]後,它顯然停止將數據傳遞到我的數據庫。

我嘗試了各種方法使這項工作沒有成功。有任何想法嗎?

形式操作:

var i=0; //Global Variable i 
//function for increment 
function increment(){ 
    i +=1; 
} 

$(document).ready(function(e){ 
    /// Variables 
    var html ='<p /><div>'; 
     html+='<label for="city">City:</label>'; 
     html+=' <input type="text" name="childcity[]" id="city">'; 
     html+=' <label for="date">Date:</label>'; 
     html+=' <input type="text" name="childdate[]" id="date">'; 
     html+='<a href="#" id="removecity">Remove City</a>'; 
     html+='</div>'; 

    var maxRows = 20; 
    // var x = 1; 

    // Rows 
    $("#addcity").click(function(e){ 
     if(i <= maxRows){ 
      $("#container").append(html); 
      i++; 
     } 
    }); 

    // Remove Rows 
    $("#container").on('click','#removecity', function(e){ 
     $(this).parent('div').remove(); 
     i--; 
    }); 
}); 

表單數據檢索和查詢:

<?php 
/* Attempt MySQL server connection. Assuming you are running MySQL 
server with default setting (user 'root' with no password) */ 

$mysqli = new mysqli("localhost", "root", "", "pb1646a_jos1"); 

// Check connection 

// Escape user inputs for security 

$city = $_POST['city']; 
$date = $_POST['date']; 

foreach($city AS $key => $value){ 
    // attempt insert query execution 
    $sql = "INSERT INTO employe_table(name,age) 
    VALUES (
     '".$mysqli->real_escape_string($value)."', 
     '".$mysqli->real_escape_string($date['$key'])."' 
    )"; 
    $insert = $mysqli->query($query); 
} 
$mysqli->close(); // Close connection 
header("location: http://www.orastravelagency.com/modules/mod_pari/test/index.html") 
?> 

形式:

<!DOCTYPE html> 
<html lang="en"> 
<head> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> 
<script src="java.js" type="text/javascript"></script> 
<meta charset="UTF-8"> 
<title>Add Record Form</title> 
</head> 
<body> 
<form action="insert2.php" method="post"> 
    <div> 
    <p> 
     <label for="name">Trip Name:</label> 
     <input type="text" name="name" id="name"> 
    </p> 
    </div> 
    <div id="container"> 
    <p> 
     <label for="">City:</label> 
     <input type="text" name="city" id="city[]"> 
     <label for="date">Date:</label> 
     <input type="text" name="date" id="date[]"> 
     <a href="#" id="addcity">Add City</a> 
    </p> 
    </div> 
    <p /> 
    <input type="submit" value="submit"> 
</form> 
</body> 
</html> 

回答

1

$date['$key']應該$date[$key]。引號阻止了對$key變量的評估,它使用$key作爲文字索引。

但是,您應該使用預準備語句而不是連接變量。

$city = $_POST['city']; 
$date = $_POST['date']; 

$stmt = $mysqli->prepare("INSERT INTO employe_table(name, age) VALUES (?, ?)"); 
$stmt->bind_param("ss", $name, $age); 
foreach ($city as $key => $name) { 
    $age = $date[$key]; 
    $stmt->execute(); 
} 
+0

當我嘗試它時,它給了我一個服務器錯誤(「si」代表什麼?) –

+0

si代表第一列是字符串,第二列是整數。 – Barmar

+0

什麼是錯誤? – Barmar

0

您的過程中有幾個錯誤。

您想要生成多個輸入字段,並將它們作爲數組存儲在$_POST中。

更改此:

但是,你寫不正確的屬性,$ _ POST不讀id的:

<input type="text" name="city" id="city[]"> 
<input type="text" name="date" id="date[]"> 

要這樣:

<input type="text" name="city[]"> 
<input type="text" name="date[]"> 

我已刪除你的id屬性,因爲你不應該/不能在你的頁面中有重複的ID。如果您需要爲css標識這些字段,請使用它們的名稱屬性或添加class屬性來標識它們。

當您在javascript中動態添加後續內容時,應該再次使用name=city[]name=date[];並刪除該ID。

這將導致您的$_POST陣列按照預期被正確填充的多維陣列。

$name=$_POST['name']; // this is a string 
$cities=$_POST['city']; // this is an array 
$dates=$_POST['date']; // this is an array 

我真的不明白爲什麼city是在你的形式,如果你不打算把它列入您的查詢 - 所以我把它在我的解決方案。而你的表格列並不直觀地命名,所以考慮將它們改爲能夠更好地表示要存儲的值的東西。

$stmt=$mysqli->prepare("INSERT INTO employe_table (`name`,`city`,`date`) VALUES (?,?,?)"); 
$stmt->bind_param("sss",$name,$city,$date); 
foreach($cities as $index=>$city){ 
    $date=$dates[$index]; 
    $stmt->execute(); 
} 

編程時,總是努力產生DAMP and DRY工作 - 這將成倍幫助你和其他人誰看到它。