Jump to content
jacko_162

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

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.

Share this post


Link to post
Share on other sites

Those $event_id and $charid and $userid and $submit variables... Where do you think they're coming from? Where should they be coming from?

Share this post


Link to post
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.

 

Share this post


Link to post
Share on other sites

Yes, you do have $charid and $submit coming from $_POST, but what about the other two?

Share this post


Link to post
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

Share this post


Link to post
Share on other sites
1 minute ago, jacko_162 said:

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

So when you do the insert, what value is going to be in $event_id?

Share this post


Link to post
Share on other sites
4 minutes ago, Barand said:

So when you do the insert, what value is going to be in $event_id?

$event_id = $row->id;

its the ID of the event from the events table query at the top of the code.

77b53-clip-18kb.png?nocache=1

Share this post


Link to post
Share on other sites

Yes, but that query reads more than one record, so - same question again

23 minutes ago, Barand said:

when you do the insert, what value is going to be in $event_id?

 

Share this post


Link to post
Share on other sites
6 minutes ago, Barand said:

Yes, but that query reads more than one record, so - same question again

 

it reads all the id's in a loop?

Share this post


Link to post
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.

Share this post


Link to post
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?

  • Thanks 1

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

ok i figured it out, my INSERT query was in the LOOP!!!!

so it ran the insert 3 times, hence the data being inserted for each submit.

 

problem is now solved

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

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.