Im relatively new to PHP and MySQL and would like some help.
Having an issue with some PHP code, particularly inserting form data into a mysql database. The code sample below works, just not the intended way.
As it stands it can insert records, though only once with a number. I believe this is because of the relationship between the fleet to FleetID?
$query1 = "INSERT INTO `fleet` (`FleetID`, `frgn_branchid`, `Fleet`, `Driver`) VALUES ({$_POST['fleet']}, '{$_POST['BranchID']}', '{$_POST['fleet']}', '{$_POST['driver']}')";
$query2 = "INSERT INTO `washes` (`WashID`, `frgn_fleetid`, `frgn_branchid`, `Washer`, `Type`, `CabState`, `TrailerState`, `Description`) VALUES (NULL,{$_POST['fleet']},'{$_POST['BranchID']}', '{$_POST['washer']}', '{$_POST['type']}', '{$_POST['cabState']}','{$_POST['trailerState']}','{$_POST['description']}')";
$query3 = "INSERT INTO `branches` (`BranchID`, `branches`) VALUES ('{$_POST['BranchID']}', ";
However I would like to add that more than once. For example: 01: Testdata, 2012 / 02: Moredata, 2013 / 01: Extradata, 2014
Question: Could I have the fleetID just increment on its own, and not store the `fleet` alongside it? And if so, how?
Thanks
You need to set up the FleetID, WashID and BranchID's as auto incrementing ID's and drop them from the insert, this will let the database set them with a unique value.
For example a valid table definition with this approach would be
create table fleet (
`FleetID` bigint not null auto_increment,
`frgn_branchid` bigint,
`Fleet` varchar(50) not null,
`Driver` varchar(50) not null,
primary key(FleetID)
);
Then when you insert
$query1 = "INSERT INTO `fleet` (`frgn_branchid`, `Fleet`, `Driver`) ".
"VALUES ('".mysql_real_escape_string({$_POST['BranchID']})."',".
"'".mysql_real_escape_string({$_POST['fleet']})."',".
"'".mysql_real_escape_string({$_POST['driver']})."')";
The first problem you are going to hit with this approach is you aren't going to know what the id is after you insert. for this you need to use mysql_insert_id (or it's equivalent for mysqli or pdo).