insert values between rows - php

I don't know if it can be done with just a sql query or it needs a php code
when a cid is missing
There exist many missing values which I can't handle manually
For example, here I don't have cid=1 and cid=6.
I want to insert a row:
cid=1 tcp_sport='undefined' tcp_dport='undefined'
and
cid=6 tcp_sport='undefined' tcp_dport='undefined'
It seems to me I should create a procedure and insert between lines
another solution that I thaught was that I will create a table with cid and undifined values with the respective order and then join this one with that one and this join should have for example ifnull(tcp_sport,'')
would you please help me?

First, use MAX for get the largest ID.
SELECT MAX(cid) as max FROM table
Then, create a for loop for checking if the individual IDs exist:
for ($i = 0; $i < $max; $i++) {
// $query = ... SELECT 1 FROM table WHERE cid = $i ...
// check if the number of rows for $query is greater than 0
// if not, INSERT INTO table VALUES ($i, DEFAULT, DEFAULT)
}

The whole idea of an auto increment ID is to have a value that only refers to one thing ever. By "inserting between the lines" you may be opening yourself up to a lot of unforeseen problems. Image you have another table that has some values that link to the CID of this table. What if that table already has an entry for CID=1, When you insert a new item with CID=1 it will then join to that supporting record. So Data that really belongs to the original item with CID=1 will show for the new item which it probably has nothing to do with.
You aren't going to run out of ID values (if you are approaching the limit of integer, switch it to bigInt), don't re-use IDs if you can avoid it.

You need to use PHP to automate this.
<?php
$link = mysql_connect("localhost", "mysql_user", "mysql_password");
mysql_select_db("database", $link);
while($i < max_value_cid)//replace max_value_cid by the numeric maximum value of cid (SELECT MAX(cid) as max FROM table)
{
$result = mysql_query("SELECT * FROM `table` WHERE cid=".$i, $link);
if(mysql_num_rows($result) == 0)
mysql_query("INSERT INTO `table` VALUES ($i, NULL, NULL);", $link);
$i++;
}
?>
Do test the query on a sample set before execution and remember to backup the entire table, just-in-case.

Related

Prevent Select sql from select the same data from DB PHP

I Have some question about the Seq in PHP.
I create 1 tabel to maintain the sequence table,
ex. table is sequence and the field is seq(start from 1)
The sequence should be unique value.
The problem is.
there is 2 process which is run parallel that will use it.
and I got the error message that say duplicate value.
The question.
How to lock the table from another select query?
this is my code
//select seq
$sqlSeq = "select seq from sequence for update";
$resultSeq = pg_query($sqlSeq);
$rowSeq = pg_fetch_assoc($resultSeq);
$seqCif = $rowSeq['seq'];
//INSERT
$sqlInsert1 ="insert into TEST (customer_id) values( '".$seqCif."')";
//UPDATE
$sqlInsert1 .= "update sequence set seq=seq+1;" ;
Can you help me for this case?
Many Thanks befor..
One simple way to make the operation atomic and to guarantee an incremental id without gaps is to turn the statement to an insert ... select ... query:
insert into test(customer_id)
select coalesce(max(customer_id), 0) + 1 from test

INSERT MULTIPLE ROWS in Gerund Table using Insert Into Select

I used INSERT INTO SELECT to copy values (multiple rows) from one table to another. Now, my problem is how do I insert rows with its corresponding IDs from different tables (since it's normalized) into a gerund table because it only outputs one row in my gerund table. What should I do to insert multiple rows and their corresponding IDs in the gerund table.
My code for the gerund table goes like this.
$insert = "INSERT INTO table1 SELECT * FROM sourcetable"; // where id1 is pk of table1.
$result =mysqli_query($conn,$insert)
$id1=mysqli_insert_id($conn);
Now table 1 has inserted multiple rows same as the other 2 tables.
Assuming id.. are the foreign keys
INSERT INTO gerundtable (pk, id1,id2,id3) VALUES ($id1,$id2,$id3);
My problem is it doesn't yield multiple rows.
According to MySql documentation:
For a multiple-row insert, LAST_INSERT_ID() and mysql_insert_id() actually return the AUTO_INCREMENT key from the first of the inserted rows. This enables multiple-row inserts to be reproduced correctly on other servers in a replication setup.
So, grab the number of records being copied, and the LAST_INSERT_ID() and you should be able to map exact IDs with each copied row.
In the lines of:
$mysqli->query("Insert Into dest_table Select * from source_table");
$n = $mysqli->affected_rows; // number of copied rows
$id1 = $mysqli->insert_id; // new ID of the first copied row
$id2 = $mysqli->insert_id + 1; // new ID of the second copied row
$id3 = $mysqli->insert_id + 2; // new ID of the third copied row
...
$mysqli->query("INSERT INTO gerundtable (pk, id1,id2,id3) VALUES ($id1,$id2,$id3)");
Thank you for trying to understand and also answering my question. I resolved my own code. I used while loop to get the ids of every row and didn't use INSERT INTO SELECT.
Here is the run down. SInce I'm just using my phone bare with my way posting.
$sqlselect = SELECT * FROM table1;
While($row=mysqli_fetch_array(table1){
$insertquery...
$id1=mysqli_insert_id($conn)
$insertgerundtable = INSERT INTO gerundtable VALUES ( $id1, $id2);
}

How to check if ID exists in database, otherwise insert it into database?

I have a code which generates a 6 digit random number with the code mt_rand(100000,999999); and stores it in the variable $student_id. I check if the number exists in the database with the following code.
if($stmt = mysqli_prepare($connect, "SELECT id FROM students WHERE id = ? LIMIT 1")) {
mysqli_stmt_bind_param($stmt, "i", $student_id);
mysqli_stmt_execute($stmt);
mysqli_stmt_store_result($stmt);
mysqli_stmt_bind_result($stmt, $db_id);
mysqli_stmt_fetch($stmt);
}
if(mysqli_stmt_num_rows($stmt) == 1) {
echo "The ID exists.";
} else {
// Insert $student_id into database
}
This code can insert the ID into the database if mysqli_stmt_num_rows($stmt) == 0. But, if the ID already exists, it won't generate a new 6 digit random ID, it won't check if that ID already exists in the database, etc. I can imagine that I would have to use some kind of loop which will keep generating a random ID untill it doesn't exists in the database and then insert it into the database. I've read about a few loops (like while or for), but I don't have any idea how to use them.
Can anyone help me out with this or help me in the right direction?
Cheers
if you don't want to use autoincrement field, you can change your query in this way:
SELECT id FROM students WHERE id = ? limit 1
becames
SELECT count(id) FROM students WHERE id = ?
So you can check if the result is > 0 (exists) or not (not exists)
I created a simple PHP solution for this, while Bohemian answer is valid for a MySQL approach, here is a way to do it using do...while loop as the questionnaire wanted.
<?php
do {
$unique_id = mt_rand(100000, 900000);//generates 6 random numbers
$query = mysqli_query($mysqli, "select * from users where unique_id='$unique_id'");
} while(mysqli_num_rows($query) > 0);// repeats loop till generated id is not found
//php insert query comes here
?>
If you just need 6 digits, use an auto increment column, but have it start at 100000 instead of the usual starting value of 1.
You can do it at the time you create the table:
CREATE TABLE MYTABLE (
ID INT NOT NULL AUTO_INCREMENT,
-- other columns
)
AUTO_INCREMENT = 100000;
See this demonstrated on SQLFiddle.
Or if the table already exists with an auto increment column, you can set it:
ALTER TABLE MYTABLE AUTO_INCREMENT = 100000;

how to get insert_id in a variable before insert query

I like to create a specific filename for a file that will be created by the input of the fields from the insert query. i like to have an unique key for that. this key consists of an user_id, a timestamp and at the end of this, it should be placed the generated insert_id from the insert query fired. it should be placed the auto_increment no. for the end of my generated variable. so the problem is, that i create a variable before the insert query fired so that this variable will be part of the insert query like:
$get_num = $db->query("SELECT COUNT (*) FROM tableA");
$num = $query->fetch_assoc();
$end = $num + 1;
$file_id = $id .".". time() .".". $end;
$insert = $db->query("INSERT INTO tableA ( file_id, a, b, c) VALUES('".$file_id."','".$a."','".$b."','".c."')");
Actually, forget what I wrote previously. You cannot count on COUNT working for you because what happens when a row is deleted? You will have duplicate values. The best bet for you is to first create the row, grab the insert_id, then UPDATE the file_id uing the function you previously described.
$uid = uniqid();
$insert = $db->query("INSERT INTO tableA ( file_id, a, b, c) VALUES('".$uid."','".$a."','".$b."','".c."')");
$file_id = $id .".". time() .".". mysql_insert_id();
$db->query("UPDATE tableA SET file_id='{$file_id}' WHERE file_id='{$uid}' LIMIT 1;");
In the end, you still have to use two queries anyway, so its not like this takes any more resources. Plus you aren't doing a COUNT operation anymore.
In other news, please be sure to read up on SQLi, depending on where your a,b,c variable are coming from.
This is a bad idea. Do your insert and then use LAST_INSERT_ID. Otherwise, as #AuthmanApatira noted, you could have the wrong id. The PHP for this is mysql_insert_id().
Also note that if your index column is auto_increment, you don't even need to worry about the id; the db takes care of it for you. You can just get it after your query runs.

How do I get all the ids of the row created by one multiple row insert statement

I'm new to php. So, please forgive me if this seems like a dumb question.
Say i have a MySQL insert statement insert into table (a,b) values (1,2),(3,4),(5,6). table 'table' has a auto increment field called 'id'.
how can I retrieve all the ids created by the insert statement above?
It will be great if i get an example that uses mysqli.
You can't. I would suggest that you maintain your own ids (using guid or your own auto-increment table) and use it when you insert into the table.
But it's possible to get the auto-increment value for the last inserted using LAST_INSERT_ID():
http://dev.mysql.com/doc/refman/5.0/en/getting-unique-id.html
AngeDeLaMort's answer is almost right. Certainly, the most appropriate way to deal with the problem is to insert one row at a time and poll the insert_id or generate the sequence elsewhere (which has additional benefits in terms of scalability).
I'd advise strongly against trying to determine the last insert_id and comparing this the most recent insert_id after the insert - there's just too may ways this will fail.
But...an alternative approach would be:
....
"INSERT INTO destn (id, data, other, trans_ref)
SELECT id, data, other, connection_id() FROM source";
....
"SELECT id FROM destn WHERE trans_ref=connection_id()";
....
"UPDATE destn SET trans_ref=NULL where trans_ref=connection_id()";
The second query will return the ids generated (note that this assumes that you use the same connection for all 3 queries). The third query is necessary because connection ids to go back into the pool when you disconnect (i.e. are reused).
C.
In some cases, if you have another identifier of sort such as a UserID, you could filter your query by UniqueID's greater than or equal to mysql_insert_id(), limit by the number of affected rows and only display those by the user. This would really only work inside of a transaction.
$SQL = "INSERT INTO Table
(UserID, Data)
VALUES
(1,'Foo'),
(1,'Bar'),
(1,'FooBar')";
$Result = mysql_query($SQL);
$LastID = mysql_insert_id();
$RowsAffected = mysql_affected_rows();
$IDSQL = "SELECT RecordID
FROM Table
WHERE UserID = 1
AND RecordID >= '$LastID'
LIMIT '$RowsAffected'";
$IDResult = mysql_query($IDSQL);
as a follow up to AngeDeLaMort:
You could seperate your inserts and do it something like this:
$data = array (
array(1,2),
array(3,4),
array(5,6)
);
$ids = array();
foreach ($data as $item) {
$sql = 'insert into table (a,b) values ('.$item[0].','.$item[1].')';
mysql_query ($sql);
$id[] = mysql_insert_id();
}
Now all your new id's are in the $id array.
Maybe I can do this
$insert = "insert into table (a,b) values (1,2),(3,4),(5,6)";
$mysqli->query($insert);
$rows_to_be_inserted=3;
$inserted_id = $mysqli->insert_id // gives me the id of the first row in my list
$last_row_id = ($inserted_id+$rows_to_be_inserted)-1;
$mysql->query("select * from table where id between $inserted_id and $last_row_id");
what to you guys say?

Categories