0

This should be a very basic error, but based on the error-description I can't seem to figure it out. Either I misunderstood some part of the concept or it's just some sign missing.

The problem arises when I try to execute a query.

This is some of the code (I think it should be enough):

//Create database connection to my server
$pdo = new PDO($dsn, $user, $password);


$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

//All single variables
$lan = $_POST["lan"];
$botyp = $_POST["botyp"];

//All variables with min and max value
$pris = $_POST["pris"];
$prisArray = explode(",", $pris); //Splits string "minvalue, maxvalue" by delimiter "," to become array with [minvalue, maxvalue]
$prisMin = $prisArray[0];
$prisMax = $prisArray[1];

$storlek = $_POST["storlek"];
$storlekArray = explode(",", $storlek);
$storlekMin = $storlekArray[0];
$storlekMax = $storlekArray[1];

$rum = $_POST["rum"];
$rumArray = explode(",", $rum);
$rumMin = $rumArray[0];
$rumMax = $rumArray[1];

$avgift = $_POST["avgift"];
$avgiftArray = explode(",", $avgift);
$avgiftMin = $avgiftArray[0];
$avgiftMax = $avgiftArray[1];

$query = "SELECT * FROM bostader 
WHERE lan = ? AND
objekttyp = ? AND
(pris >= ? AND pris <= ?) AND
(area >= ? AND area <= ?) AND
(rum >= ? AND rum <= ?) AND
(avgift >= ? AND avgift <= ?)";

$stmt = $pdo->prepare($query);
$stmt->execute([$lan, $botyp, $prisMin, $prisMax, $storlekMin, $storlekMax, $rumMin, $rumMax, $avgiftMin, $avgiftMax]); //Execute query using relevant variables

When I run this I get an error saying:

Parse error: parse error, expecting `']'' in /Library/WebServer/Documents/resultat.php on line 58

Which points to this line:

$stmt->execute([$lan, $botyp, $prisMin, $prisMax, $storlekMin, $storlekMax, $rumMin, $rumMax, $avgiftMin, $avgiftMax]);

Thank you in advance for your help.

u_mulder
  • 54,101
  • 5
  • 48
  • 64
user3058751
  • 123
  • 1
  • 6

1 Answers1

1

Instead of this code

$stmt->execute([$lan, $botyp, $prisMin, $prisMax, $storlekMin, $storlekMax, $rumMin, $rumMax, $avgiftMin, $avgiftMax]); 

you shuld try this one

$stmt->execute(array(
    $lan, 
    $botyp, 
    $prisMin, 
    $prisMax, 
    $storlekMin, 
    $storlekMax, 
    $rumMin, 
    $rumMax, 
    $avgiftMin, 
    $avgiftMax
)); 
Neo Morina
  • 391
  • 2
  • 13