简体   繁体   English

从动态发布表单php获取数据

[英]fetch data from a dynamic post form php

I have a form in a php website to edit table data from a dynamic table, it displays correctly but I'm not sure if I'm passing the data correctly or how to access it. 我在php网站上有一个表单可以编辑来自动态表的表数据,它可以正确显示,但是我不确定是否正确传递了数据或如何访问它。

editMenu() 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);

the editAttendance.php looks like this: 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

?> ?>

I know how to fetch the data of a form when it has a set size, but it's my first time working on a dynamic one, and I can't make heads or tails of it. 我知道如何在具有固定大小的情况下获取表单的数据,但这是我第一次尝试动态表单,因此我无法做出正面或反面的事情。

The thing i want to get is an mysql update statement for the whole table, something like: 我想得到的是整个表的mysql更新语句,类似:

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

The attendance table looks like this and has about 15-18 rows: 出勤表如下所示,大约有15-18行:

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

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

2 | ..

When you post the data the format is $_POST['field_name'] = 'value' in the backend. 当您发布数据时,后端的格式为$ _POST ['field_name'] ='value'。 Do a loop to make the query. 循环执行查询。

$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

Then you can execute: 然后您可以执行:

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

Sorry for my english. 对不起我的英语不好。

I hope been helpful. 希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM