0

Whenever I run this function (see below) I get an error (see further below)

I have looked over the code repeatedly and cannot find the cause of this syntax error.

function create($dog){
    $details = json_decode($dog, true);
    $name = $details['Name'];
    $age = $details['Age'];
    $breed = $details['Breed'];
    $gender = $details['Gender'];
    $chip = $details['Chip'];
    $vaccine = $details['Vaccine'];
    $neutered = $details['Neutered'];
    $flea = $details['Flea'];
    $health = $details['Health'];
    $diet = $details['Diet'];
    $behaviour = $details['Behaviour'];
    $aggression = $details['Aggression'];
    $training = $details['Training'];
    $recall = $details['Recall'];
    $pull = $details['Pull'];
    $offlead = $details['Offlead'];
    $vet = $details['Vet'];
    $conn = self::connect();
    //INSERT Order
    $sql = "INSERT INTO dog (`Name`, `Age`, `Gender`, `Breed`, `Neutered`, `Microchip`, `Vaccine`, `Flea`, `Health`, `Diet`, `Behaviour`, `Aggression`, `Training`, `Recall`, `Pull`, `Offlead`, `vetID`)
    VALUES ('$name', $age, '$gender', '$breed', $neutered, $chip, $vaccine, $flea, '$health', '$diet', '$behaviour', '$aggression', '$training', '$recall', '$pull', '$offlead', $vet)";
    // use exec() because no results are returned
    $conn->exec($sql);

}

the error:

Fatal error: Uncaught PDOException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'n' in 'field list' in C:\xampp\htdocs\classes\DogHandler.php:45 Stack trace: #0 C:\xampp\htdocs\classes\DogHandler.php(45): PDO->exec('INSERT INTO dog...') #1 C:\xampp\htdocs\classes\main.php(29): DogHandler->create('{"Name":"Sparky...') #2 {main} thrown in C:\xampp\htdocs\classes\DogHandler.php on line 45

if it helps, this is the data in the $dog parameter:

{"Name":"Sparky",
"Age":8,
"Breed":"Labradoodle",
"Gender":"male",
"Neutered":true,
"Flea":true,
"Vaccine":true,
"Health":"Sore Hips",
"Diet":"No Treats",
"Behaviour":"N/a",
"Aggression":"N/a",
"Training":"Well trained",
"Recall":"Will come back when name called",
"Pull":"Will pull around cats",
"Offlead":"Only in parks",
"Vet":"vet.ID",
"Chip":"n/a"
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Jamie McAllister
  • 729
  • 2
  • 11
  • 33
  • 1
    Not sure if you know, but you are vulnerable to SQL-injection. On the other hand, try ech-ing the query and run it in phpMyAdmin and also add the full query here.. – Naruto Jun 03 '16 at 13:30
  • the problem is in the wrong way you are writing your query. Rewrite your code as shown in the linked answer, and the error will be gone. – Your Common Sense Jun 03 '16 at 13:32
  • 1
    use `mysql_real_escape_string` you are getting this error because of `N/a` – Niklesh Raut Jun 03 '16 at 13:37
  • 1
    `$chip` is a string, but your'e treating it as a numeric.... please learn to use prepared statements/bind variables – Mark Baker Jun 03 '16 at 13:41

0 Answers0