gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 00:37:21 25 4
gpt4 key购买 nike

我在 php 网站中有一个表单,用于编辑动态表中的表数据,它显示正确,但我不确定我是否正确传递数据或如何访问它。

编辑菜单()

$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");

对不起我的英语。

希望对您有所帮助。

关于php - 从动态发布表单 php 获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13684337/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com