2012-12-03 23 views
1

我有一個PHP網站的表格,可以從動態表格中編輯表格數據,它可以正確顯示,但我不確定我是否正確地傳遞了數據或者如何訪問它。從動態帖子表單獲取數據php

editMenu()

$query = "SELECT * FROM attendance"; 
$result = mysql_query($query); 

print("<b>Edit the details</b><br>"); 

print("<form method=\"POST\" action=\"editAttendance.php\" >"); 
print("<table>"); 
print("<tr>"); 
for($i = 0; $i < mysql_num_fields($result); $i++) { 
    $field_info = mysql_fetch_field($result, $i); 
    print("<th>" . $field_info->name . "</th>"); 
} 

while($row = mysql_fetch_row($result)) { 
    print("<tr>"); 
    foreach($row as $_column) { 
    print("<td>"); 
    print("<input type='text' name='{$_column}' size=15 value='{$_column}'>"); 
    print("</td>");    
    } 
print("</tr>"); 
} 

print("</table>"); 

print("<input type='submit' value='Edit'>"); 
print("<input type='button' value='Cancel' onclick=\"location.href='raidAttendance.php'\">"); 

print("</form>"); 

mysql_free_result($result); 

的editAttendance.php看起來是這樣的:

<?php 

@mysql_connect("localhost", "root", "vertrigo"); 
@mysql_select_db("test"); 

    $b = 0; 
    $query = array(); 
    $result = mysql_query("SELECT * FROM attendance"); 

    //get all the column names, works 
    for ($i = 0; $i < mysql_num_fields($result); $i++) { 
     $vars[$i] = mysql_field_name($result,$b); 
     $b++; 
    } 

    //get all the data, stuck here 

>

我知道如何獲取表單的數據時,它有一組?大小,但這是我第一次從事動態活動,而且我無法做出正面或反面的評論。

我想要得到的東西是爲整個表的mysql的update語句,是這樣的:

$query = ""; //all the data from the table 
$result = mysql_query("UPDATE attendance SET $query"); 

考勤表看起來像這樣,擁有大約15-18行:

id | name | strikes | date1 |date2 | date.. 

---------------------------------------------- 
1 | name1 | number | 21/10/2012 | 27/10/2012 | .. 

2 | .. 

回答

1

當您發佈數據時,後端的格式爲$ _POST ['field_name'] ='value'。做一個循環來進行查詢。

$query = ''; 

// Generate the query string 
foreach ($_POST as $field => $value) { 
    $query .= $field .' = ' .$value .', '; 
} 

$query = substr($query, 0, -2); // Remove the last comma and space 

$query .= ' where someField = someValue'; // add condition to the update statment 

則可以執行:

$result = mysql_query("UPDATE attendance SET $query"); 

對不起,我的英語水平。

我希望有幫助。