only want to insert one row into other table - php

How do I get this to only insert one row of data, with selected fields inserted into another table by using the reference ?
so far I can only get all of the fields transferred when using the statement in phpmyadmin
$Reference=$_GET['Reference'];
$sql="INSERT INTO Triage (Reference, Forename)
SELECT Reference, Forename from `Instruction` WHERE Reference='$Reference'"

INSERT INTO Table2(LongIntColumn2, CurrencyColumn2)
SELECT LongIntColumn1, Avg(CurrencyColumn) as CurrencyColumn1 FROM Table1 GROUP BY LongIntColumn1;
INSERT INTO prod_detail(prod_name,prod_code) select Name,Zip as data from info_tb where Name="mittal";

Try something like this:
$Reference=mysql_real_escape_string($_GET['Reference']);
$sql="INSERT INTO Triage (Reference, Forename)
SELECT Reference, Forename FROM `Instruction`
WHERE Reference='$Reference' LIMIT 1";

Related

insert records from one table to another

Hi I am trying to add records from one table to another, once i have added a 'user' record, the table that is being selected contains rows of available security options, and the table that is being inserted to is the child table for the user, detailing security options.
I cam across this code in an earlier post, which i am sure works nicely, however i am trying to modify it so that the values from statement, includes two parts, one from the select query and one which is the key from the master record.#
This is the original code I found from this site:
INSERT INTO def (catid, title, page, publish)
SELECT catid, title, 'page','yes' from `abc`
And this is what I am trying to do with it:
$sql = "INSERT INTO Link_UserSecurity (UserFk, ModuleFk) values ('".$keys["UserPk"]."', SELECT ModulePk from Global_Modules)";
CustomQuery($sql);
And this is the error I am getting:
INSERT INTO Link_UserSecurity (UserFk, ModuleFk) values ('4', SELECT
ModulePk from Global_Modules)
See screenshot for further detail
Obviously I am not concating the from statement properly, but would appreciate any help?
You can insert the $keys["UserPk"] variable as if it were a constant in the SQL:
$sql = "INSERT INTO Link_UserSecurity (UserFk, ModuleFk) SELECT '{$keys["UserPk"]}', ModulePk from Global_Modules";
Do note that $keys["UserPk"] must be escaped before adding it into the query. In PDO, it would look like this:
$keys["UserPk"] = $pdo->quote($keys["UserPk"]);
$sql = "INSERT INTO Link_UserSecurity (UserFk, ModuleFk) SELECT '{$keys["UserPk"]}', ModulePk from Global_Modules";
Could be a problem related to the double quotes sequence
"INSERT INTO Link_UserSecurity (UserFk, ModuleFk)
values ('". $keys['UserPk']. "', SELECT ModulePk from Global_Modules)";
but you could use also a select insert
"INSERT INTO Link_UserSecurity (UserFk, ModuleFk)
SELECT '" . $keys['UserPk']. "' , ModulePk from Global_Modules)";
Adding only new and unique records from one table to another. Limiting is a good idea to prevent it from timeout. It can be run several times until all the records copied.
First, select the latest record ID from the table to be copied:
SET #lastcopied =
(SELECT
IF(MAX(a.exp_inotech_id)>0, MAX(a.exp_inotech_id), 0) AS lastcopied
FROM
kll_export_to a
WHERE exp_tezgah = 'A2015-0056');
Then, select and add the records to the destination table:
INSERT INTO kll_export_to
(SELECT * FROM
kll_export_from f
GROUP BY f.exp_inotech_id
HAVING COUNT(f.exp_inotech_id) = 1 AND exp_tezgah = 'A2015-0056' AND f.exp_inotech_id > #lastcopied
ORDER BY exp_inotech_id
LIMIT 1000);

Get sql Id and insert it into another query instantly

