FreakingOUT Posted June 3, 2014 Share Posted June 3, 2014 I spent several hours trying to figure this thing out. Thought I had it nailed, but still getting duplicate record entries into the MySQL DB when I do NOT want them. Here's the plot: People filling out the possible attendance form for a Ham Radio event *sometimes* bring a 2nd person (either a spouse or a friend). The 2nd person may, or may not, also has a Callsign which I need to put INSERT the same MySQL Callsign column. In any event, to also identify the 2nd person as coming 'with' the 1st person. MOST of the attendees are individuals with NO 2nd person. My entry form has these primary fields: callsign fullname AND... callsign2 fullname2 What I came up with was to process the MySQL INSERT for the primary callsign & fullname into their respective MySQL DB Columns (which works fine), and then........... immediately following the main Query INSERT, to do a substitution type thing depending on whether or not a form entry was made in the callsign2 field, AND/OR, the fullname2 field. This partially works, but if there is ONLY a primary callsign and fullname in the form, I'm still getting a duplicate record entry which includes the callsign in the `with` column (which should ONLY take place IF there is a 2nd person indicated). Confusing? Here is what I have been wrestling with to try and accomplish the objective, and now my eyes are glazed over ;-( // TRICKY PART HERE // If a 2nd Callsign AND a Fullname if ($callsign2 != ' ' && $fullname2 != ' ') { // Still make reference to the primary Callsign in the MySQL DB `with` column $with = $callsign; // Assignment to allow 2nd Callsign to be entered in the MySQL `callsign` column $callsign=$callsign2; $fullname=$fullname2; $sql="INSERT INTO `mytable` (`callsign`, `fullname`, `with`) VALUES ('$callsign', '$fullname', '$with')"; // If NO 2nd Callsign BUT a Fullname } elseif ($callsign2 = ' ' && $fullname2 != ' ') { // Make reference to the primary Callsign in the MySQL DB `with` column $with = $callsign; $callsign=$callsign2; $fullname=$fullname2; $sql="INSERT INTO `mytable` (`callsign`, `fullname`, `with`) VALUES ('$callsign', '$fullname', '$with')"; } else { // The only thing I could thing of to (hopefully) NOT make a 2nd entry // record in the MySQL DB IF there is NO 2nd person referenced $with = $callsign; } if (!mysqli_query($con,$sql)) { die('Error: ' . mysqli_error($con)); } This mostly works EXCEPT if only a single (primary) person entry. The recap the objetives: 1. If ONLY a primary/single person entry on the form: * callsign & fullname get INSERTed into the `callsign` and `fullname` columns in the DB as ONLY one record entry 2. If BOTH a primary and 2nd person on the form: A. IF the 2nd person has a Callsign, then the 2nd record entry would be: * callsign2 & fullname2 get INSERTed into the `callsign` & `fullname` columns in the 2nd DB as a separate record entry * callsign of the primary person also gets INSERTED into the `with` column in the same 2nd DB record entry B. IF the 2nd person does NOT have a callsign, then the 2nd record entry would be: * fullname2 gets INSERTed into the `fullname` column in the DB as a separate 2nd DB record entry * callsign of the primary person also gets INSERTED into the `with` column in the same 2nd DB record entry I obvioiusly have overlooked something, but just can't seem to figure it out at this point {SIGH}. Thanks for any enlightenment. -FreakingOUT Quote Link to comment https://forums.phpfreaks.com/topic/288953-unique-form-entry-to-mysql-db-situation/ Share on other sites More sharing options...
Solution mac_gyver Posted June 3, 2014 Solution Share Posted June 3, 2014 (edited) so, if there is a second person, there would be a nonempty $fullname2 (your posted code is testing if it is not equal to a space ' ', which is likely the cause of your extra inserted record.) the only logic would be to test if $fullname2 is present and use $callsign for the `with` field. $callsign2 would use used as is, it's either a callsign or an empty string. if($fullname2 != ''){ // note, this is an empty string '', not a space ' ' // second person fields have information in them $sql="INSERT INTO `mytable` (`callsign`, `fullname`, `with`) VALUES ('$callsign2', '$fullname2', '$callsign')"; if (!mysqli_query($con,$sql)) { die('Error: ' . mysqli_error($con)); } } Edited June 3, 2014 by mac_gyver Quote Link to comment https://forums.phpfreaks.com/topic/288953-unique-form-entry-to-mysql-db-situation/#findComment-1481735 Share on other sites More sharing options...
FreakingOUT Posted June 3, 2014 Author Share Posted June 3, 2014 so, if there is a second person, there would be a nonempty $fullname2 (your posted code is testing if it is not equal to a space ' ', which is likely the cause of your extra inserted record.) the only logic would be to test if $fullname2 is present and use $callsign for the `with` field. $callsign2 would use used as is, it's either a callsign or an empty string. if($fullname2 != ''){ // note, this is an empty string '', not a space ' ' // second person fields have information in them $sql="INSERT INTO `mytable` (`callsign`, `fullname`, `with`) VALUES ('$callsign2', '$fullname2', '$callsign')"; if (!mysqli_query($con,$sql)) { die('Error: ' . mysqli_error($con)); } } OMG - methinks you are a GENIUS, mac_gyver !!! In my wildest dreams I would have never imagined it could be done this way. An elegantly simple solution to what I thought was mega-complicated. I have learned a LOT here, and my bad forgetting that any field (column) data can be placed in the VALUES list - even from *other* fields on a form. I kept re-testing all 3 scenarios and was blown away to finally see things working as intended. Now I can finally go to bed relieved as people have started filling out the form data. No more having to email them double-checking about a 2nd person {SIGH}. Thanks very much again. -FreakingOUT (But not anymore tonight / this morning :-) Quote Link to comment https://forums.phpfreaks.com/topic/288953-unique-form-entry-to-mysql-db-situation/#findComment-1481737 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.