2 Replies - 369 Views - Last Post: 25 September 2017 - 11:41 AM Rate Topic: -----

#1 dak45309  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 155
  • Joined: 22-April 13

PHP PDO questions needing answered

Posted 22 September 2017 - 07:44 PM

So I have a form that submits user data via POST. This form then sends the information to the processing page in which I end up doing all that I need to do with it. I do 2 inserts on a registration page (one to an address table, one to a user table so it keeps the user table more clean). I first submit the address into the address table.

First question I have:

I have tried using

$PDO -> lastInsertId()


to get the id of the last inserted record. This means that I would call this after I have executed my insert query (which I have done via a stored procedure). It is supposed to return the last id and it doesn't. It only returns 0 (and I mean...it has every single time I've called it). So I decided to go for the next best thing which was to do it via the database call itself with

SELECT LAST_INSERT_ID() as ID


and this returns the correct number. Is there any reason why the SQL ran version works compared to the PDO version in this example?

Next thing..........

After I insert the address of the user (and currently call the SQL version to get last id inserted), I assign this id of the last inserted row to a variable to be used in inserting to the user table. Only problem is that when I try to run this insert query, it doesn't even insert the user. It's like there was nothing that happened. I had tried putting each insert query in it's own try/catch, and under a single try/catch block but still (again), the user information for the page won't insert. I don't know if it has anything to do with password_hash and using the PASSWORD_DEFAULT option in which is uses $2y as the beginning of the password in the hash. Can anyone figure out what is making the user insert statement not properly inserting the user information?

PHP CODE:

if(is_null($_SESSION["Registrationerrors"]))
	{
		$addressID = null;
		$userID = null;
		
		try
		{
			$PDO = con();
			$insertAddr = $PDO -> prepare("CALL insert_address(:addr, :apt, :city, :stateID, :zip)");
			$insertAddr -> bindValue(":addr", $_POST["address1"], PDO::PARAM_STR);
			$insertAddr -> bindValue(":apt", $_POST["address2"], PDO::PARAM_STR);
			$insertAddr -> bindValue(":city", $_POST["city"], PDO::PARAM_STR);
			$insertAddr -> bindValue(":stateID", $_POST["state"], PDO::PARAM_INT);
			$insertAddr -> bindValue(":zip", $_POST["zip"], PDO::PARAM_INT);
			$insertAddr -> execute();
			foreach ($PDO -> query("CALL get_last_insert_id()") as $row) { $addressID = $row["ID"]; }
			$insertAddr = null;
		}
		catch(PDOException $ex)
		{
			header("Location: register.php");
			exit();
		}
		
		try
		{
			$pass = password_hash($_POST["pass"], PASSWORD_DEFAULT);
			
			$insertUser = $PDO -> prepare("CALL insert_user(:username, :fName, :lName, :email, :addrID, :pass");
			$insertUser -> bindParam(":username", $_POST["user"]);
			$insertUser -> bindParam(":fName", $_POST["fName"]);
			$insertUser -> bindParam(":lName", $_POST["lName"]);
			$insertUser -> bindParam(":email", $_POST["email"]);
			$insertUser -> bindParam(":addrID", $addressID);
			$insertUser -> bindParam(":pass", $pass);
			$insertUser -> execute();
			foreach ($PDO -> query("CALL get_last_insert_id()") as $row) { $userID = $row["ID"]; }
			echo $userID;
			
			if (!is_null($userID) || $user <= 0)
			{
				#header("Location: login.php");
				exit();
			}
			else
			{
				header("Location: register.php");
				exit();
			}
		}
		catch(PDOException $ex)
		{
			echo $ex -> getMessage();
		}
		
	}
	else
	{
		header("Location: register.php");
		exit();
	}


STORED PROCEDURE CODE:

INSERT INTO `users`(`Username`, `fName`, `lName`, `Email`, `AddressID`, `SubscriptionendDT`, `BasicPremium`, `ExtraPremium`, `Hold`, `Admin`, `Password`) VALUES(Users, Firsts, Lasts, Email, AddrID, NULL, 0, 0, 0, 0, Pass)


Is This A Good Question/Topic? 0
  • +

Replies To: PHP PDO questions needing answered

#2 dak45309  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 155
  • Joined: 22-April 13

Re: PHP PDO questions needing answered

Posted 22 September 2017 - 10:16 PM

Found out I was missing a ) in my insert_user prepared statement :) Spent all day trying to figure out what was wrong with now. Finally figured it out :)
Was This Post Helpful? 0
  • +
  • -

#3 ArtificialSoldier  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1828
  • View blog
  • Posts: 5,755
  • Joined: 15-January 14

Re: PHP PDO questions needing answered

Posted 25 September 2017 - 11:41 AM

Quote

Is there any reason why the SQL ran version works compared to the PDO version in this example?

I bet it has something to do with the fact that you're using a stored procedure.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1