jacko_162 Posted December 13, 2018 Share Posted December 13, 2018 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. Quote Link to comment https://forums.phpfreaks.com/topic/308012-adding-a-in-a-loop-duplaicate-data-going-into-database/ Share on other sites More sharing options...
requinix Posted December 13, 2018 Share Posted December 13, 2018 Those $event_id and $charid and $userid and $submit variables... Where do you think they're coming from? Where should they be coming from? Quote Link to comment https://forums.phpfreaks.com/topic/308012-adding-a-in-a-loop-duplaicate-data-going-into-database/#findComment-1562806 Share on other sites More sharing options...
jacko_162 Posted December 13, 2018 Author Share Posted December 13, 2018 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; let me know if this makes no sense as im not very good at explaining. Quote Link to comment https://forums.phpfreaks.com/topic/308012-adding-a-in-a-loop-duplaicate-data-going-into-database/#findComment-1562807 Share on other sites More sharing options...
requinix Posted December 13, 2018 Share Posted December 13, 2018 Yes, you do have $charid and $submit coming from $_POST, but what about the other two? Quote Link to comment https://forums.phpfreaks.com/topic/308012-adding-a-in-a-loop-duplaicate-data-going-into-database/#findComment-1562808 Share on other sites More sharing options...
jacko_162 Posted December 13, 2018 Author Share Posted December 13, 2018 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 Quote Link to comment https://forums.phpfreaks.com/topic/308012-adding-a-in-a-loop-duplaicate-data-going-into-database/#findComment-1562809 Share on other sites More sharing options...
Barand Posted December 13, 2018 Share Posted December 13, 2018 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? Quote Link to comment https://forums.phpfreaks.com/topic/308012-adding-a-in-a-loop-duplaicate-data-going-into-database/#findComment-1562810 Share on other sites More sharing options...
jacko_162 Posted December 13, 2018 Author Share Posted December 13, 2018 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. Quote Link to comment https://forums.phpfreaks.com/topic/308012-adding-a-in-a-loop-duplaicate-data-going-into-database/#findComment-1562811 Share on other sites More sharing options...
Barand Posted December 13, 2018 Share Posted December 13, 2018 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? Quote Link to comment https://forums.phpfreaks.com/topic/308012-adding-a-in-a-loop-duplaicate-data-going-into-database/#findComment-1562814 Share on other sites More sharing options...
jacko_162 Posted December 13, 2018 Author Share Posted December 13, 2018 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? Quote Link to comment https://forums.phpfreaks.com/topic/308012-adding-a-in-a-loop-duplaicate-data-going-into-database/#findComment-1562815 Share on other sites More sharing options...
Barand Posted December 14, 2018 Share Posted December 14, 2018 You are not answering the question. Quote Link to comment https://forums.phpfreaks.com/topic/308012-adding-a-in-a-loop-duplaicate-data-going-into-database/#findComment-1562820 Share on other sites More sharing options...
jacko_162 Posted December 14, 2018 Author Share Posted December 14, 2018 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. Quote Link to comment https://forums.phpfreaks.com/topic/308012-adding-a-in-a-loop-duplaicate-data-going-into-database/#findComment-1562833 Share on other sites More sharing options...
requinix Posted December 14, 2018 Share Posted December 14, 2018 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? 1 Quote Link to comment https://forums.phpfreaks.com/topic/308012-adding-a-in-a-loop-duplaicate-data-going-into-database/#findComment-1562834 Share on other sites More sharing options...
jacko_162 Posted December 14, 2018 Author Share Posted December 14, 2018 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: Quote Link to comment https://forums.phpfreaks.com/topic/308012-adding-a-in-a-loop-duplaicate-data-going-into-database/#findComment-1562836 Share on other sites More sharing options...
jacko_162 Posted December 14, 2018 Author Share Posted December 14, 2018 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 Quote Link to comment https://forums.phpfreaks.com/topic/308012-adding-a-in-a-loop-duplaicate-data-going-into-database/#findComment-1562841 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.