0

It works on my localhost but not on the server. Does this mean the database is not connecting at all or do I have something wrong in the statements?

$submission = $_POST['submit'];
$decode = json_decode($submission, true);

try {
    $pdo = new PDO('mysql:localhost;dbname=database', 'username', 'password');

} catch(PDOException $e){
    echo 'PDO Exception: '.$e->getMessage();
    die();
}

class QueryBuilder {
    protected $pdo;
    public function __construct($pdo){
        $this->pdo = $pdo;
    }

    public function insert($parameters){
        $sql = "insert into tableName ('entry', 'author', 'email', 'sign', 'month') values (:entry, :author, :email, :sign, :month)";
        $statement = $this->pdo->prepare($sql);
        try {
            $statement->execute($parameters);
        } catch (Exception $e) {
            die($e->getMessage());
        }
    }
}

$query = new QueryBuilder($pdo);
$query->insert($decode);

I even tried a table clearing statement and it also does nothing...

public function clear(){
    try {
        $statement = $this->pdo->prepare("truncate table submissions");
        $statement->execute();
    } catch (Exception $e) {
        die($e->getMessage());
    }
}

$query->clear();
Magdi Gamal
  • 681
  • 1
  • 9
  • 25
  • Are you sure "$decode" holds all the parameters you need? – in need of help Jun 27 '18 at 07:27
  • 2
    You turned error reporting on? `error_reporting(E_ALL); ini_set('display_errors', 1); ` – Ende Jun 27 '18 at 07:28
  • try localhost name to server name – Prasanna Mane Jun 27 '18 at 07:28
  • look for errors inside the php.log – M0ns1f Jun 27 '18 at 07:29
  • 2
    When referencing column names `('entry',` use backticks `(\`entry\`,` – Nigel Ren Jun 27 '18 at 07:31
  • Hm tried all that, but it's still not working :/ Switched localhost to server name, used backticks, and checked $decode again and it holds all the parameters, but still nothing changes. I also turned error reporting on but no errors show or an error.log file on the server. I'm so confused – Magdi Gamal Jun 27 '18 at 07:37
  • What is the return value of `$statement->execute($parameters);` ? Should be true if everything went okay, false otherwise. – RyanNerd Jun 27 '18 at 07:51
  • how do I check the value for it? echo $statement->execute($parameters) ? – Magdi Gamal Jun 27 '18 at 07:55
  • ok I checked it with an if else statement and looks $statement->execute($parameters); is false. Does this mean the database is connecting fine, but there is something wrong with the prepare command? – Magdi Gamal Jun 27 '18 at 07:59
  • See also http://php.net/manual/en/pdo.setattribute.php -- You should set your PDO attributes for error handing: `PDO::ATTR_ERRMODE` See the comments section on how to set this: `$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);` – RyanNerd Jun 27 '18 at 08:00
  • 1
    [`$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);`](http://php.net/manual/en/pdo.construct.php) – RiggsFolly Jun 27 '18 at 08:01
  • Thanks guys. I set up pdo error handling and now it says SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected So I guess it's not accessing the database at all. It's so weird though. I checked again. The database is created with all privileges granted and all the credentials I entered look correct – Magdi Gamal Jun 27 '18 at 08:07

0 Answers0