Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 380
  • Last Modified:

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
0
peter_lawrie
Asked:
peter_lawrie
  • 4
1 Solution
 
Ray PaseurCommented:
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.
0
 
peter_lawrieAuthor Commented:
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
0
 
peter_lawrieAuthor Commented:
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);
0
 
peter_lawrieAuthor Commented:
Silly me, I had not extracted the file primary key in the original read, so of course it would not write the same record back. Once I fixed my mistake the code now works correctly
0
 
peter_lawrieAuthor Commented:
found a stupid mistake in the code - sorry for wasting anyone's time.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now