Jump to content

Adding a <form> in a loop duplaicate data going into database.


jacko_162

Recommended Posts

So i have an event page where it will show the next 3 events, then i need logged in users to click either of two buttons to say there attending or not attending.

i have the below code showing events, and the <form> and the submit and both buttons, but if i submit 1 button on event ID: 1 it inserts data into the database but for event ID: 2, and if i click the button on event ID :2 it also puts it into database with eventid:2

so no matter which event i click it seems to submit data with the latest event ID shown on page, and not for "each" event seperatly.

i have moved the submit query everywhere around and still same results or duplicate results if its in the loop.

i assume its looping the submit and only submitting the latests event ID.

 

<?php
if ($result = $con->query("SELECT * FROM events ORDER BY id ASC LIMIT 3")) {
if ($result->num_rows > 0) {
while ($row = $result->fetch_object()) {
$event_id = $row->id;
// set up table and echo data!
echo "<table border='1' cellpadding='2' width='50%'>";
echo "<tr><td>";
echo "<p><img src='images/raid_banners/" . $row->bannerimg . "'>" . $row->name . " (iLvl: " . $row->itemlevel . ")</p>";
echo "<p>Event Starts: " . $row->datestart . " - " . $row->timestart . "</p>";
echo "<p>Event Ends: " . $row->dateend . " - " . $row->timeend . "</p>";
echo "<p>Raid Lead: " . $row->raidlead . "</p>";
echo "<form action='' name='$event_id' method='post'>";
// Process and populate SELECT form element
echo "<select name=\"charname\">";
$sql = mysqli_query($con, "SELECT * FROM characters WHERE userid = $userid");
while ($row = $sql->fetch_assoc()){ echo "<option value=\"{$row['id']}\">{$row['charname']}</option>"; }
echo "</select>";
echo "<input type='hidden' name='raidid' value ='$event_id'>";
echo "<input type='hidden' name='action' value='submit' />";
echo "<input type=\"submit\" name=\"submit\" value=\"going\">";
echo "<input type=\"submit\" name=\"submit\" value=\"notgoing\">";
echo "</form></td></tr></table><br><br>"; 
 } 
 if(isset($_POST['action'])){
$charid = $_POST['charname'];
$submit = $_POST['submit'];
// Submit the data from dropdown in the form
mysqli_query($con,"INSERT INTO eventsignup (eventid, charid, userid, status) VALUES ('$event_id', '$charid', '$userid', '$submit')");  } 
}
else { echo "No results to display!"; } }
else { echo "Error: " . $con->error; }
$con->close();
?>

any help would be awesome. im tearing my hair out here.

Link to comment
Share on other sites

there coming from the $_POST and are set variables, these arnt the problem as there being posted into the table correctly.

but i did notice the event_id variable wasnt closed of correctly after you mentioned it.

<?php
if ($result = $con->query("SELECT * FROM events ORDER BY id ASC LIMIT 3")) {
if ($result->num_rows > 0) {
while ($row = $result->fetch_object()) {
$event_id = $row->id;
// set up table and echo data!
echo "<table border='1' cellpadding='2' width='50%'>";
echo "<tr><td>";
echo "<p><img src='images/raid_banners/" . $row->bannerimg . "'>" . $row->name . " (iLvl: " . $row->itemlevel . ")</p>";
echo "<p>Event Starts: " . $row->datestart . " - " . $row->timestart . "</p>";
echo "<p>Event Ends: " . $row->dateend . " - " . $row->timeend . "</p>";
echo "<p>Raid Lead: " . $row->raidlead . "</p>";
echo "<form action='' name='$event_id' method='post'>";
// Process and populate SELECT form element
echo "<select name=\"charname\">";
$sql = mysqli_query($con, "SELECT * FROM characters WHERE userid = $userid");
while ($row = $sql->fetch_assoc()){ echo "<option value=\"{$row['id']}\">{$row['charname']}</option>"; }
echo "</select>";
echo "<input type='hidden' name='raidid' value ='$event_id'>";
echo "<input type='hidden' name='action' value='submit' />";
echo "<input type=\"submit\" name=\"submit\" value=\"going\">";
echo "<input type=\"submit\" name=\"submit\" value=\"notgoing\">";
echo "</form></td></tr></table><br><br>"; 

 if(isset($_POST['action'])){
$charid = $_POST['charname'];
$submit = $_POST['submit'];
// Submit the data from dropdown in the form
mysqli_query($con,"INSERT INTO eventsignup (eventid, charid, userid, status) VALUES ('$event_id', '$charid', '$userid', '$submit')");  }  } 
}
else { echo "No results to display!"; } }
else { echo "Error: " . $con->error; }
$con->close();
?>

this is where i am at now, but when i click on any button in each event table i get the following in table;

4f4a2-clip-14kb.png?nocache=1

let me know if this makes no sense as im not very good at explaining.

 

Link to comment
Share on other sites

ok so $event_id is being set just under the While () loop

$event_id = $row->id;

and $userid is set in the session; $userid = $_SESSION['user_id']; (this isnt actually on the code i posted, apologies its in the header just under the include for the connection.php file

Link to comment
Share on other sites

on the contrary i am answering the question..... unless you're attempting to be cryptic then i have no clue

but as i said above, its looping the results and pulls the id from events table, and i assigned it to the $event_id variable, it then adds that into the eventssignup table, which is exactly as i want it to do.. but i only want the form to process the single table insert, not for all the looped results.

Link to comment
Share on other sites

I'll try.

$event_id is being assigned in the loop. Right. So it gets set to the first value, then the second, and it gets set for every row that came back from the query. All that happens inside the loop.

When that finishes and there are no more rows, what will $event_id be? And does that value have any relation whatsoever to the form that the user submitted?

Link to comment
Share on other sites

6 minutes ago, requinix said:

I'll try.

$event_id is being assigned in the loop. Right. So it gets set to the first value, then the second, and it gets set for every row that came back from the query. All that happens inside the loop.

When that finishes and there are no more rows, what will $event_id be? And does that value have any relation whatsoever to the form that the user submitted?

you are correct, it will keep getting the id from the query till the loop finishes.

yeah i need the form to send the ID of each table so i can collect a list of who is attending the event, im storing this in another table so i can correlate the data between the 2x tabels.

but if the user clicks attend on the top table i dont want the data being sent for the 2nd OR 3rd table.

here is a visual representation of what i wanted to achieve:

e99d4-clip-33kb.png?nocache=1

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.