簡體   English   中英

從動態發布表單php獲取數據

[英]fetch data from a dynamic post form 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更新語句,類似:

$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 | ..

當您發布數據時,后端的格式為$ _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");

對不起我的英語不好。

希望對您有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM