Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Let's just say I have two tables.
The first one :
id thing1 thing2
The second :
id_fo other_thing
where id_fo depend from the first one table.
I have to insert thingS in both table, but in php mysql request (pdo for example), how can I get all last insert id ?
I mean, if I had 30 row in one query, I will have 30 new id.
How can I do my second SQL query ?
In Psuedo code:
Start transaction
insert into table1 (thing1, thing2) values (thingS,thingS2)
lastidtable1 = lastinsertedid();
insert into table2 (id_fo, other_thing) values (lastidtable1,thingS);
lastidtable2 = lastinsertedId();
commit;
Every of the above lines should php code which calls a query.
Are you looking for something like this ?
$main_data = array('dino', 'babu', 'john');
foreach($main_data as $main) {
// Insert main to 1st table
mysql_query("MY INSERT QUERY TO TABLE 1");
// Get the last insert id
$new_id = mysql_insert_id();
// Insert the sub data to 2nd table using the insert id
mysql_query("MY INSERT QUERY TO TABLE 2 USING $new_id ");
}
INSERT and UPDATE do not return any datasets,you can add an INSERTED column with timestamp type,set DEFAULT to CURRENT_TIMESTAMP and select all rows with the greatest timestamp value.
if you can guarantee that there is only one insert process going on at a time you could do something like this psuedo code:
$max1=select max(id) as max1 from table;
insert into table ....
$num_inserted_rows=number of inserted rows.
$max2=select max(id) as max2 from table;
if(($max2-$max1) == $num_inserted_rows){
$lastid=select last_insert_id();
$inserted_ids=range($lastid-$num_inserted_rows,$lastid);
}
Related
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.
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 9 years ago.
Improve this question
I currently have a table that looks like this.
http://i.stack.imgur.com/KFP6Q.png
This is a comment system. the column id gives the comment an ID. the server_id is the ID for the section the comment was posted on. The user_id is the ID for the person who posted it. And lastly, the comment is the comment itself. Here is how I created the comment:
http://pastebin.com/VHUDW6Dm
What I want to do is create a variable, $commentcount, that will count how many comments there are for a server and be able to display them on a page. If someone could direct me to a function that can help me with this or actually create the code here, it would be greatly appreciated.
Since you want the number of comments per server, you can use the SQL GROUP BY clause to aggregate the resulting rows by the unique server_id.
SELECT server_id, COUNT(id) FROM comments GROUP BY server_id;
This will return the count for each server_id group. If you are only displaying this for a single server_id at a time, you can simply use
SELECT COUNT(id) FROM comments WHERE server_id = <your server id>;
http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html
A very rough draft for you would be to iterate a count.
$q = mysql_query("MYSQL QUERY TO GET ALL THE COMMENTS FOR THE USER");
$count = 1; // Start the count, preferrably at 1
while ($comment = mysql_fetch_assoc($q)) {
$count++; //iterate the count
}
echo $count; // Echo's the count;
use following mysql query
select count(*) as count_comment from comments;
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
I'm trying make something unique in my database.
First I check all required rows SELECT * FROM table WHERE x='y'
this gives me 10 rows (as an example). Now what I'm trying to do is UPDATE a field of just one of the 10 rows and disallow any further updates once the update has been executed.
For example, imagine the 10 rows are photos and the WHERE clause is a date. So on a certain date only 10 photos were taken. I have created a field named fave and I want to select just ONE of these images. Basically this is the best photo select on that date.
How do I disable all future updates from this? To prevent me from selecting more than one favourite for any given date?
An unique index can help.
Look at a simple example.
Say we have a table like this:
CREATE TABLE photos(
photo_id int primary key ,
dat date not null,
fave enum ('y')
);
where fave enum ('y') declares, that the column fave can have either 'y' value or NULL.
Then define an unique index on dat+fave columns:
CREATE UNIQUE INDEX only_one_fav ON photos( dat, fave );
The index does allow only one row with given combination of dat+fav, two identic rows are prohibited - however this does not apply to null.
Take a look at this demo: http://www.sqlfiddle.com/#!2/2f28a8/3
CREATE TABLE photos(
photo_id int primary key ,
dat date not null,
fave enum ('y')
);
insert into photos( photo_id, dat ) values
(1,'2013-11-02'),(2,'2013-11-02'),(3,'2013-11-02'),(4,'2013-11-02'),(5,'2013-11-02'),
(11,'2013-11-05'),(12,'2013-11-05'),(13,'2013-11-05'),(14,'2013-11-05'),(15,'2013-11-05');
CREATE UNIQUE INDEX only_one_fav ON photos( dat, fave );
UPDATE photos SET fave = 'y' WHERE photo_id = 2;
UPDATE photos SET fave = 'y' WHERE photo_id =14;
-- UPDATE photos SET fave = 'y' WHERE photo_id = 4;
There are two dates, and 5 photos are taken on each date.
Try to uncommend the last UPDATE command in this demo and hit a Build schema button - you will get an error message - MySql wont allow for two favorite photos id: 2 + 4 on the same date: '2013-11-02'.
Every item in table should have something to differentiate it from the others.... Like an ID.
When you find the one you want to update... Just update on one unique column, or a series of columns that make it unique.
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
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Well so I got a tiny problem, I got a db and 2 tables like this
Table User
user_id
Table UserrToData
user_id int pk
data_id int
Table UserData
data_id
name
nick
I got the user_id and i need to get records from the userdata , is it possible using 1 query?
Of course, if anyone know how I would really appreciate if he would help me : )
For the selecting I got only the relationship user_id > data_id
update
Guys I got huge db I just simplified the problem to the minimum ^_-
Hope this helps:
SELECT name, nick
FROM UserToData utd INNER JOIN UserData ud ON utd.data_id = ud.data_id
WHERE utd.user_id = [supplied userid]
With such little data, however, there is no need for separate tables, just store name and nick in the User table:
Table User
user_id pk
name
nick
You should overthink your database design. The highest aim is always to prevent redundancies.
If you want the user to have more than one userdata entry you should define your database like this:
Table User
user_id pk
Table UserData
data_id pk
user_id fk
name
nick
So the query would be SELECT * FROM UserData WHERE user_id = ?.
If you only want the user to have one set of userdata you should integrate it into the user table:
Table User
user_id pk
name
nick
So the query would be SELECT * FROM User WHERE user_id = ?.
Why have you put the name and nick in a separate table? You can just put the data in one user table. Might be wise to learn more about how to formulate entities and relations in your database.
Either way, if you ever need to do something like this in Zend at a later time, you can use Zend_Db_Select and do a join. Lookie lookie: http://framework.zend.com/manual/1.12/en/zend.db.select.html#zend.db.select.building.join