dsiimyoc804955 2015-06-04 10:29 采纳率: 100%
浏览 23
已采纳

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

I'm absolutely stumped by this so I'll try and explain it the best I can.

I'd like to make an attendance system for a web app I'm making (nothing public, just a closed personal system). I want to grab all the data from the 'users' table and display the 'forename' and 'surname' on a table. Then each 'name' will have a radio button group. Like this:

<?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>

Now, what would I have to do, so that when I press the submit button, it will get all this data, and add the data to a table called attendance? I want the table to look something like this

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

with the id and date being automatic.

Can anyone shed any light? I'm pretty new to this and just tinkering around

Many thanks in advance

</div>
  • 写回答

2条回答 默认 最新

  • douxianwu2221 2015-06-04 10:52
    关注

    SEE Edit below - I will answer to the real question instead messing you with depreciated fucntions.
    Here is non-depecriated function example you could use :

    (mysql functions are depreciated, safer to use PDO or 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);
    

    Remove the line : $row = mysql_fetch_assoc($result); Change : while ($row = mysql_fetch_assoc($result)); into while ($row = $result->fetch_assoc());

    If the problem persists - It might an issue with the query. Could you provide us an :

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

    EDIT :

    Situation : U have to carry id_student(int) + tag_student(str) (let's call them like that) through the <form>.

    That means each Radio button have to carry 2 informations :

    • the related id_student
    • the tag

    Best display would be an array :

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

    I tend to this by using the trick of using an array into name of radio button + formatting all the datas who need to be stored using this pattern : (id_student)_(tag_student).

    Example :

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

    Finally here's the code : (i use an int as number_student to make it simple - just replace my classic loop by you result query loop)

    <?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>
    

    Example of OUTPUT :

    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
    

    All datas went through $_POST and were extracted by regex. Hope it helped.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