CUTCODEDOWN
Minimalist Semantic Markup

Welcome Guest
Please Login or Register

If you have registered but not recieved your activation e-mail in a reasonable amount of time, or have issues with using the registration form, please use our Contact Form for assistance. Include both your username and the e-mail you tried to register with.

Author Topic: Mental Block Again - Unique Information on two possible tables...  (Read 1090 times)

GrumpyYoungMan

  • Hero Member
  • *****
  • Posts: 792
  • Karma: +8/-0
    • Grumpy Young Man
I have two tables: users and vehicles, when the form is submitted it adds the user information to the user table and then the vehicle information into the vehicle table, which is simple, but the problem I have come across is how to handle the error management if
  • the user information
  • the vehicle information
already exists I do actually have a third table mots which only gets updated with the vehicle and user id from the first two queries...

I can not merge it into one query as if it fails I won't know why and secondly if it passes the user information but fails the vehicle I will then run into trouble if they resubmit the form as their details would be already into the database - but under a failed table? or do I just run a delete on the user table if hit an error? but that would cause a break in the auto-increment numbering on the user/and or vehicle tables?

Wasn't sure what code to post, so haven't posted any, more using this is a sounding board - also I am trying to insert the values and not select/insert.

« Last Edit: 21 Feb 2020, 09:06:10 am by GrumpyYoungMan »
Trying to learn a new trick to prove old dogs can learn new ones...

Total Novice have-a go Amateur Programmer - not sure that is the right thing to say... but trying to learn...

Dave

  • Junior Member
  • *
  • Posts: 38
  • Karma: +12/-0
Re: Mental Block Again - Unique Information on two possible tables...
« Reply #1 on: 21 Feb 2020, 10:26:33 am »
If your tables are InnoDB use a transaction to "bundle" the inserts and roll back the changes if something doesn't go as expected.

And do not be concerned with whether or not your auto increment values are sequential, it doesn't matter at all.
Dave

GrumpyYoungMan

  • Hero Member
  • *****
  • Posts: 792
  • Karma: +8/-0
    • Grumpy Young Man
Re: Mental Block Again - Unique Information on two possible tables...
« Reply #2 on: 21 Feb 2020, 10:58:15 am »
If your tables are InnoDB use a transaction to "bundle" the inserts and roll back the changes if something doesn't go as expected.

And do not be concerned with whether or not your auto increment values are sequential, it doesn't matter at all.
I'll look into that as that sounds better and cleaner than doing this:
Code: [Select]
echo 'It looks like it may be safe - so lets try to insert the vehicle...!';

$vehicleStatus = vehicles_Create( ['vrm' => vrm_Clean($_POST['vrm']), 'uid' => $clientStatus ] );

if( is_array($vehicleStatus) ) {

// Add the User Insert Errors to the maater insert error array
$insertErrors = array_merge($insertErrors, $vehicleStatus);

echo "Type: 1";
}
else {

$insertMOT = $DB->prepare("
INSERT INTO
mots
(
expiry, user_id, vehicle_id, added
)
VALUES
(
:expiry, :uid, :vid, NOW()
)
");

$insertMOT->execute( [ ':expiry' => $_POST['expiry'], ':uid' => $clientStatus, ':vid' => $vehicleStatus ] );

$MOTId = $DB->lastInsertId();

$updateVehicle = $DB->prepare("
UPDATE
vehicles
SET
mot_id=:motid
WHERE
id=:vehicleId
");

$updateVehicle->execute( [ ':motid' => $MOTId, ':vehicleId' => $vehicleStatus ] );

header("Location: ?module=MOT&action=View&id=$MOTId");

}

}

exit(mot_AddForm($insertErrors));

The only problem I see with the transaction method is I was using three queries - from three functions, so I would have merge them into the single query?

Because I am going in the order of user, vehicle and then the mot record? or if the transaction is started at the very query, the rollback will unroll all the queries?
« Last Edit: 21 Feb 2020, 11:09:13 am by GrumpyYoungMan »
Trying to learn a new trick to prove old dogs can learn new ones...

Total Novice have-a go Amateur Programmer - not sure that is the right thing to say... but trying to learn...

GrumpyYoungMan

  • Hero Member
  • *****
  • Posts: 792
  • Karma: +8/-0
    • Grumpy Young Man
Re: Mental Block Again - Unique Information on two possible tables...
« Reply #3 on: 21 Feb 2020, 12:29:09 pm »
Code: [Select]
$DB->beginTransaction();

try {

// USER:
$createUser = $DB->prepare(user_Query_Create());

// Vehicle:
$createVehicle = $DB->prepare(vehicles_Query_Create());



    $createUser->execute([ ':firstname' => $_POST['firstname'], ':surname' => $_POST['surname'], ':email' => $_POST['email'], ]);

    $userId =  $DB->lastInsertId();

    $createVehicle->execute( [ ':vrm' => vrm_Clean($_POST['vrm']), ':uid' => $DB->lastInsertId() ]);
   
    $vehicleId = $DB->lastInsertId();
// MOT:
    $insertMOT = $DB->prepare("
INSERT INTO
mots
(
expiry, user_id, vehicle_id, added
)
VALUES
(
:expiry, :uid, :vid, NOW()
)
");

$insertMOT->execute( [ ':expiry' => $_POST['expiry'], ':uid' => $userId, ':vid' => $vehicleId ] );

$MOTId = $DB->lastInsertId();

$updateVehicle = $DB->prepare("
UPDATE
vehicles
SET
mot_id=:motid
WHERE
id=:vehicleId
");

$updateVehicle->execute( [ ':motid' => $MOTId, ':vehicleId' => $vehicleId ] );


    $DB->commit();


} catch (\Exception $e) {
   
    $DB->rollback();
    throw $e;

}
Something like that? needs tidying up and the variables for nothing removing...
Trying to learn a new trick to prove old dogs can learn new ones...

Total Novice have-a go Amateur Programmer - not sure that is the right thing to say... but trying to learn...

GrumpyYoungMan

  • Hero Member
  • *****
  • Posts: 792
  • Karma: +8/-0
    • Grumpy Young Man
Re: Mental Block Again - Unique Information on two possible tables...
« Reply #4 on: 22 Feb 2020, 10:50:42 am »
How would you handle this logic problem?

Obviously my form is only good for creating the vehicle, user and MOT record at the same time, but I could have the situation where the user could be already created along with the vehicle as I plan to use the vehicle for another part of my script so in theory they could just end up in the situation of needing an MOT record and that is before you throw in multiple vehicles? 

So do I add multiple tick boxes to my existing form saying existing vehicle, user? And if selected create the new records? Or Have a separate form so they can add MOT records via there user list?
Trying to learn a new trick to prove old dogs can learn new ones...

Total Novice have-a go Amateur Programmer - not sure that is the right thing to say... but trying to learn...

Dave

  • Junior Member
  • *
  • Posts: 38
  • Karma: +12/-0
Re: Mental Block Again - Unique Information on two possible tables...
« Reply #5 on: 22 Feb 2020, 12:24:10 pm »
Something like that? needs tidying up and the variables for nothing removing...

Yep, that's how you can do it. You've bundled all of your queries into a transaction and either they all work correctly or they fail and nothing happens.
Dave

 

SMF spam blocked by CleanTalk

Advertisement