0

I'm messing with PDO and I'm trying to insert some data into my table; however, I'm getting a DBConnect::prepare() error.

Here's my insertcard.php file:

<?php

  require_once "connection.php";

  $Name = $_POST['cardname'];
  $Colour = $_POST['colour'];
  $Rarity = $_POST['rarity'];

  $CardQuery = "INSERT INTO `cards`(`Name`, `Colour`, `Rarity`) VALUES (`:Name`,`:Colour`,`:Rarity`)";

  $CardResult = $dbconnection->prepare($CardQuery);
  $CardExec = $CardResult->execute(array(":Name"=>$Name,":Colour"=>$Colour,":Rarity"=>$Rarity));

  if ($CardExec) {
    Echo "Data Inserted!";
  }

?>

And my function in connection.php that connects to my database:

private function Connect() {

  try {
    $db = $this->Connection = new PDO('mysql:host=' . $this->hostname . ';dbname=' . $this->dbname . '', $this->username, $this->password);
  }
  catch(PDOException $e)
  {
    echo $e->getMessage();
    echo "<br>Wow, we done goofed.";
  }

  }

And instantiated by:

$dbconnection = new DBConnect('localhost', 'root', '', 'cardtest');

Does anyone know what the issue is? I'll show more of the connection.php file if need be.

Thomas Hutton
  • 793
  • 5
  • 15
  • 34
  • You're blindly going without checking for errors, so you can't see that you have a syntax error. With bound parameters, you don't need to quote your values. – aynber Dec 02 '16 at 15:47
  • (`:Name`,`:Colour`,`:Rarity`) change to (:Name, :Colour, :Rarity) – Fabio Dec 02 '16 at 15:49
  • @aynber I'm not blindly going, I didn't know that bound parameters shouldn't be quoted. That being said, I changed it to: `$CardQuery = "INSERT INTO `cards` (`Name`, `Colour`, `Rarity`) VALUES (:Name, :Colour, :Rarity)";` And it still doesn't work. – Thomas Hutton Dec 02 '16 at 16:01
  • still you need to [check for errors](https://phpdelusions.net/pdo#errors) – Your Common Sense Dec 02 '16 at 16:02

0 Answers0