Problem when posting into the DB

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • 4

48 Replies - 636 Views - Last Post: 15 July 2013 - 05:40 AM Rate Topic: -----

#16 Yeronox  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 94
  • Joined: 12-March 12

Re: Problem when posting into the DB

Posted 14 July 2013 - 01:28 PM

It doesn't show any error, but it still doesn't post to the DB.
And I'm almost sure I've set up everything correctly.
Attached Image
$query = DB::prepare('INSERT INTO users (email, name, password) VALUES(:email, :name, :password)');

Was This Post Helpful? 0
  • +
  • -

#17 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3391
  • View blog
  • Posts: 9,586
  • Joined: 08-June 10

Re: Problem when posting into the DB

Posted 14 July 2013 - 10:17 PM

View PostYeronox, on 14 July 2013 - 10:28 PM, said:

And I'm almost sure I've set up everything correctly.

then it’s time to set up error handling.
Was This Post Helpful? 0
  • +
  • -

#18 Yeronox  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 94
  • Joined: 12-March 12

Re: Problem when posting into the DB

Posted 15 July 2013 - 12:24 AM

	class DB
	{
	// Made this private again, really shouldn't call this outside the DB wrapper.
	private static function getDB()
	{
	if (self::$connection === null)
	{
		try
		{
			// Correctly placed the settings array in the PDO constructor.
			global $connection;
			self::$connection = new PDO(
				DATABASE_DSN,
				DATABASE_USER,
				DATABASE_PASSWORD,
				array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)
			);
		}
		catch (PDOException $e)
		{
			die($e->getMessage()); // We failed, die with the message.
			// This should be correctly handled by an error handler in production code.
		}
		return self::$connection;
	}
	}

	public static function prepare($query)
	{
		return self::getDB()->prepare($query);
	}

	private static $connection;
	}

Fatal error: Call to a member function prepare() on a non-object

This post has been edited by Yeronox: 15 July 2013 - 12:46 AM

Was This Post Helpful? 0
  • +
  • -

#19 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3391
  • View blog
  • Posts: 9,586
  • Joined: 08-June 10

Re: Problem when posting into the DB

Posted 15 July 2013 - 12:41 AM

for PHP in general–yes, but PDO requires its own setting:
// $pdo being the PDO instance
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// or
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);

Was This Post Helpful? 0
  • +
  • -

#20 Yeronox  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 94
  • Joined: 12-March 12

Re: Problem when posting into the DB

Posted 15 July 2013 - 12:47 AM

I`ve updated the above post again, as it throws that error now :\
Was This Post Helpful? 0
  • +
  • -

#21 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3391
  • View blog
  • Posts: 9,586
  • Joined: 08-June 10

Re: Problem when posting into the DB

Posted 15 July 2013 - 12:49 AM

which line? if it’s line #30, verify that you have a valid return value.

This post has been edited by Dormilich: 15 July 2013 - 12:51 AM

Was This Post Helpful? 0
  • +
  • -

#22 Yeronox  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 94
  • Joined: 12-March 12

Re: Problem when posting into the DB

Posted 15 July 2013 - 12:53 AM

Yep it`s that line;
	function user_register($email, $name, $password)
	{
		$pass_hash = hash("sha512",$password);
		$query = DB::prepare('INSERT INTO `phpupload`.`users` (`user_id`, `email`, `name`, `password`) VALUES (NULL, :email, :name, :password);');
		$query->bindValue(':email',$email);
		$query->bindValue(':name',$name);
		$query->bindValue(':password',$pass_hash);
		$query->execute();
		return PDO::lastInsertId();
	}

I`m using lastInsertId() as an alternative to mysql_insert_id()
Was This Post Helpful? 0
  • +
  • -

#23 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3391
  • View blog
  • Posts: 9,586
  • Joined: 08-June 10

Re: Problem when posting into the DB

Posted 15 July 2013 - 12:56 AM

View PostYeronox, on 15 July 2013 - 09:53 AM, said:

I`m using lastInsertId() as an alternative to mysql_insert_id()

that should get you an E_STRICT warning.

verify, what DB::getDB() really returns (and why).
Was This Post Helpful? 0
  • +
  • -

#24 Yeronox  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 94
  • Joined: 12-March 12

Re: Problem when posting into the DB

Posted 15 July 2013 - 12:59 AM

How can I see what it should return?
Was This Post Helpful? 0
  • +
  • -

#25 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3391
  • View blog
  • Posts: 9,586
  • Joined: 08-June 10

Re: Problem when posting into the DB

Posted 15 July 2013 - 01:00 AM

e.g. by putting var_dump(self::getDB()) in an appropriate place.
Was This Post Helpful? 0
  • +
  • -

#26 Yeronox  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 94
  • Joined: 12-March 12

Re: Problem when posting into the DB

Posted 15 July 2013 - 01:06 AM

Where, for example?
I tried everywhere but I keep on getting errors :(
Was This Post Helpful? 0
  • +
  • -

#27 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3391
  • View blog
  • Posts: 9,586
  • Joined: 08-June 10

Re: Problem when posting into the DB

Posted 15 July 2013 - 01:10 AM

where do you have access to getDB()?
Was This Post Helpful? 0
  • +
  • -

#28 Yeronox  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 94
  • Joined: 12-March 12

Re: Problem when posting into the DB

Posted 15 July 2013 - 01:12 AM

public static function prepare($query)
	{
		return self::getDB()->prepare($query);
	}

Was This Post Helpful? 0
  • +
  • -

#29 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3391
  • View blog
  • Posts: 9,586
  • Joined: 08-June 10

Re: Problem when posting into the DB

Posted 15 July 2013 - 01:13 AM

then put it there (preferably before the return).
Was This Post Helpful? 0
  • +
  • -

#30 Yeronox  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 94
  • Joined: 12-March 12

Re: Problem when posting into the DB

Posted 15 July 2013 - 01:17 AM

object(PDO)[1]
Call to a member function prepare() on a non-object
Was This Post Helpful? 0
  • +
  • -

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • 4