This topic is locked

Help on Inserting Data

7/23/2009 4:33:31 AM
PHPRunner General questions
S
swanside author

Hello.
I have two tables.

job with a layout like this.

--

-- Table structure for table `job`

--
CREATE TABLE IF NOT EXISTS `job` (

`Order_Date` datetime DEFAULT NULL,

`File_No` varchar(50) DEFAULT NULL,

`Job_No` int(50) NOT NULL AUTO_INCREMENT,

`CustomerId` smallint(10) DEFAULT NULL,

`Order_Time` varchar(50) DEFAULT NULL,

`Contract` longtext,

`Order_Site_Address` varchar(200) DEFAULT NULL,

`Job_Description` longtext,

`VAT` double(7,2) DEFAULT NULL,

`Invoice_Printed` tinyint(1) DEFAULT NULL,

`Invoice_Printing_Date` datetime DEFAULT NULL,

`Invoice_Tax_Date` datetime DEFAULT NULL,

`Payment_Received` tinyint(1) DEFAULT NULL,

`Job_Finished` tinyint(1) DEFAULT NULL,

`CustomerRef` varchar(50) DEFAULT NULL,

`Customer_Name` varchar(50) DEFAULT NULL,

`Payment_Due_date` datetime DEFAULT NULL,

`Payment_Date` datetime DEFAULT NULL,

`Locked` tinyint(1) DEFAULT NULL,

`Note` varchar(255) DEFAULT NULL,

`Date_Added` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,

`1st_Payment` double(7,2) DEFAULT NULL,

`2nd_Payment` double(7,2) DEFAULT NULL,

`3rd_Payment` double(7,2) DEFAULT NULL,

`1st_Payment_Date` date DEFAULT NULL,

`2nd_Payment_Date` date DEFAULT NULL,

`3rd_Payment_Date` date DEFAULT NULL,

`EngineerId` varchar(50) NOT NULL,

`PDAAllocation` varchar(100) DEFAULT NULL,

`LocationId` varchar(50) DEFAULT NULL,

`JobTypeId` varchar(50) NOT NULL,

`Job_Sheet` varchar(50) NOT NULL,

PRIMARY KEY (`Job_No`),

KEY `Customer_Name` (`Customer_Name`),

KEY `File_No` (`File_No`)

) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1021651;


And labour with a layout like this.
--

-- Table structure for table `labour`

--

CREATE TABLE IF NOT EXISTS `labour` (

`Job_No` varchar(50) NOT NULL,

`Engineer_Name` varchar(50) NOT NULL DEFAULT '',

`Paying_Rate` double(7,2) DEFAULT NULL,

`EngineerId` int(10) DEFAULT NULL,

`Working_Hrs` double(7,2) DEFAULT NULL,

`Sheet_Returned` tinyint(1) DEFAULT NULL,

`Invoice` double(7,2) DEFAULT NULL,

`Send_To_Invoice` tinyint(2) DEFAULT NULL,

`PDAAllocation` varchar(100) DEFAULT NULL,

PRIMARY KEY (`Job_No`,`Engineer_Name`)

) ENGINE=InnoDB DEFAULT CHARSET=latin1;


I have put this code in the job>Before Record Added

global $conn;

$strSQLInsert = "insert into labour (Job_No, Engineer_Name) values ('".$values["Job_No"]."','".$values["Engineer_Name"]."')";

db_exec($strSQLInsert,$conn);


labour is a child table to job. This holds information about labour to a specific job and is linked by the Job_No
We usually add the information to job first, then, we click on the labour link and Add new to enter the information into the labour table aswell.

I am trying to cut down the information inputting times, as in job, there is a PDAAllocation field, this is the name of the engineer from another table. In the labour we use the same information from the same table, so we might aswell enter the PDAAllocation and have that inser the Engineer name in labour at the same time.
When I go to job>Add new enter my information and save, I get this error
PHP error happened
Technical information

Error type 256

Error description Duplicate entry '' for key 'PRIMARY'

URL localhost/EXTRA/job_add.php?

Error file D:\EXTRA\include\dbconnection.php

Error line 34

SQL query insert into labour (Job_No, Engineer_Name) values ('','')

Solution It seems that you add duplicate key fields.
Please check data you enter on the ADD or REGISTER page.
I try again using a different Engineer Name and get the same error.
Any ideas please anybody?
Thanks

Paul

J
Jane 7/23/2009

Hi,
auto-increment field is empty in the Before record added event. You should use After record addedevent for this purpose.

Also please make sure you use fields from job table, not from labour table in your code:

$jobno = mysql_insert_id();

$strSQLInsert = "insert into labour (Job_No, Engineer_Name) values ('".$jobno."','".$values["EngineerId"]."')";