SQL将值插入NORMALIZED DB

时间:2016-04-14 02:47:33

标签: php sql-server database mysqli sql-insert

这是表格:

CREATE TABLE IF NOT EXISTS `dvlaInfoBasic` (
  `id_uDInfoB` int(11) NOT NULL AUTO_INCREMENT,
  `make` varchar(50) NOT NULL,
  `model` varchar(50) NOT NULL,
  `twelveMonthRate` varchar(50) NOT NULL,
  `yearOfManufacture` varchar(50) NOT NULL,
  `cylinderCapacity` varchar(50) NOT NULL,
  `dateofFirstRegistration` varchar(50) NOT NULL,
  `co2Emissions` varchar(50) NOT NULL,
  `fuelType` varchar(50) NOT NULL,
  `colour` varchar(50) NOT NULL,
  `mot` varchar(50) NOT NULL,
  `motDetails` varchar(50) NOT NULL,
  `vin` varchar(50) NOT NULL,
  `transmission` varchar(50) NOT NULL,
  PRIMARY KEY (`id_uDInfoB`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=11 ;

-- --------------------------------------------------------

--
-- Table structure for table `dvlaInfoMore`
--

CREATE TABLE IF NOT EXISTS `dvlaInfoMore` (
  `id_uDInfoM` int(11) NOT NULL AUTO_INCREMENT,
  `sixMonthRate` varchar(50) NOT NULL,
  `wheelPlan` varchar(50) NOT NULL,
  `revenueWeight` varchar(50) NOT NULL,
  `typeApproval` varchar(50) NOT NULL,
  `taxStatus` varchar(50) NOT NULL,
  `taxed` varchar(50) NOT NULL,
  `taxDetails` varchar(50) NOT NULL,
  PRIMARY KEY (`id_uDInfoM`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=11 ;

-- --------------------------------------------------------

--
-- Table structure for table `subscription`
--

CREATE TABLE IF NOT EXISTS `subscription` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `email` varchar(25) NOT NULL,
  `carBSR` varchar(11) NOT NULL,
  `carMake` varchar(25) NOT NULL,
  `carYearFrom` int(4) NOT NULL,
  `carYearTo` int(4) NOT NULL,
  `carTransmissionType` varchar(11) NOT NULL,
  `carFuelType` varchar(11) NOT NULL,
  `carTaxed` varchar(5) NOT NULL,
  `carMot` varchar(5) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=39 ;

-- --------------------------------------------------------

--
-- Table structure for table `userBasic`
--

CREATE TABLE IF NOT EXISTS `userBasic` (
  `id_uB` int(11) NOT NULL AUTO_INCREMENT,
  `userNameG` varchar(50) NOT NULL,
  `userEmailG` varchar(50) NOT NULL,
  PRIMARY KEY (`id_uB`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=11 ;

-- --------------------------------------------------------

--
-- Table structure for table `userChioce`
--

CREATE TABLE IF NOT EXISTS `userChioce` (
  `id_uChoice` int(11) NOT NULL AUTO_INCREMENT,
  `doWithCar` varchar(10) NOT NULL,
  `plateNumber` varchar(20) NOT NULL,
  PRIMARY KEY (`id_uChoice`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=11 ;

-- --------------------------------------------------------

--
-- Table structure for table `userLocation`
--

CREATE TABLE IF NOT EXISTS `userLocation` (
  `id_uLoc` int(11) NOT NULL AUTO_INCREMENT,
  `latitude` varchar(50) NOT NULL,
  `longitude` varchar(50) NOT NULL,
  `postCode` varchar(10) NOT NULL,
  PRIMARY KEY (`id_uLoc`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=11 ;

-- --------------------------------------------------------

--
-- Table structure for table `userMain`
--

CREATE TABLE IF NOT EXISTS `userMain` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `userIdG` int(50) NOT NULL,
  `id_uB` int(11) NOT NULL,
  `id_uM` int(11) NOT NULL,
  `id_uDInfoB` int(11) NOT NULL,
  `id_uDInfoM` int(11) NOT NULL,
  `id_uChoice` int(11) NOT NULL,
  `id_uLoc` int(11) NOT NULL,
  `id_uStat` int(11) NOT NULL,
  `ts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `id_uB` (`id_uB`),
  KEY `id_uB_2` (`id_uB`),
  KEY `id_uM` (`id_uM`),
  KEY `id_uDInfoB` (`id_uDInfoB`),
  KEY `id_uDInfoM` (`id_uDInfoM`),
  KEY `id_uChoice` (`id_uChoice`),
  KEY `id_uLoc` (`id_uLoc`),
  KEY `id_uStat` (`id_uStat`),
  KEY `id_uStat_2` (`id_uStat`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

-- --------------------------------------------------------

--
-- Table structure for table `userMore`
--

CREATE TABLE IF NOT EXISTS `userMore` (
  `id_uM` int(11) NOT NULL AUTO_INCREMENT,
  `userGenderG` tinyint(1) NOT NULL DEFAULT '9',
  `userAboutG` varchar(255) NOT NULL,
  `userBirthdayG` varchar(50) NOT NULL,
  `userLanguageG` varchar(50) NOT NULL,
  PRIMARY KEY (`id_uM`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=11 ;


-- --------------------------------------------------------

--
-- Table structure for table `userStatus`
--

CREATE TABLE IF NOT EXISTS `userStatus` (
  `id_uStat` int(11) NOT NULL AUTO_INCREMENT,
  `termCheck` varchar(3) DEFAULT 'tes',
  PRIMARY KEY (`id_uStat`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=11 ;


--
-- Constraints for dumped tables
--

--
-- Constraints for table `userMain`
--
ALTER TABLE `userMain`
  ADD CONSTRAINT `userMain_ibfk_1` FOREIGN KEY (`id_uB`) REFERENCES `userBasic` (`id_uB`),
  ADD CONSTRAINT `userMain_ibfk_2` FOREIGN KEY (`id_uM`) REFERENCES `userMore` (`id_uM`),
  ADD CONSTRAINT `userMain_ibfk_3` FOREIGN KEY (`id_uDInfoB`) REFERENCES `dvlaInfoBasic` (`id_uDInfoB`),
  ADD CONSTRAINT `userMain_ibfk_4` FOREIGN KEY (`id_uDInfoM`) REFERENCES `dvlaInfoMore` (`id_uDInfoM`),
  ADD CONSTRAINT `userMain_ibfk_5` FOREIGN KEY (`id_uChoice`) REFERENCES `userChioce` (`id_uChoice`),
  ADD CONSTRAINT `userMain_ibfk_6` FOREIGN KEY (`id_uLoc`) REFERENCES `userLocation` (`id_uLoc`),
  ADD CONSTRAINT `userMain_ibfk_7` FOREIGN KEY (`id_uStat`) REFERENCES `userStatus` (`id_uStat`);

这就是我插入数据的方式:

<?php

if($_SERVER["REQUEST_METHOD"]=="POST"){
    require 'connection.php';
    createStudent();
}


function createstudent()
{
    global $connect;

        $userIdG= $_POST["userIdG"];        
        $userNameG= $_POST["userNameG"];
        $userEmailG= $_POST["userEmailG"];  
    $plateNumber = $_POST["plateNumber"];
        $recAccuracy= $_POST["recAccuracy"];
    $latitude = $_POST["latitude"];
    $longitude= $_POST["longitude"];
    $postCode = $_POST["postCode"];
    $doWithCar = $_POST["doWithCar"];
    $make= $_POST["make"];
    $model= $_POST["model"];
    $sixMonthRate= $_POST["sixMonthRate"];
    $twelveMonthRate= $_POST["twelveMonthRate"];
    $dateofFirstRegistration= $_POST["dateofFirstRegistration"];
    $yearOfManufacture= $_POST["yearOfManufacture"];
    $cylinderCapacity= $_POST["cylinderCapacity"];
    $co2Emissions= $_POST["co2Emissions"];
    $fuelType = $_POST["fuelType"];
    $taxStatus = $_POST["taxStatus"];
    $colour= $_POST["colour"];
    $typeApproval= $_POST["typeApproval"];
    $wheelPlan = $_POST["wheelPlan"];
    $revenueWeight= $_POST["revenueWeight"];
    $taxDetails= $_POST["taxDetails"];
    $motDetails= $_POST["motDetails"];
    $taxed= $_POST["taxed"];
    $mot= $_POST["mot"];
    $vin= $_POST["vin"];
    $transmission = $_POST["transmission"];


$query = "Insert into userStatus (termCheck)
values ('$termCheck');";

$query .= "Insert into userLocation (latitude, longitude, postCode)
values ('$latitude', '$longitude', '$postCode');";

$query .= "Insert into userChioce (doWithCar, plateNumber)
values ('$doWithCar', '$plateNumber');";

$query .= "Insert into dvlaInfoMore (sixMonthRate, wheelPlan, revenueWeight, typeApproval, taxStatus, taxed, taxDetails)
values ('$sixMonthRate', '$wheelPlan', '$revenueWeight', '$typeApproval', '$taxStatus', '$taxed', '$taxDetails');";

$query .= "Insert into dvlaInfoBasic (make, model, twelveMonthRate, yearOfManufacture, cylinderCapacity, dateofFirstRegistration, co2Emissions, fuelType, colour, mot, motDetails, vin, transmission)
values ('$make', '$model', '$twelveMonthRate', '$yearOfManufacture', '$cylinderCapacity', '$dateofFirstRegistration', '$co2Emissions', '$fuelType', '$colour', '$mot', '$motDetails', '$vin', '$transmission');";

$query .= "Insert into userMore (userGenderG, userAboutG, userBirthdayG, userLanguageG)
values ('$userGenderG', '$userAboutG', '$userBirthdayG', '$userLanguageG');";

$query .= "Insert into userBasic (userNameG, userEmailG)
values ('$userNameG', '$userEmailG');";

$query .= "Insert into userMain (userIdG)
values ('$userIdG')";


if ($connect->multi_query($query) === TRUE) {
    echo "New records created successfully";
} else {
    echo "Error: " . $query . "<br>" . $connect->error;
}

$connect->close();

}

?>

一切正常......插入到每个表的数据但不插入表 userMain ,或者如果我设法插入,则只发生一次。通过处理此PHP查询,除 userMain

之外,所有表都会不断更新和分配数据

这是我用户失败的另一个版本的userMain:

$query .= "Insert into userMain (userIdG, id_uB, id_uM, id_uDInfoB, id_uDInfoM, id_uChoice, id_uLoc, id_uStat)
values ('$userIdG', userBasic.id_uB, userMore.id_uM, dvlaInfoBasic.id_uDInfoB, dvlaInfoMore.id_uDInfoM, userChioce.id_uChoice, userLocation.id_uLoc, userStatus.id_uStat)";

1 个答案:

答案 0 :(得分:0)

确保在userMain表中填充所有必需的列(非空)。

在获取每个表的最新id存储时,使用max获取最新的id:

SELECT max(id_uDInfoB) from dvlaInfoBasic

对于多笔交易,最好搜索@@ identity或IDENT_CURRENT(&#39; tableName&#39;)主题(Best way to get identity of inserted row?)。

感谢。