Jump to content

PHP PDO MySQL Insert into two tables


spertuit
Go to solution Solved by mac_gyver,

Recommended Posts

I am attempting to insert data into two tables with a single function. Currently I can enter into one table, but not the other. Anyone have advice or see anything wrong?

public function createlog($logDay,$logMonth,$logYear,$logCaptain,$logEmployer,$logLocation,$logWeather,$logSeaCondition,$logVisibility,$logWind,$logRunningTime,$logStandByTime,$logDownTime,$logMate,$logEngineer,$logDeckhand,$logDeckhand2,$logCook,$logAccidents,$logCaptainRemarks,$logFuelOnBoardPrevious,$logFuelReceived,$logFuelTotal,$logFuelUsed,$logFuelTransferred,$logMeterClosing,$logMeterOpening,$logMeterPumped,$logFuelOnBoardPrevious,$logLubeReceived,$logLubeTotal,$logLubeUsed,$logLubeOnBoardEnd,$logHoursOnLubePort,$logHoursOnLubeStbd,$totalHoursOnEnginePort,$totalHoursOnEngineStbd,$vesselName,$vesselNumber,$vesselType,$vesselOwner,$table,$table2){
		$sql = "INSERT INTO $table2 SET vesselName=:vesselName,vesselNumber=:vesselNumber,vesselType=:vesselType,vesselOwner=:vesselOwner";
		$q = $this->conn->prepare($sql);
		$q->execute(array(':vesselName'=>$vesselName,':vesselNumber'=>$vesselNumber,':vesselType'=>$vesselType,':vesselOwner'=>$vesselOwner));
		$sql = "INSERT INTO $table SET logDay=:logDay,logMonth=:logMonth,logYear=:logYear,logCaptain=:logCaptain,logEmployer=:logEmployer,logLocation=:logLocation,logWeather=:logWeather,logSeaCondition=:logSeaCondition,logVisibility=:logVisibility,logWind=:logWind,logRunningTme=:logRunningTime,logStandByTime=:logStandByTime,logDownTime=:logDownTime,logMate=:logMate,logEngineer=:logEngineer,logDeckhand=:logDeckhand,logDeckhand2=:logDeckhand2,logCook=:logCook,logAccidents=:logAccidents,logCaptainRemarks=:logCaptainRemarks,logFuelOnBoardPrevious=:logFuelOnBoardPrevious,logFuelReceived=:logFuelReceived,logFuelTotal=:logFuelTotal,logFuelUsed=:logFuelUsed,logFuelTransferred=:logFuelTransferred,logMeterClosing=:logMeterClosing,logMeterOpening=:logMeterOpening,logMeterPumped=:logMeterPumped,logFuelOnBoardPrevious=:logFuelOnBoardPrevious,logLubeReceived=:logLubeReceived,logLubeTotal=:logLubeTotal,logLubeUsed=:logLubeUsed,logLubeOnBoardEnd=:logLubeOnBoardEnd,logHoursOnLubePort=:logHoursOnLubePort,logHoursOnLubeStbd=:logHoursOnLubeStbd,totalHoursOnEnginePort=:totalHoursOnEnginePort,totalHoursOnEngineStbd=:totalHoursOnEngineStbd";
		$q = $this->conn->prepare($sql);
		$q->execute (array(':logDay'=>$logDay,':logMonth'=>$logMonth,':logYear'=>$logYear,':logCaptain'=>$logCaptain,':logEmployer'=>$logEmployer,':logLocation'=>$logLocation,':logWeather'=>$logWeather,':logSeaCondition'=>$logSeaCondition,':logVisibility'=>$logVisibility,':logWind'=>$logWind,':logRunningTime'=>$logRunningTime,':logStandByTime'=>$logStandByTime,':logDownTime'=>$logDownTime,':logMate'=>$logMate,':logEngineer'=>$logEngineer,':logDeckhand'=>$logDeckhand,':logDeckhand2'=>$logDeckhand2,':logCook'=>$logCook,':logAccidents'=>$logAccidents,':logCaptainRemarks'=>$logCaptainRemarks,':logFuelOnBoardPrevious'=>$logFuelOnBoardPrevious,':logFuelReceived'=>$logFuelReceived,':logFuelTotal'=>$logFuelTotal,':logFuelUsed'=>$logFuelUsed,':logFuelTransferred'=>$logFuelTransferred,':logMeterClosing'=>$logMeterClosing,':logMeterOpening'=>$logMeterOpening,':logMeterPumped'=>$logMeterPumped,':logFuelOnBoardPrevious'=>$logFuelOnBoardPrevious,':logLubeReceived'=>$logLubeReceived,':logLubeTotal'=>$logLubeTotal,':logLubeUsed'=>$logLubeUsed,':logLubeOnBoardEnd'=>$logLubeOnBoardEnd,':logHoursOnLubePort'=>$logHoursOnLubePort,':logHoursOnLubeStbd'=>$logHoursOnLubeStbd,':totalHoursOnEnginePort'=>$totalHoursOnEnginePort,':totalHoursOnEngineStbd'=>$totalHoursOnEngineStbd));
			return true;	
	}	
Link to comment
Share on other sites

