-5

Parse error: syntax error, unexpected ';' in C:\wamp\www\12.01.2015 Class 01\Coffee Website\Model\CoffeeModel.php on line 106

<?php

require ("Entities/CoffeeEntity.php");

// contains database related code for the coffee type

class CoffeeModel

{

//  Get all coffee types from the database and return them in an array

  function GetCoffeeTypes()

{

 require 'Credentials.php';


 //Open connection and Select database

mysql_connect($host, $user, $password) or die (mysql_error());

mysql_select_db($database);

$result = mysql_query("SELECT DISTINCT type FROM coffee") or 

die(mysql_error());

$types = array();


// Get data from databse

while($row = mysql_fetch_array($result))

{
        array_push($types, $row[0]);

     }

/ Close connection and return

 mysql_close();

 return $types;  

}

// GET coffeeEntity objects from the database and return them in an array.

function GetCoffeeByType($type)

{
    require 'Credentials.php';


// Open connection and select database

mysql_connect($host, $user, $password) or die (mysql_error());

 mysql_select_db($database);


$query = "SELECT * FROM coffee WHERE type LIKE '$type'";

 $result = mysql_query($query) or die (mysql_error());

$coffeeArray = array();


//GET Data from Database

while ($row = mysql_fetch_array($result))
     {

 $name = $row[1];

 $type = $row[2];

 $price = $row[3];

 $roast = $row[4];

 $country = $row[5];

 $image = $row[6];

 $review = $row[7];


// Create Coffee objects and store them in an array

$coffee = new CoffeeEntity (-1, $name, $type, $price, $roast, $country, $image, $review);

array_push($coffeeArray, $coffee);

}

// CLose connection and return result

mysql_close();

return $coffeeArray;

     }


function GetCoffeeById($id)
{
    require 'Credentials.php';


// Open connection and select database

mysql_connect($host, $user, $password) or die (mysql_error());

 mysql_select_db($database);


$query = "SELECT * FROM coffee WHERE id = $id";

 $result = mysql_query($query) or die (mysql_error());



//GET Data from Database

 while ($row = mysql_fetch_array($result))

{

   $name = $row[1];

 $type = $row[2];

 $price = $row[3];

 $roast = $row[4];

 $country = $row[5];

  $image = $row[6];

 $review = $row[7];


        // Create Coffee 


 $coffee = new CoffeeEntity ($id, $name, $type, $price, $roast, $country, 

$image, $review);

 }

 // CLose connection and return result

  mysql_close();

return $coffee;

 }


 function InsertCoffee (CoffeeEntity $coffee)

 {

   $query = sprintf ("INSERT INTO coffee

   (name, type, price, roast, country, image, review)

 VALUES 

 ('%s','%s','%s','%s','%s','%s','%s')",

 mysql_real_escape_string($coffee->name),

  mysql_real_escape_string($coffee->type),

 mysql_real_escape_string($coffee->price),

 mysql_real_escape_string($coffee->roast),

 mysql_real_escape_string($coffee->country),

 mysql_real_escape_string("Images/Coffee/". $coffee->image),

 $this->PerformQuery($query);

 }



 function UpdateCoffee($id, CoffeeEntity $coffee)

 {

  $query =("UPDATE coffee

       SET name = '%s', type = '%s', price = '%s', roast = '%s',


       country = '%s', image = '%s', review = '%s'

       WHERE id = $id"


  mysql_real_escape_string($coffee->name),

  mysql_real_escape_string($coffee->type),

 mysql_real_escape_string($coffee->price),

 mysql_real_escape_string($coffee->roast),

 mysql_real_escape_string($coffee->country),

 mysql_real_escape_string("Images/Coffee/". $coffee->image),

 $this->PerformQuery($query);


                        );

 $this-> PerformQuery($query);

 }

        function DeleteCoffee($id)

 {

   $query = "DELETE FROM coffee WHERE id = $id";

 $this->PerformQuery($query);

 }


function PerformQuery ($query)

{

require ('Credentials.php');

 mysql_connect($host, $user, $password) or die(mysql_error());

 mysql_select_db($database);


//Execute query and close connection

 mysql_query($query) or die(mysql_error());

 mysql_close();

}

}

?>
  • We need to see the file, I am not sure what to make of all that up there...EDIT: saw update, checking now – mcdonaldjosh Jan 16 '15 at 21:16
  • And what is line 106? – Josh Jan 16 '15 at 21:17
  • 1
    $this->PerformQuery($query); – Sheikh Noman Mehmood Jan 16 '15 at 21:17
  • this is the line o6 file complete code posted in the question – Sheikh Noman Mehmood Jan 16 '15 at 21:18
  • Looks like your code is all messed up in `function UpdateCoffee()`. Clean that up & try again. – mopo922 Jan 16 '15 at 21:18
  • 1
    **Please, [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php).** They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). **Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement)** instead, and **use [PDO](http://us1.php.net/pdo).** – Jay Blanchard Jan 16 '15 at 21:22
  • You might find the best help you can get is to switch to an IDE. That will show you your syntax errors without even running any code. – halfer Jan 16 '15 at 22:05

3 Answers3

1

It's right here in the function InsertCoffee:

$query = sprintf ("INSERT INTO coffee etc etc" 
// Snip    
mysql_real_escape_string("Images/Coffee/". $coffee->image),

// You never had the closing paren ) to sprintf()

$this->PerformQuery($query);

You need to get rid of the trailing comma after the final mysql_real_escape_string(), and then close the call to sprintf() with );.

nickb
  • 59,313
  • 13
  • 108
  • 143
1

First error is in line 42 (second slash is missing to let it be a comment):

/ Close connection and return

Change it to:

// Close connection and return

Next error is in line 188 (value for review is missing), to avoid the syntax error and insert an empty string for review change it from:

 mysql_real_escape_string("Images/Coffee/". $coffee->image),

to:

 mysql_real_escape_string("Images/Coffee/". $coffee->image),  "");

Next error is in line 210 (sprintf is missing), so change

  $query =("UPDATE coffee

to:

  $query = sprintf("UPDATE coffee

Then line 207 change

   WHERE id = $id"

to

   WHERE id = $id",

Then line 222 change

 $this->PerformQuery($query);

to:

""
hellcode
  • 2,678
  • 1
  • 17
  • 21
  • @Sheikh Noman Mehmood: because you did several syntax errors, see edit. – hellcode Jan 16 '15 at 21:55
  • @Sheikh: when responding to help, please avoid simply adding "it's not solved" or "it doesn't work". You are not providing any extra information for the poster to help you with. Try to do some debugging, or explain exactly _how_ it doesn't work. More detail = better answers! – halfer Jan 16 '15 at 22:02
0
 $this->PerformQuery($query);

Is throwing the error. Not sure what you're trying to do but it is improper syntax.

mcdonaldjosh
  • 119
  • 2
  • 8
  • Sorry, try simple debugging next time before wasting everyones time on here. We're not here to debug your syntax errors. – mcdonaldjosh Jan 16 '15 at 22:26