Multiple table insertion

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
batowiise
Forum Commoner
Posts: 28
Joined: Fri Dec 17, 2010 7:11 am

Multiple table insertion

Post by batowiise »

Hi All,

I want to keep audit of transactions. I have two tables, one acting as the main table and the other acting as a copy of the main table (audit) with some of the field copied to the audit table.

The following is my code but data is not been written to the audit table.

Your help is welcome

Code: Select all


$traveller = $_REQUEST['traveller'];
$requestdate = $_REQUEST['requestdate'];

require_once("../admin/db.php");

$sqlinsert="INSERT INTO `internationaltravel` (`traveller`,`requestdate`)
VALUES ('$traveller','$requestdate')";
				
mysql_query($sqlinsert);
				
$last_inserted_id = mysql_insert_id();
				
$sqlinsert_audit="INSERT INTO `audit` (`date`,`initiator`,`transid`)
				VALUES ('$requestdate','$traveller',$last_inserted_id)";
maneetpuri
Forum Commoner
Posts: 60
Joined: Tue Oct 07, 2008 6:32 am

Re: Multiple table insertion

Post by maneetpuri »

Hi,

First try this,

replace this code

$sqlinsert_audit="INSERT INTO `audit` (`date`,`initiator`,`transid`)
VALUES ('$requestdate','$traveller',$last_inserted_id)";

With the below code (single quotes are missing for the IIIrd value)

$sqlinsert_audit="INSERT INTO `audit` (`date`,`initiator`,`transid`)
VALUES ('$requestdate','$traveller','$last_inserted_id')";

if above does not work then try this,-

Go to mysql query browser and execute the insert statement, if there are error in the insert statement it will show you the exact error.


Hope this helps,

Thanks,
batowiise
Forum Commoner
Posts: 28
Joined: Fri Dec 17, 2010 7:11 am

Re: Multiple table insertion

Post by batowiise »

hi and thanks for your reply. the above solutions still did not work. please what next?
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: Multiple table insertion

Post by mikosiko »

2 simple debugging actions:

a) as a temporary error control modify this line as it is shown

Code: Select all

             mysql_query($sqlinsert) or die("Query Error : " . mysql_error() . "<br /> SQL was : " . $sqlinsert);
b) same for the $sqlinsert_audit

and in your code you are not showing where are you executing $sqlinsert_audit... are you?
emelianenko
Forum Commoner
Posts: 35
Joined: Thu Sep 09, 2010 11:49 am

Re: Multiple table insertion

Post by emelianenko »

batowiise wrote:hi and thanks for your reply. the above solutions still did not work. please what next?

can you echo your variables to make sure you have passed them from your form ?

and also, you are not using mysql_query to execute your query, all you do is put it into a variable, but what is happening next ?
Post Reply