PHP/MySQL -列表行与自己的单选按钮,然后添加所有行与选定的单选按钮


PHP/MySQL - List rows with own radio button, then add all rows with selected radio button

我被这个问题难住了,所以我会尽量解释清楚。

我想做一个考勤系统的web应用程序,我正在做(没有公开,只是一个封闭的个人系统)。我想从"用户"表中抓取所有数据,并在表上显示"名称"answers"姓氏"。然后每个'name'将有一个单选按钮组。这样的:

<?php
include "functions.php";
$query = "SELECT * FROM users ORDER by surname ASC";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_assoc($result);
?>
<table>
<tr>
<td>Name</td>
<td colspan="5"></td>
</tr>
<form id="form" name="form" method="post" action="process.php">
<?php do { ?>
<tr>
<td><?php echo $row['forename']. ' ' .$row['surname']; ?></td>
<td><input type="radio" name="<?php echo $row['id']; ?>" id="<?php echo $row['id']; ?>_PU" value="PU" >PU</td>
<td><input type="radio" name="<?php echo $row['id']; ?>" id="<?php echo $row['id']; ?>_PC" value="PC">PC</td>
<td><input type="radio" name="<?php echo $row['id']; ?>" id="<?php echo $row['id']; ?>_AA" value="AA">AA</td>
<td><input type="radio" name="<?php echo $row['id']; ?>" id="<?php echo $row['id']; ?>_S" value="S">S</td>
<td><input type="radio" name="<?php echo $row['id']; ?>" id="<?php echo $row['id']; ?>_AWOL" value="AWOL">AWOL</td>
</tr>
<?php } while ($row = mysql_fetch_assoc($result)); ?> 
</form>
</table>

现在,我需要做什么,当我按下提交按钮时,它将获得所有这些数据,并将数据添加到一个名为attendance的表中?我想让表格看起来像这样

id | user_id | record | date
1  | 1       | PU     | CURRENT_TIMESTAMP

id和日期是自动的。

有谁能透露点什么吗?我对这个很陌生,只是在这里修修补补

提前致谢

参见下面的编辑-我将回答真正的问题,而不是用贬值的函数来打扰你。
以下是您可以使用的非折旧函数示例:

(mysql函数是贬值的,使用PDO或mysqli更安全)。

<?php
// Fill these infos with correct parameters
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "";
include "functions.php";
$query = "SELECT * FROM users ORDER by surname ASC";
$mysqli = new mysqli($servername, $username, $password, $dbname);
if ($mysqli->connect_error) {
die('Connexion Error (' . $mysqli->connect_errno . ') '
        . $mysqli->connect_error);
}
$result = mysqli->query($query);

删除行:$row = mysql_fetch_assoc($result); 变化:while ($row = mysql_fetch_assoc($result));变为while ($row = $result->fetch_assoc());

如果问题仍然存在-可能是查询有问题。你能提供给我们一个:

print "<pre>";
print_r($row);
print "</pre>";
编辑:

情况:你必须通过<form>携带id_student(int) + tag_student(str)(让我们这样称呼它们)。

这意味着每个单选按钮必须携带2个信息:

  • 相关id_student
  • 标签

最好的显示方式是数组:

    [array2insert] => Array
    (
        [0] => Array(
                     [num_student] => 14
                     [tag_student] => PU)
        [1] => Array(
                     [num_student] => 15
                     [tag_student] => PC)
)

我倾向于使用这样的技巧:在单选按钮的名称中使用数组+格式化需要存储的所有数据,使用这种模式:(id_student)_(tag_student).

的例子:

[array2insert] => Array
    (
        [0] => 14_PU
        [1] => 15_PC
        [2] => 16_AA
        [3] => 17_AWOL
        [4] => 18_S
    )

最后这里是代码:(我使用int作为number_student使其简单-只需用您的结果查询循环替换我的经典循环)

<?php 
if(isset($_POST) && !empty($_POST))
{
    $pattern = '/('d{1,})_(.*?)$/';
    foreach($_POST['array2insert'] as $row)
    {
        preg_match($pattern, $row, $output);
        echo "num student : " . $output[1] . " tag : " . $output[2] . "<br />"; 
    }    
    print "<pre>";
    print_r($_POST);
    print "</pre>";
}
?>
<html>
<body>
<form action="" method="post">
<?php 
$num_student = 14;
for($i = 0; $i < 5; ++$i)
{ ?>
    <table>
    <tr>
    <td><input type="radio" name="array2insert[<?php echo $i; ?>]" 
              value="<?php echo $num_student;?>_PU">PU</td>
    <td><input type="radio" name="array2insert[<?php echo $i; ?>]" 
              value="<?php echo $num_student;?>_PC">PC</td>
    <td><input type="radio" name="array2insert[<?php echo $i; ?>]" 
              value="<?php echo $num_student;?>_AA">AA</td>
    <td><input type="radio" name="array2insert[<?php echo $i; ?>]" 
              value="<?php echo $num_student;?>_S">S</td>
    <td><input type="radio" name="array2insert[<?php echo $i; ?>]" 
              value="<?php echo $num_student;?>_AWOL">AWOL</td>
    </tr>
    </table>
<?php 
    ++$num_student; 
} ?>
<input type="submit" name="submit" value="Get Selected Values" />
</form>
</body>
</html>

输出示例:

num student : 14 tag : PU
num student : 15 tag : PC
num student : 16 tag : AA
num student : 17 tag : AWOL
num student : 18 tag : S

所有数据都经过$_POST并通过regex提取。希望有帮助。

首先,您可以执行查询以获取表用户的所有结果,然后执行循环foreach以打印所有单选按钮,例如

foreach($result as $row){
    <input type="radio" name="<?php echo $row['id']; ?>" id="<?php echo $row['id']; ?>_PU" value="PU" >PU
}

然后在表单之后,当您按下按钮时,您可以获得选定的值,因为它将返回从输入的名称,然后您可以再次查询该特定用户的数据库,并在相同的查询中传递您想要的值到另一个表或使用结果进行2次查询。我的英语不好,但我认为你说的有道理