I am developing Android application that sends HTTP requests to a server using php and mysql.
The user has to fill in two fields. let's say A and B these will be added to two columns of a table (col A and col B. However, I want to add the contents of A to B after that. it works fine.
I am having a problem in checking the duplicates of A in B. The result i want is A as the user enters and B of the content of A+B with no duplicates.
This is what i am using
$A = $_POST['A'];
$last_inserted_id = mysql_insert_id();
$updateTable = mysql_query("UPDATE Table SET B=IFNULL(CONCAT(B, '$A'), '$A')WHERE _id='$last_inserted_id'");
To make it clearer, B contains only words not a sentence, however, A is a sentence.
so I stemmed the sentence A .. for example A="I want to learn programming" -$A= "learn programming".
So if B is programming .. the final result must be learn and programming only . Now I'm getting learn programming learn.
If this record was just inserted, you probably could've put that data in there in the first place. If this is a subsequent request, then mysql_insert_id() cannot be trusted, as other records might've been inserted.
Is there anything that precludes you from putting the data in there in the first place?
If you want a method to append to an existing field, you could simplify this by defaulting B to be an empty string, making the IFNULL check on your CONCAT call redundant.
Related
I feel like there is probably a very simple answer for this, but I've spent about 20 minutes searching and can't find anything.
Basically, I am using PHP to query a table and output the results as a list, using the primary key column (COL_1) of the table to create a link for each record that will bring the user to a detail page for that record. It works fine when the data in COL_1 is a straight-forward string such as "TEST". The edit link will then be detail.php?COL_1=TEST The detail page works by querying the database using the data passed by the link. So in this case it would do a select on the table where COL_1 = 'TEST' and return the correct record.
However, when new line characters are stored in COL_1 things get a bit complicated. For instance, if 'TEST\r\nTEST' is stored in COL_1, when the original query of the entire table is done, $row['COL_1'] for that line will give me 'TESTTEST', which gets passed to the detail page as detail.php?COL_1=TESTTEST, the detail page does a select on the table where COL_1 = 'TESTTEST', and it returns nothing.
However, if I manually link to detail.php?COL_1=TEST\r\nTEST the detail page will query on 'TEST\r\nTEST' and return the correct record.
So basically what I need is a way to do a query and have $row['COL_1'] return 'TEST\r\nTEST' instead of 'TESTTEST'. Does this make sense? How would I go about doing this?
As for why the table is set up like this, don't ask me. I didn't design it. I'd never design keys that can include line breaks like this. But I do have to interact with this table. Bah.
You should encode values that are passed in the URL:
echo urlencode("TEST\r\nTEST");
However, why would TEST\r\nTEST be a primary key? That's crazy. Maybe you need to rethink how you are doing things. Primary keys as integers work nicely.
The values I have are in a session. I can't use JOIN (I assume) because that is for multiple tables. I'm working with one table.
The table is called 'VEHICLES'. Each row has a field called 'car' and has an AUTO INCREMENTED ID. I need the ID for each $car*. So I would like a query that will pull ID's only if $car* exists.
There will always be at least one $car*. But 2, 3, 4 cars etc... are possible. How can check for, and query these possibly existing $cars?
$car1 = 'Honda';
$car2 = 'Mazda';
$car3 = '';
$query="SELECT
id( WHERE car='$car1') as My_car1,
id( WHERE car='$car2') as My_car2,
FROM VEHICLES";
list($My_car1,$My_car2)=pdo_fetch_array(pdo_query($query));
I realize my query is wrong and will not work. But it was the only way I could visually explain what I am trying to do.
Basic SQL:
SELECT id
FROM VEHICLES
WHERE car IN ('Honda', 'Mazda')
And as a general tip, NEVER chain DB calls the way you are. DB operations can/will fail, and you're assuming success. if any of the "inner" calls fail, they'll return a boolean false and break the outer calls. You'll be left in the dark as to why things failed.
NEVER assume success when dealing with external resources. Always assume everything will fail, CHECK for failure at every stage, and treat success as a pleasant surprise.
Directly under this small intro here you'll see the layout of the database tables that I'm working with and then you'll see the details of my question. Please provide as much guidance as possible. I am still learning PHP and SQL and I really do appreciate your help as I get the hang of this.
Table One ('bue') --
chp_cd
rgn_no
bgu_cd
work_state
Table Two ('chapterassociation') --
chp_cd
rgn_no
bgu_cd
work_state
Database Type: PostgreSQL
I'm trying to do the following with these two tables, and I think it's a JOIN that I have to do but I'm not all that familiar with it and I'm trying to learn. I've created a query thus far to select a set of data from these tables so that the query isn't run on the entire database. Now with the data selected, I'm trying to do the following...
First and foremost, 'work_state' of table one ('bue') should be checked against 'work_state' of table two ('chapterassociation'). Once a match is found, 'bgu_cd' of table one ('bue') should be matched against 'bgu_cd' of table two ('chapterassociation'). When both matches are found, it will always point to a unique row within the second table ('chapterassociation'). Using that unique row within the second table ('chapterassociation'), the values of 'rgn_no' and 'chp_cd' should be UPDATED within the first table ('bue') to match the values within the second table ('chapterassociation').
I know this is probably asking a lot, but if someone could help me to construct a query to do this, it'd be wonderful! I really do want to learn, as I don't wish to be ignorant to this forever. Though I'm not sure if I completely understand how the JOIN and comparison here would work.
If I'm correct, I'll have to put this into seperate queries which will then be in PHP. So for example, it'll probably be a few IF ELSE statements that end with the final result of the final query, which updates the values from table two to table one.
A JOIN will do both level of matching for you...
bue
INNER JOIN
chapterassociation
ON bue.work_state = chapterassociation.work_state
AND bue.bgu_cd = chapterassociation.bgu_cd
The actual algorithm is determined by PostreSQL. It could be a merge, use hashes, etc, and depends on indexes and other statistics about the data. But you don't need to worry about that directly, SQL abstracts that away for you.
Then you just need a mechanism to write the data from one table to the other...
UPDATE
bue
SET
rgn_no = chapterassociation.rgn_no,
chp_cd = chapterassociation.chp_cd
FROM
chapterassociation
WHERE bue.work_state = chapterassociation.work_state
AND bue.bgu_cd = chapterassociation.bgu_cd
I've got a problem that I just can't seem to find the answer to. I've developed a very small CRM-like application in PHP that's driven by MySQL. Users of this application can import new data to the database via an uploaded CSV file. One of the issues we're working to solve right now is duplicate, or more importantly, near duplicate records. For example, if I have the following:
Record A: [1, Bob, Jones, Atlanta, GA, 30327, (404) 555-1234]
and
Record B: [2, Bobby, Jones, Atlanta, GA, 30327, Bob's Shoe Store, (404) 555-1234]
I need a way to see that these are both similar, take the record with more information (in this case record B) and remove record A.
But here's where it gets even more complicated. This must be done upon importing new data, and a function I can execute to remove duplicates from the database at any time. I have been able to put something together in PHP that gets all duplicate rows from the MySQL table and matches them up by phone number, or by using implode() on all columns in the row and then using strlen() to decide the longest record.
There has got to be a better way of doing this, and one that is more accurate.
Do any of you have any brilliant suggestions that I may be able to implement or build on? It's obvious that when importing new data I'll need to open their CSV file into an array or temporary MySQL table, do the duplicate/similar search, then recompile the CSV file or add everything from the temporary table to the main table. I think. :)
I'm hoping that some of you can point out something that I may be missing that can scale somewhat decently and that's somewhat accurate. I'd rather present a list of duplicates we're 'unsure' about to a user that's 5 records long, not 5,000.
Thanks in advance!
Alex
If I were you I'd give a UNIQUE key to name, surname and phone number since in theory if all these three are equal then it means that it is a duplicate. I am thinking so because a phone number can have only one owner. Anyways, you should find a combination of 2-3 or maybe 4 columns and assign them a unique key. Once you have such a structure, run something like this:
// assuming that you have defined something like the following in your CREATE TABLE:
UNIQUE(phone, name, surname)
// then you should perform something like:
INSERT INTO your_table (phone, name, surname) VALUES ($val1, $val2, $val3)
ON DUPLICATE KEY UPDATE phone = IFNULL($val1, phone),
name = IFNULL($val2, name),
surname = IFNULL($val3, surname);
So basically, if the inserted value is a duplicate, this code will update the row, rather than inserting a new one. The IFNULL function performs a check to see whether the first expression is null or not. If it is null, then it picks the second expression, which in this case is the column value that already exists in your table. Hence, it will update your row with as much as information possible.
I don't think there're brilliant solutions. You need to determine priority of your data fields you can rely on for detecting similarity, for example phone, some kind of IDs, of some uniform address or official name.
You can save some cleaned up values (reduced to the same format like only digits in phones, concatenated full address) along with row which you would be able to use for similarity search when adding records.
Then you need to decide on data completeness in any case to update existing rows with more complete fields, or delete old and add new row.
Don't know any ready solutions for such a variable task and doubt they exist.
Say if I have an array and I want to check if an element is a part of that array, I can go ahead and use in_array( needle, haystack ) to determine the results. I am trying to see the PHP equivalent of this for my purpose. Now you might have an instant answer for me and you might be tempted to say "Use IN". Yes, I can use IN, but that's not fetching the desired results. Let me explain with an example:
I have a column called "pets" in DB table. For a record, it has a value: Cat, dog, Camel
(Yes, the column data is a comma separated value). Consider that this row has an id of 1.
Now I have a form where I can enter the value in the form input and use that value check against the value in the DB. So say I enter the following comma separated value in the form input: CAT, camel
(yes, CAT is uppercase & intentional as some users tend to enter it that way).
Now when I enter the above info in the form input and submit, I can collect the POST'ed info and use the following query:
$search = $_POST['pets'];
$sql = "SELECT id FROM table WHERE pets IN ('$search') ";
The above query is not fetching me the row that already exists in the DB (remember the record which has Cat, dog, Camel as the value for the pets column?). I am trying to get the records to act as a superset and the values from the form input as subsets. So in this case I am expecting the id value to show up as the values exist in the column, but this is not happending.
Now say if I enter just CAT as the form input and perform the search, it should show me the ID 1 row.
Now say if I enter just camel, cAT as the form input and perform the search, it should show me the ID 1 row.
How can I achieve the above?
Thank you.
The function you're looking for is find_in_set:
select * from ... where find_in_set($word, pets)
for multi-word queries you'll need to test each word and AND (or OR) the tests:
where find_in_set($word1, pets) AND find_in_set($word2, pets) etc
IN() Check whether a value is within a set of values
mysql> SELECT 2 IN (0,3,5,7);
-> 0
mysql> SELECT 'wefwf' IN ('wee','wefwf','weg');
-> 1
SELECT val1 FROM tbl1 WHERE val1 IN (1,2,'a');
View: IN MySql
I've got several things for you in terms of feedback & in direct response to your questions:
First, I suggest you sanitize the input. Everybody is going to tell you that. For that, see What’s the best method for sanitizing user input with PHP?.
Second, normalize the input with UPPER() or LOWER() if you want to use MySQL and need to store user-formatted input, or use strtoupper() and strtolower() if you wanted to process the input before storing it.
You're still left with the order in the user query. E.g. "cat, dog" ought to yield the same result as "dog, cat". If you were to code that with a LIKE statement, performance issues are going to eat you alive. Not only would you have to create the query dynamically, you'd also end up with huge and unnecessarily complex queries. In short, forget it. You have to change the way you store your data.
One way to accomplish this is by creating a relationship table that references a table of unique user input and your record. This table would look similar to
user_id | pet_id
Every user could have more than one pet_id associated with them. I've set up a database a long time ago the same way you did and ran into the same issues. Performance-wise it never paid off and it's anything but good style. I ended up changing my structure because of that to the above-mentioned method.
This mysql function search an INT value into a json array of INT:
-- EXAMPLES :
-- select is_into_json_array(18, '[25, 10, 15]'); -> -100
-- select is_into_json_array(25, '[25, 10, 15]'); -> 0
-- select is_into_json_array(15, '[25, 10, 15]'); -> 2
https://github.com/PietroLaGrotta/Json-in-mysql-text-type/blob/master/is_into_json_in_array.sql
Yes, the column data is a comma
separated value
Here is your fault.
No, it shouldn't be comma separated value
And your database structure should be normalized.