Jump to content

Unique form entry to MySQL DB situation


FreakingOUT
Go to solution Solved by mac_gyver,

Recommended Posts

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

 

 

 

Link to comment
Share on other sites

  • Solution

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 by mac_gyver
Link to comment
Share on other sites

 

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 :-)

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.