I have two tables first called messages and the other called messages_reply.
I used this code to insert into messages table:
$query = "INSERT INTO `messages` VALUES('', '$id', '$otherId', '')";
$query_run = mysqli_query($connect, $query);
I have the first column auto_increment thats why I left it empty by writing ''
Now i want this auto_increment value that i have inserted to be inserted in the other table called messages_reply
Do I have to create another query to return it or there is an instant way to insert it here and there?
you have to select the last id on table messages first, then you can insert that last id + 1 into messages reply
$query_sel_last_id = "SELECT id FROM messages ORDER BY id desc LIMIT 1"; // select the last id
after that, you only need to insert to messages_reply, remember to plus the value
$query_sel_last_id + 1
EDIT: gordon's solution is better and simpler, LAST_INSERT_ID()

Sql select max id from table

I want to get the Max userID from tblLogin and add 1
This value i want to put into a string to insert into the table
$sql2="INSERT INTO tblLogin (Gebruikersnaam, Wachtwoord, UserID)
VALUES ('$_POST[gebruikersnaam]','$_POST[wachtwoord]', 'SELECT MAX(tblLogin.UserID) FROM tblLogin')";
I want to insert another row with the UserID then being the max one (5) + 1 so 6
However if i try this with the code i have now
it just adds 1 instead of 6
It looks like it takes the value as 0 and i don't know why.
The 'Gebruikersnaam' (it's dutch :p) and 'Wachtwoord' are being inserted fine.
Only the UserID gives an issue.
Help.
You can set UserID field to AUTO_INCREMENT.
Thereafter you don't need to pass USerID value in your insert query.
if you want to to increase it everytime then you should set it to auto increment.
otherwise you can try like this:
$sql2="INSERT INTO tblLogin
(Gebruikersnaam, Wachtwoord, UserID)
VALUES ('$_POST[gebruikersnaam]','$_POST[wachtwoord]',
(SELECT 1+MAX(tblLogin.UserID) FROM tblLogin)";
$sql2="INSERT INTO tblLogin (Gebruikersnaam, Wachtwoord, UserID)
SELECT '$_POST[gebruikersnaam]','$_POST[wachtwoord]', MAX(tblLogin.UserID)
FROM tblLogin";

MySQL insert combo from php var and other table

i am trying to insert into my sql table a combination of php variables and 1 row data from another table. ive found examples that are similar but none that have this combination.
OTHERTABLE.liveDate would be where i need the single record from, but i would need to add a condition such as WHERE id='1' from OTHERTABLE
'$navid', '$loginid', '$total', '$where' are not from OTHERTABLE
INSERT INTO myTable (`navid`,`loginid`, `pageDisplayNum`, `whereFrom`, `liveDate`)
VALUES ('$navid', '$loginid', '$total', '$where', 'OTHERTABLE.liveDate')
INSERT INTO myTable (`navid`,`loginid`, `pageDisplayNum`, `whereFrom`, `liveDate`)
select '$navid', '$loginid', '$total', '$where', OTHERTABLE.liveDate
from OTHERTABLE
where OTHERTABLE.id='1'

Access last row added to table - PHP

I have a simple sql query adding a new row to a database and need it to return the a field back to Javascript. The field does Auto_increment but stupildy I called it 'itemId' so mysql_insert_id doesnt work and I don't think I have time to go and amend all the php files that use 'itemId'
Here's my code if it helps:
$addMainItem = "INSERT INTO newsItems (itemId, title, date, tags, location, latitude, longitude, visibleFrontpage, introText, fullDome, liveEvent, customServing, visitorAttraction, retail, digitalCinema, visiblePublic, thumbPath, links, smallDesc) VALUES ('','$title','$date','$tags','$loco','$lat','$long','$visiFront','$intro','$dome ','$live','$custom','$attrac','$retail','$cinema','$public','$thumbPath','$links','$smallDesc')";
$result = mysql_query($addMainItem) or die('error '.mysql_error());
if($result) echo (mysql_insert_id());
I've never heard that naming a column itemId breaking mysql_isert_id().
But you can just select the last inserted record if auto_increment is working.
SELECT * FROM newsItems ORDER BY itemId DESC LIMIT 1
You can put the select statement into a transaction with the insert statement if you're using innoDB and you're worried about a race condition.
mysql_query("SELECT LAST_INSERT_ID()");
Isn't it what are you looking for?

Categories