Solved

select mysql myisam table by secondary key

Posted on 2014-02-01
5
366 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 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
found a stupid mistake in the code - sorry for wasting anyone's time.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

743 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

16 Experts available now in Live!

Get 1:1 Help Now