Select data from mysql and if exist increment by one [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
So what i want is to insert username in table user, So what i want to do is something like this. On my example username will be Jhon. So i want to insert Jhon int table user in column username. And before i insert username i check if Jhon already exist in table. And if Jhon exist (i count rows to see if is already exist) then set John to John2 (because John exist i count 1 row and that count increment by one and add it to John so result is John2). All that works for me but problems start when in my table is John and John2. Then i can count only John and i will inserting John2 all the time. So my problem is how to count all John, John2, John3, John4... from table and get the rows count so when i count 3 Johns so php know there are 3 Johns and it need insert John4?

Write a subquery that counts the number of matching names. Add 1 to that and concatenate it to the prefix.
INSERT INTO user (username, col1, col2, col3)
SELECT CONCAT('Jhon', IF(count = 0, '', count+1)), col1value, col2value, col3value
FROM (SELECT COUNT(*) as count
FROM user
WHERE username REGEXP '^Jhon[0-9]*$') AS x

Related

How to drop a table with register form (SQL Injection) (closed) [duplicate]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am trying to fetch bulk data from a website database but could not succeed. Can somebody suggest if SQL injection is possible and how to do in this case.
There are many ways to do SQL Injection to a website similar to the one you provided.
In the where clause it is expecting ac_no. I assume that this value is being passed from the browser as user input. In that case you can pass ac_no value along with or 1 = 1. e.g where ac_no = 123 or 1 = 1. It returns everything from the table RollPdf1.
For string comparison you can add "" = "" to the where clause.
If you want to perform other select operations ( if you know other table names) then you can append select statements delmited by ;.
UNION operator :
If you know the data types of the columns selected in the query then you can use UNION to get additional data from other tables.
e.g
original query : select name, age, sex from table1 where id = 1
sql injected query : select name, age, sex from table1 where id = 1 AND 1 = 2 UNION select username, id, password from userstable or someother table.

Mysql sort BY number of filled columns [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I'm comparing prices of products from 3 different website. So i have 3 different prices. If website doesn't offer that product, its price is empty.
MySQL looks like this:
**id | name | price_1 | price_2 | price 3**
I would like to sort it from products, which are available at all 3 websites. To products, that are available at just one website.
And I can't figure out how to approach this!
If the empty fields have NULL value, you can use
SELECT * FROM sometable ORDER BY ISNULL(price_1) + ISNULL(price_2) + ISNULL(price_3) DESC;
But a more sensible solution would be:
You have one table, which contains the products
You have another
table, which contains the product's ID, the price and a value which
indicates which website provided that price.
Benefits:
You only have to COUNT(*) the entries of a product in the second table to find out on how many it is listed
You can change the number of websites any time without changing a table
Try this:
SELECT *, (IF(price_1 IS NULL,0,1) + IF(price_2 IS NULL,0,1) + IF(price_3 IS NULL,0,1)) AS odr FROM `test1` ORDER BY odr DESC

update all the selected items [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I want to update all the selected items in sql.
For example, in a table I have a lot of item, all of them with a name. I want to update all this items where the name is 'aaa' for example.
Anybody knows how to do that easy?
Thanks
Best regards
It really depend upon how you want to handle update.
e.g,
1. Update all records where FIELD_NAME equal to aaa
UPDATE TABLE_NAME WHERE FIELD_NAME = 'aaa';
2. Update all records where FIELD_NAME is matched with aaa
UPDATE TABLE_NAME WHERE FIELD_NAME LIKE '%aaa%';
3. Update all records where FIELD_NAME is matched and start with aaa
UPDATE TABLE_NAME WHERE FIELD_NAME LIKE 'aaa%';
4. Update all records where FIELD_NAME is matched and end with aaa
UPDATE TABLE_NAME WHERE FIELD_NAME LIKE '%aaa';
You have to use UPDATE clause to update records with Where clause to update only selected records
https://msdn.microsoft.com/en-us/library/ms177523.aspx
UPDATE tblName
SET columnname = newValue
WHERE name = 'aaa'

How could I filter duplicates? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
INFORMATION:
TABLE = giveaway
FORM 1 = id
FORM 2 = username
FORM 3 = userid
FORM 4 = wish
END OF INFORMATION
I am trying to make it filter through duplicate usernames and userids
NOTE: people enter the userid and the username (it is for a giveaway)
can anyone post me some code to do that?
Try this one:
SELECT `username`,`userid` FROM `giveaway` group by `username` HAVING COUNT(`username`) > 1 UNION SELECT `username`,`userid` FROM `giveaway` group by `userid` HAVING COUNT(`userid`) > 1
drop table test;
/
create table test
(
id number,
username varchar2(20),
userid number,
wish varchar2(40)
);
/
insert into test
values(1,'A',1,'ZXX');
/
insert into test
values(1,'A',1,'YYY');
/
insert into test
values(1,'B',2,'ZXX');
/
SELECT * FROM
(
SELECT ID ,USERNAME,USERID,COUNT(*) OVER (PARTITION BY USERNAME,USERID) AS CNT
FROM TEST
)
WHERE CNT <2
This will give you unique values

Insert multiple records in mysql [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I am getting the list of students from 1 table it has 5 records. Now i added a checkbox at the end of each row.
Now what i want to do is insert all the data in a new table including the checkbox value as 1 if checked and 0 for unchecked.
New table will have a new colomn date_id which will be same for all 5 entries, and other columns will remain same as in table 1.
how to do this please help
Did you mean inserting multiple rows in mysql with a single query then:-
Insert into table table_name Values (date_id,values_of_a),(date_id,values_of_a),(date_id,values_of_a)... n number of records;
The values for a,b,c columns you already have from select query from table 1.
You can try this:
<input type="checkbox" name="check[]" value="<?=(isset($table1Id))?$table1Id:'0'?>" />
<?php
foreach($_POST['check'] as $each){
if($each != "0"){
$table1Id = $each;
### retrive the details from table 1 here based on pk "$table1Id" and insert into table 2
}
}
?>
execute a for/ for each loop for entire list and execute insert query for every record in that list array
You need to do something like:
INSERT INTO new_table (col1, col2, col3) select col1, col2, custom_value from old_table;
Note:
The column count needs to match.
You can specify any custom values in the select query.
If you want using single query then
Insert into table your_table_name Values (date_id,values1),(date_id,values2),(date_id,values3),(date_id,values4),(date_id,values5)
or
store all value in list and execute for loop for every record in that list

Categories