Error uploading data to MySQL using PDO

0

Today I am trying to upload data from a registration form to my db using PDO, I get "Error" which is the return that I have established when sending data.

    <?php 

require_once('models/connection.php');

class Data extends Connection {
    public function RegisterUserModel($dataModel, $table) {

        #Preparando conexion a la base de datos.
        $stmt = Connection::connect()->prepare("INSERT INTO $table(user, pass, email) VALUES (:user,:password,:email)");


        #Vinculando parametros a los valores que se almacenarán en la base de datos.
        $stmt->bindParam(":user", $dataModel["user"], PDO::PARAM_STR);
        $stmt->bindParam(":password", $dataModel["pass"], PDO::PARAM_STR);
        $stmt->bindParam(":email", $dataModel["email"], PDO::PARAM_STR);

        #Ejecutando el registro del usuario en la base de datos.
        if($stmt->execute()){
            return "Succes";
        } else {
            return "Error";
        }
    }
}
?>

this is the connection.php file:

    <?php

class Connection {

    public function connect() {

        $link = new PDO("mysql:localhost;dbname=encomienda;","root","");
        return $link;

    }
}

?>

and this is the controller.php file where the form data is sent:

<?php 

class MvcController {

    public function view(){
        include('view/login.view.php');
    }

    public function LinkPagesController() {
        if(isset($_GET['action'])){
            $linkcontroller = $_GET['action'];    
        } else {
            $linkcontroller = "index";
        }

        $request = LinkPageClass::LinkPagesModel($linkcontroller);

        include $request;
    }

    public function RegisterUserController() {

       if(isset($_POST["user"])) {

           $dataController = array("user"=>$_POST["user"], 
                          "pass"=>$_POST["password"], 
                          "email"=>$_POST["email"]);

           $request = Data::RegisterUserModel($dataController, "user");

           echo $request;
       }

    }


}

?>
    
asked by Daniel Parra 14.01.2018 в 00:21
source

1 answer

2

You need to place the host, in connection.php you have

$link = new PDO("mysql:localhost;dbname=encomienda;","root","");

and it's

$link = new PDO("mysql:host=localhost;dbname=encomienda;","root","");

It is also highly recommended that you save the data of the connection in variables, so you would be left:

$user = 'root';
$pass = '';
$host = 'localhost';
$db = 'encomienda';
$link = new PDO("mysql:host=$host;dbname=$db;", $user, $pass);
    
answered by 14.01.2018 / 00:40
source