mario mario - 4 months ago 15
SQL Question

Disable input fields based on timestamp of mysql timestamp

I'm programming a betting game for football tournaments/cups. In this game users shall be able to place their bets in a form until 5 minutes before the match starts. After that point in time the fields shall be disabled/greyed out so that it still shows the entered value but is not editable anyore.

How can I disable these "score input fields" automatically based on the

time
and
date
(saved for each game in the mysql table)?

If you need to see more of the code or need more info about the mysql tables, please ask.

SQL Query & table

$records = mysqli_query($conn, "
SELECT
sp.spiel_id,
DATE_FORMAT(sp.datum, \"%d.%m.%Y\") AS datum,
DATE_FORMAT(sp.zeit, \"%H:%i\") AS zeit,
sp.heimteam_id,
sp.gastteam_id,
sp.tore_heimteam,
sp.tore_gastteam,
ht.teamname AS heimteam_name,
at.teamname AS gastteam_name,
t.match_id,
t.user_id,
t.tipp_heim,
t.tipp_gast,
t.punkte_tipp,
u.user_id,
u.username
FROM
spielplan sp
LEFT JOIN mannschaften ht
ON sp.heimteam_id = ht.mannschafts_id
LEFT JOIN mannschaften at
ON sp.gastteam_id = at.mannschafts_id
LEFT JOIN tipps t
ON sp.spiel_id = t.match_id
LEFT JOIN users u
ON t.user_id = u.user_id
WHERE
username = '".$_SESSION["username"]."'
ORDER BY
spiel_id ASC
");
while($fields = mysqli_fetch_assoc($records)) {
?>
<tr>
<input type="hidden" name="spiel_ids[]" value="<?php echo $fields["spiel_id"] ?>">
<td><?php echo $fields["datum"] ?></td>
<td><?php echo $fields["zeit"] ?></td>
<td><?php echo $fields["heimteam_name"] ?></td>
<td><?php echo $fields["tore_heimteam"] ?></td>
<td>:</td>
<td><?php echo $fields["tore_gastteam"] ?></td>
<td><?php echo $fields["gastteam_name"] ?></td>
<input type="hidden" name="user_ids[]" value="<?php echo $fields["user_id"] ?>">
<td><input type="tel" maxlength="2" size="5" name="tipps_heim[]" value="<?php echo $fields["tipp_heim"] ?>"></td>
<td><input type="tel" maxlength="2" size="5" name="tipps_gast[]" value="<?php echo $fields["tipp_gast"] ?>"></td>
<td><?php echo $fields["punkte_tipp"] ?></td>
</tr>
<?php
}
?>

Answer

I would not recommend you doing this, but you can try this:

For example it's your field that you want to disable

<input name="spiel_ids[]" value="<?php echo $fields["spiel_id"] ?>">

You can declare $disabled variable based on you db record:

    <?php 
    $disabled = "";

    //you check if you date is in future, then you disable field
    if (date('Y-m-d', strtotime($fields['datum'])) > date('Y-m-d')) { 

      $disabled = "disabled='disabled'";
    } ?>

And put this variable to your output string:

<input <?php echo $disabled ?> name="spiel_ids[]" value="<?php echo $fields["spiel_id"] ?>">