Thanks a lot! My code is messy, but I am working on it right now. Everything is working at the moment.

	public function createlog($logDay,$logMonth,$logYear,$logCaptain,$logEmployer,$logLocation,$logWeather,$logSeaCondition,$logVisibility,$logWind,$logRunningTime,$logStandByTime,$logDownTime,$logMate,$logEngineer,$logDeckhand,$logDeckhand2,$logCook,$logAccidents,$logCaptainRemarks,$logFuelOnBoardPrevious,$logFuelReceived,$logFuelTotal,$logFuelUsed,$logFuelTransferred,$logMeterClosing,$logMeterOpening,$logMeterPumped,$logFuelOnBoardEnd,$logLubeOnBoardPrevious,$logLubeReceived,$logLubeTotal,$logLubeUsed,$logLubeOnBoardEnd,$logHoursOnLubePort,$logHoursOnLubeStbd,$totalHoursOnEnginePort,$totalHoursOnEngineStbd,$vesselName,$vesselNumber,$vesselType,$vesselOwner,$table,$table2){
		$sql = "INSERT INTO $table2 SET vesselName=:vesselName,vesselNumber=:vesselNumber,vesselType=:vesselType,vesselOwner=:vesselOwner";
		if(!$stmt = $this->conn->prepare($sql)){
    		// prepare failed
   			 echo "<pre>Prepare failed:\n";
    		print_r($pdo->errorInfo());
    		echo "</pre>";
		} else {
   		 	if(!$stmt->execute(array(':vesselName'=>$vesselName,':vesselNumber'=>$vesselNumber,':vesselType'=>$vesselType,':vesselOwner'=>$vesselOwner))){
        		// execute failed
        		echo "<pre>Execute failed:\n";
        		print_r($stmt->errorInfo());
        		echo "</pre>";
    		} else {
        	// query ran without any errors, you can test if it returned any rows and use them here
					$sql = "INSERT INTO $table SET logDay=:logDay,logMonth=:logMonth,logYear=:logYear,logCaptain=:logCaptain,logEmployer=:logEmployer,logLocation=:logLocation,logWeather=:logWeather,logSeaCondition=:logSeaCondition,logVisibility=:logVisibility,logWind=:logWind,logRunningTime=:logRunningTime,logStandByTime=:logStandByTime,logDownTime=:logDownTime,logMate=:logMate,logEngineer=:logEngineer,logDeckhand=:logDeckhand,logDeckhand2=:logDeckhand2,logCook=:logCook,logAccidents=:logAccidents,logCaptainRemarks=:logCaptainRemarks,logFuelOnBoardPrevious=:logFuelOnBoardPrevious,logLubeOnBoardPrevious=:logLubeOnBoardPrevious,logFuelReceived=:logFuelReceived,logFuelTotal=:logFuelTotal,logFuelUsed=:logFuelUsed,logFuelTransferred=:logFuelTransferred,logMeterClosing=:logMeterClosing,logMeterOpening=:logMeterOpening,logMeterPumped=:logMeterPumped,logFuelOnBoardEnd=:logFuelOnBoardEnd,logLubeReceived=:logLubeReceived,logLubeTotal=:logLubeTotal,logLubeUsed=:logLubeUsed,logLubeOnBoardEnd=:logLubeOnBoardEnd,logHoursOnLubePort=:logHoursOnLubePort,logHoursOnLubeStbd=:logHoursOnLubeStbd,totalHoursOnEnginePort=:totalHoursOnEnginePort,totalHoursOnEngineStbd=:totalHoursOnEngineStbd";
					if(!$stmt = $this->conn->prepare($sql)){
    				// prepare failed
   			 		echo "<pre>Prepare failed:\n";
    				print_r($pdo->errorInfo());
    				echo "</pre>";
					} else {
   		 				if(!$stmt->execute (array(':logDay'=>$logDay,':logMonth'=>$logMonth,':logYear'=>$logYear,':logCaptain'=>$logCaptain,':logEmployer'=>$logEmployer,':logLocation'=>$logLocation,':logWeather'=>$logWeather,':logSeaCondition'=>$logSeaCondition,':logVisibility'=>$logVisibility,':logWind'=>$logWind,':logRunningTime'=>$logRunningTime,':logStandByTime'=>$logStandByTime,':logDownTime'=>$logDownTime,':logMate'=>$logMate,':logEngineer'=>$logEngineer,':logDeckhand'=>$logDeckhand,':logDeckhand2'=>$logDeckhand2,':logCook'=>$logCook,':logAccidents'=>$logAccidents,':logCaptainRemarks'=>$logCaptainRemarks,':logFuelOnBoardEnd'=>$logFuelOnBoardEnd,':logFuelReceived'=>$logFuelReceived,':logFuelTotal'=>$logFuelTotal,':logFuelUsed'=>$logFuelUsed,':logFuelTransferred'=>$logFuelTransferred,':logMeterClosing'=>$logMeterClosing,':logMeterOpening'=>$logMeterOpening,':logMeterPumped'=>$logMeterPumped,':logFuelOnBoardPrevious'=>$logFuelOnBoardPrevious,':logLubeOnBoardPrevious'=>$logLubeOnBoardPrevious,':logLubeReceived'=>$logLubeReceived,':logLubeTotal'=>$logLubeTotal,':logLubeUsed'=>$logLubeUsed,':logLubeOnBoardEnd'=>$logLubeOnBoardEnd,':logHoursOnLubePort'=>$logHoursOnLubePort,':logHoursOnLubeStbd'=>$logHoursOnLubeStbd,':totalHoursOnEnginePort'=>$totalHoursOnEnginePort,':totalHoursOnEngineStbd'=>$totalHoursOnEngineStbd))){
        				// execute failed
        				echo "<pre>Execute failed:\n";
        				print_r($stmt->errorInfo());
        				echo "</pre>";
    				} else {
        			// query ran without any errors, you can test if it returned any rows and use them here
					return true;
    				}
				}
    		}
		}		
	}	
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.