PDA

View Full Version : Help! Not sure how to do something..



Luke
04-06-2015, 01:39 PM
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


*AI*
1 (or row['id'])
PU (or one of the radio buttons)
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

Luke
05-06-2015, 08:23 AM
I figured it out. For anyone interested:



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

<tbody>
<form id="form" name="form" method="post" >
<?php
$idx = 0;
do {
?>
<tr>
<td><?php echo $row['forename']. ' ' .$row['surname']; ?></td>
<td><input type="radio" name="radioName[<?php echo $idx; ?>]" value="PU" >PU</td>
<td><input type="radio" name="radioName[<?php echo $idx; ?>]" value="PC">PC</td>
<td><input type="radio" name="radioName[<?php echo $idx; ?>]" value="AA">AA</td>
<td><input type="radio" name="radioName[<?php echo $idx; ?>]" value="S">S</td>
<td><input type="radio" name="radioName[<?php echo $idx; ?>]" value="AWOL">AWOL</td>
</tr>
<input type = "hidden" value = "<?php echo $row['id']; ?>" name="userId[<?php echo $idx; ?>]">

<?php $idx ++; ?>
<?php } while ($row = mysql_fetch_assoc($result)); ?>
</tbody>
<input type = "submit" value = "Register" >
</form>

</table>


</br>
<?php

if (isset($_POST['radioName'][0])) {

foreach ($_POST['radioName'] as $index => $value) {


if (empty($value)) {
echo "You must select the radio button.";
break;
}
$record = $_POST['radioName'][$index];
$userId = $_POST['userId'][$index];

$query = "INSERT INTO attendance(user_id, record, date)
VALUE ('$userId','$record', NOW())";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
}
echo "Data stored successly!";
}

?>

Want to hide these adverts? Register an account for free!