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>

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.

well, first you can do a query to get all results from your table users, and then do a cycle foreach to print all radio buttons, for example

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

then after the form when you press a button, you can get selected value because it will return the name from input, then you can query again the database for that specific user, and pass the values you want to another table in the same query or do 2 query, using the results. my english its not good, but i think you have the point