@WolfShade ; Sorry have tried this 10 different ways in MySQL and cannot get it to work.
IF EXISTS(SELECT clientid FROM `client_phone` WHERE `clientid` = 22)
THEN
UPDATE client_phone SET `phone_number`=888, `country_code`=2 WHERE `clientid` = 22
Think that was the last way round I tried it (clientid DOES exist)
Sorry know I am being really dense but just cannot get the syntax right. Took the BEGIN and END out not sure if I should but made no difference.
EDIT here is the current code:
$sql =
'UPDATE client, client_email, client_phone
SET
first = :first,
last = :last,
initials = :initials,
clientname = :clientname,
phone_number = :phone_number,
email = :email
WHERE client.id = :id';
// All the same - value and var :=place holder
$s = $pdo->prepare($sql);
$s->bindValue(':first', $_POST['first']);
$s->bindValue(':id', $_POST['id']);
$s->bindValue(':clientname', $_POST['clientname']);
$s->bindValue(':last', $_POST['last']);
$s->bindValue(':initials', $_POST['initials']);
$s->bindValue(':phone_number', $_POST['phone_number']);
$s->bindValue(':email', $_POST['email']);
$s->execute();
Works fine if the clientid exists otherwise it updates the first entry in the table
The code posted by WolfShade is specific for tsql, the dialect of SQL used by Sybase and SQL server, and will not work in Mysql. In Mysql you would use their specific version of INSERT, i.e. INSERT … ON DUPLICATE. You could look for this in the Mysql documentation or do an Internet search for more details.
@r937 ; I realy really did think you had it. First tried with id=10 and it failed on foreign key constraint - perfect.
Tried it with existing id not in this table - perfect.
Tried it with id that existed in the client_phone table it accepted BUT produced duplicate rows.
I am either being dense or not explaining myself. I am trying to update a record if it exists and, if it does not, I am trying to insert it.
Maybe I am missing something basic. It really does not seem that this should be difficult and yes I have spent HOURS reading the MySQL documentation. (The search function is great when you search for something it comes back in all the languages on gods’s earth!) I can find little useful info on EXISTS which I think I may need but cannot make work.
I can do all sorts of fixes for this specific instance but this will come up all over the place. Bottle of scotch (virtual) for right answewr.
the ON DUPLICATE KEY approach is by far the easiest way
if you have a situation where you don’t want duplicates on a certain column (or combination of columns), then just go ahead and declare it (or them) UNIQUE
the alternative is to break out and flex your START TRANSACTION … COMMIT/ROLLBACK chops
The clientid is unique but I want to update the record. But with the current solution I get and extra copy because we have told it to ignore the duplicate key.
Is there no way to test if a record a certain field-value exists? And then stinging that together with an IF statement and then either do UPDATE or INSERT. From a programming standpoint it seems incredibly basic but in MySQL it seems not to be doable - certainly not easily.
I have not looked at database rollbacks since 1979!!! I am just getting to grips with very basic MySQL and just trying to do a small (10 table) DB without (at this stage) any proper management tools (rollback transaction clash potection - there are only two users).
I really did thing EXISTS was the way to go.but I just could not make it work.
This would be ideal but I am ending up with multiple values for the foreign key which I cannot have (well I suppose I could and through in a timestamp element) and get rid of all but the latest but that is horribly inelegant.
Greatful for any idea if not will have to fudge this but seriously clinky coded.
The $_POST[id] is the id from the main client table that is the foreign key in this one. So I am checking to see if an entry exists for that specific user.
Sorry not at all clear but just wanted to share the results of all this.
HEY HEY HEY @SpacePhoenix ; just noticed you are from Poole I was born in Poole but the family lived in Parkstone…
Actually I am 100% sure I did not explain myself clearly
But much more important than that is why testing for “= ZERO” fails but “>ZERO works”. Really got me confused. Not a problem just I like to understand what is going on.
Thanks for all you help with this and other questions.