Reputation: 47
I have a database with different activities, their dates and descriptions and I am doing a table with it that looks like this.
So basically, when someone use the submit button their user id (id_user), the id of the activity (id_f) and the maximum number of people in an activity (nb_max) is sent to my database named 'atelier'.
It works but it doesn't stop when the button is pushed, for instance the user is registering to all of the activities at once (I only want him to be registered to the activity where the submit button is) ! There is an image of my database 'atelier' after submitting the button.
Here is my html/php code :
<thead>
<tr>
<!-- Creating the title of my table -->
<th>Date de la formation</th>
<th>Debut</th>
<th>Fin</th>
<th>Description</th>
<th>Nombre maximum de participants</th>
<th>Créateur</th>
<th>État</th>
</tr>
</thead>
<?php
// $contenu = $bdd->query('SELECT * FROM formation ORDER BY date_f');
// Creating the row of my table
while ($donnees = $contenu->fetch())
{
?>
<tr>
<td><?php echo $donnees['date_f']; ?></td>
<td><?php echo $donnees['h_debut']; ?>h</td>
<td><?php echo $donnees['h_fin']; ?>h</td>
<td><?php echo $donnees['contenu']; ?></td>
<td><?php echo $donnees['nb_max']; ?></td>
<td><?php echo $donnees['id_user']; ?></td>
<td>
<?php
$id_user = $_SESSION['id_user'];
$id_f = $donnees['id_f'];
$nb_max = $donnees['nb_max'];
// Checking if the user has already registered to the activity
$reqAtelier = $bdd->prepare("SELECT * FROM atelier WHERE id_user = ? AND id_f = ?");
$reqAtelier->execute(array($id_user, $id_f));
$atelierExist = $reqAtelier->rowCount();
// If not, he can register like so
if($atelierExist == 0) {
if(isset($_POST['forminscription'])) {
$insertmbr = $bdd->prepare("INSERT INTO atelier (id_f, nb_max, id_user) VALUES(?, ?, ?)" );
$insertmbr->execute(array($id_f, $nb_max, $id_user));
header("LOCATION: activites.php"); // Refreshing the page after sending the information
}
?>
<form method="POST" action="activites.php">
<input type="submit" name="forminscription" value="Register" />
</form>
<?php
}
// Otherwise, the submit button changes value and doesn't do anything
// This works fine since after pushing my submit button once, all of the button turn to 'Already registered'
else{
?>
<form method="POST" action="activites.php">
<input type="submit" name="forminscription" value="Already registered" />
</form>
<?php
}
?>
</td>
</tr>
<?php
}
?>
I am using fetch() to create my table and I think that is why I am getting all of the activities at once. But I can't exit my while statement, so this is why I am trying to exit my if statement or to stop my button from working.
Do you know how can I fix my problem and only get the values of the activity where my submit button is ? Is there a solution to do this without using javascript ?
I tried to be as clear as possible, so I hope you'll understand my problem.
Many thanks in advance.
Upvotes: 0
Views: 118
Reputation: 6006
Your insertion code is inside a loop, that's why you get multiple rows inserted. What you want to do instead is sending more parameters in your form.
<?php
$id_user = $_SESSION['id_user'];
$result = $bdd->prepare("SELECT id_f FROM atelier WHERE id_user = ? GROUP BY id_f");
$result->execute(array($id_user));
$userActivities = $result->fetchAll(PDO::FETCH_COLUMN);
if(isset($_POST['id_f'], $_POST['nb_max'])) {
$id_f = $_POST['id_f'];
$nb_max = $_POST['nb_max'];
// Checking if the user has already registered to the activity
// If not, he can register like so
if(!in_array($id_f, $userActivities)) {
$insertmbr = $bdd->prepare("INSERT INTO atelier (id_f, nb_max, id_user) VALUES(?, ?, ?)" );
if ($insertmbr->execute(array($id_f, $nb_max, $id_user))) {
$userActivities[] = $id_f;
}
// No need to refresh the page, since we insert first then we fetch
}
}
?>
<thead>
<tr>
<!-- Creating the title of my table -->
<th>Date de la formation</th>
<th>Debut</th>
<th>Fin</th>
<th>Description</th>
<th>Nombre maximum de participants</th>
<th>Créateur</th>
<th>État</th>
</tr>
</thead>
<?php
// $contenu = $bdd->query('SELECT * FROM formation ORDER BY date_f');
// Creating the row of my table
while ($donnees = $contenu->fetch())
{
?>
<tr>
<td><?php echo $donnees['date_f']; ?></td>
<td><?php echo $donnees['h_debut']; ?>h</td>
<td><?php echo $donnees['h_fin']; ?>h</td>
<td><?php echo $donnees['contenu']; ?></td>
<td><?php echo $donnees['nb_max']; ?></td>
<td><?php echo $donnees['id_user']; ?></td>
<td>
<?php if (!in_array($donnees['id_f'], $userActivities)): ?>
<form method="POST" action="activites.php">
<input type="hidden" name="id_f" value="<?php echo $donnees['id_f']; ?>" />
<input type="hidden" name="nb_max" value="<?php echo $donnees['nb_max']; ?>" />
<input type="submit" name="forminscription" value="Register" />
</form>
<?php else: ?>
<p>Already registered</p>
<?php endif; ?>
</td>
</tr>
I changed your code a bit. First, I get all of the user's activities. I did that because you don't want to have a query inside a loop, that's bad performance. Then inside the loop and on the insertion I check if the user has the activity simply by checking if it's in the array.
Note: For the sake of simplicity I went with array of values. Better performance will be with some kind of a hash-table, and check with isset()
instead of in_array()
.
Example:
$userActivitiesHash = array_flip($userActivities);
Then:
isset($userActivitiesHash[$id_f])
And:
!isset($userActivitiesHash[$donnees['id_f']])
Upvotes: 3