This topic is locked
[SOLVED]

 Insert A Record Into Another Table

6/19/2013 5:29:24 PM
PHPRunner General questions
N
nti author

Insert a record into another table and utilize current record id as new record id.
I understand the table name, field names and field values.
Would like to utilize the current record id FOR the additional record id being added FOR > Insert a record into another table "After record added"
{the sentence above sucks, hard to understand, I know.} but I will hope for the answer. Thank you.



global $conn;

$strSQLInsert = "insert into TableName (Field1, Field2) values (Value1, Value2)";

db_exec($strSQLInsert,$conn);
C
cgphp 6/19/2013
global $conn;

$strSQLInsert = "INSERT INTO TableName (id, Field1, Field2) VALUES (".$keys['id'].", Value1, Value2)";

db_exec($strSQLInsert,$conn);
N
nti author 6/19/2013

Thank you Cristian, works perfect.

A
algreilly 6/20/2013

I have tried this code:
global $conn;

$strSQLInsert = "INSERT INTO TableName (id, Field1, Field2) VALUES (".$keys['id'].", Value1, Value2)";

db_exec($strSQLInsert,$conn);
What I am trying to do is move the ID, MemberLastName, MemberFirstName, MemberStatus to another table for tracking.
Member1 Table has ID, MemberLastName, MemberFirstName, MemberStatus

inputact Table (the table I am trying to insert to) has LastName, FirstName, MemberStatus,ID
Here is the code:
global $conn;

$strSQLInsert = "INSERT INTO inputact (ID, LastName, FirstName, memstatus) VALUES (".$keys['ID'].",MemberLastName, MemberFirstName, MemberStatus)";

db_exec($strSQLInsert,$conn);
I receive the following error:
php error happened

Technical information

Error type 256

Error description Unknown column 'MemberLastName' in 'field list'

URL superaposs.com/nonprod_members1_edit.php?ferror=1&editid1=44037&;

Error file C:\Inetpub\vhosts\superaposs.com\httpdocs\include\dbconnection.my.mysql.php

Error line 36

SQL query INSERT INTO inputact (ID, LastName, FirstName, memstatus) VALUES (44037,MemberLastName, MemberFirstName, MemberStatus)
Anyone have any ideas?
Thanks

Al Reilly

Sergey Kornilov admin 6/20/2013

Assuming that MemberLastName, MemberFirstName and MemberStatus are all text fields you can try the following:

global $conn;

$strSQLInsert = "INSERT INTO inputact (ID, LastName, FirstName, memstatus) VALUES (".$keys['ID'].",'".

$values["MemberLastName"]."','".$values["MemberFirstName"]."','".$values["MemberStatus"]."')";

db_exec($strSQLInsert,$conn);
N
nti author 6/20/2013



Here is the code:



global $conn;

$strSQLInsert = "INSERT INTO inputact (ID, LastName, FirstName, memstatus) VALUES (".$keys['ID'].",MemberLastName, MemberFirstName, MemberStatus)";

db_exec($strSQLInsert,$conn);



I am guessing you are looking for something closer to: (edited next day: I promise to quit guessing.)



global $conn;

$strSQLInsert = "INSERT INTO inputact (ID, LastName, FirstName, memstatus) VALUES (".$keys['ID'].", ".$keys['MemberLastName'].", ".$keys['MemberFirstName'].", ".$keys['MemberStatus'].")";

db_exec($strSQLInsert,$conn);


Also try resetting the form in the phpr Editor.

A
algreilly 6/21/2013

Sergey,
Your code worked great. My next question here would be if I wanted to keep on adding to the same record to keep a running log of changes that were done to that record.
Regards,

Al Reilly



Assuming that MemberLastName, MemberFirstName and MemberStatus are all text fields you can try the following:

global $conn;

$strSQLInsert = "INSERT INTO inputact (ID, LastName, FirstName, memstatus) VALUES (".$keys['ID'].",'".

$values["MemberLastName"]."','".$values["MemberFirstName"]."','".$values["MemberStatus"]."')";

db_exec($strSQLInsert,$conn);