Link to home
Start Free TrialLog in
Avatar of peter_lawrie
peter_lawrieFlag for United Kingdom of Great Britain and Northern Ireland

asked on

select mysql myisam table by secondary key

Hi
I have been asked to recreate a 10-year old access system in mySQL and PHP.
The data has been extracted from access and 30 tables created in a mysql database on a Centos linux server. I have written a dozen or so php scripts which are working but have now hit a problem.
I have a table of clients with primary key client_number and a tbl of client_notes with primary key note_id and secondary key client_number.
I have an update program to which I enter a client_id 'sel_id' which has a mySQL SELECT FROM client WHERE client_number='sel_id' followed by a mysql SELECT FROM client_notes WHERE client_number='sel_id'.
Then I create a form, update the data and then REPLACE INTO client and client_notes.
client updates correctly but every time a new record is created in client_notes
I have confirmed that the initial SELECT FROM client_notes fails to read the record I want.

(There is actually a third table involved as well with primary key client_number and that SELECTs correctly and REPLACEs correctly.
Surely I should be able to SELECT by the secondary key.
I've tried googling for an answer but all I can find is arguments about INNODB in place of MySQL
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Can you please create a small test data set from the existing tables and post that along with the output you want to get from the queries?   Once we see the data and the desired output we can almost certainly show you the code to bridge the gap.
Avatar of peter_lawrie

ASKER

phpHi
Thanks for the response.
Please see attached the code and the three tables concerned - I have extracted the table set up and the first few records. (these three tables are 5Mb with 18000 records in tblClient)
In tblClientNotes.sql the first few records are those created by my code testing on client 18365, it creates a new tblClientNotes record each time, 6572, 6573, 6574 etc because it fails to SELECT existing record successfully.
tfpupdclient.php
tfpupdclient2.php
tblClient.sql
tblClient-Personal-Notes.sql
tblClientNotes.sql
updclient2-output.png
Some additional detail -
I amended
if (mysqli_num_rows($get_tblClientNotes_res) > 0)  to > 0 rather than ==1
So as there a multiple entries in the table due to the problem I at least managed to read the last record from the table for client_number. - This displays correctly, however the REPLACE still adds another, incrementing note_id rather than replacing it.
I can't see why it does this
       //update tblClientNotes table
                $upd_tblClientNotes_sql = "REPLACE INTO tblClientNotes (client_number, note_id, site_code, notes, entry_date)
                                                   VALUES ('".$_POST["client_number"]."', '".$_POST["note_id"]."',  '".$_POST["site_code"]."',
                                                            '".$_POST["notes"]."', '".$_POST["entry_date"]."'
                                                        )";
                $upd_tblClientNotes_res = mysqli_query($mysqli, $upd_tblClientNotes_sql) or die(mysqli_error($mysqli));

        mysqli_close($mysqli);
ASKER CERTIFIED SOLUTION
Avatar of peter_lawrie
peter_lawrie
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
found a stupid mistake in the code - sorry for wasting anyone's time.