Solved

select mysql myisam table by secondary key

Posted on 2014-02-01
5
371 Views
Last Modified: 2014-02-15
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
Comment
Question by:peter_lawrie
  • 4
5 Comments
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39827121
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
 

Author Comment

by:peter_lawrie
ID: 39827621
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
 

Author Comment

by:peter_lawrie
ID: 39828133
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
 

Accepted Solution

by:
peter_lawrie earned 0 total points
ID: 39848240
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
 

Author Closing Comment

by:peter_lawrie
ID: 39861156
found a stupid mistake in the code - sorry for wasting anyone's time.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now