Insert multiple records in mysql [closed] - php

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

Related

Select data from mysql and if exist increment by one [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 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

Special keyword in mysql select statement [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
Consider the following script:
SELECT * FROM TABLE1 WHERE COLUMN1='$exb';
$exb is a PHP variable which is fed from an HTML select list with 3 values:0,1,2. Values 1 and 2 correspond to column1 values. In case of selecting value 0, I want to include all the values of COLUMN1. Is there a way to implement the above without changing the script? In other words is there any keyword to assign to $exb which will oblige the script to select all the rows of table TABLE1 in case the user selects 0 from HTML select list?
Thank you
It's a little unclear, but I think you are asking is there a special clause you can add to a where clause to return every single row from the database rather than specific criteria matched in a where clause.
You can put in a pretend where clause by saying col1=col1 which is effectively a bogus (though valid) syntax like this:
SELECT * FROM TABLE1 WHERE COLUMN1=column1;
Though it would have to be without the quotes to select every single row of the table.
Putting the quotes around the variable would be very simple in your php however.
Having said that, wouldn't it be much easier to simply omit the where clause entirely if the value selected is 0?
For this you require to build a dynamic query.
$query = "SELECT * FROM TABLE1";
$exb = isset($_GET['exb']) ? $_GET['exb'] : 0;
$goodParam = array(0,1,2);//array of allowed values
if($exb>0){
if (in_array($exb, $goodParam)) {
$query .= " WHERE COLUMN1 = '$exb'";
}
}
echo $query;
I don't think you can do that with MySql query only. You need to use php and ifelse statement where you can check for $exb value before executing query. For example:
if($exb == "0")
$query = "SELECT * FROM TABLE1";
else
$query = "SELECT * FROM TABLE1 WHERE COLUMN1='$exb'";

Multiple ID SQL query [closed]

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);
}

Select entire table contents and insert into another table with the same column names [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 8 years ago.
Improve this question
I have two tables that have the same column names. Is there a way of getting the contents from one table and inserting it into another. I could export it into a sreadsheet then import it into the other table but would prefer to quickly use mysql. My experience with php and mysql is mainly selecting and inserting from one table.
Use INSERT ... SELECT:
INSERT INTO table1 SELECT * FROM table2
Note that this will only work if the tables are defined with columns in the same order; otherwise one will have to explicitly name the columns in one (or both) of the INSERT and SELECT parts of the command:
INSERT INTO table1 (colA, colB, colC) SELECT colA, colB, colC FROM table2

MySQL - Adding Columns to table? [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
I'm working on the first giant update to my website. It has an existing table for posts. If I add on two columns to the table, will I be able to import the old data to the updated table?
EDIT: I meant columns. Whoops.
You mean two columns? If they are nullable then you should be able to import your old data which doesn't contain these columns. You'll just need to write your insert statement carefully by making sure to specify the exact columns you are inserting into. This is a good practice and should be done anyway (thanks Bruno for the tip).
Example:
INSERT INTO table1 (Col1, Col2, Col3)
SELECT Col1, Col2, Col3
FROM sourceTable
Instead of
INSERT INTO table1
SELECT *
FROM sourceTable
If the two new columns are not nullable then you can you try appending some dummy data using literals just to get the insert to work.
Example:
INSERT INTO table1 (Col1, Col2, Col3, NewCol1, NewCol2)
SELECT Col1, Col2, Col3, '', ''
FROM oldDataTable
Or you could create a default constraint which will work just as well.
First Import the data and then add the new columns.
Or you can insert the two columns taking care that they cann't be null, and then import the old data
I assume you mean if you add two columns to the table?
Yes you should be able to insert the existing data using INSERT statements as along as the new columns are nullable or you set a default value for those columns.

Categories