PHP PDO query does not work edit

1

Hello I have a detail is that I have a page that shows me the data of a bd table then when I'm going to modify it opens a modal and loads me the data, until there all the problem comes when I update it I get the error:

  

Fatal error: Uncaught Error: Call to undefined method   Database :: update () in Path: edita.php: 24

Here I leave my conector.php

<?php
    class Database 
    {
        // private static $dbName = 'naw' ; 
        private static $dbName = 'naw'; 
        // private static $dbHost = 'localhost' ;
        private static $dbHost = 'localhost' ;
        // private static $dbUsername = 'root';
        private static $dbUsername = 'root';
        // private static $dbUserPassword = '';
        private static $dbUserPassword = '';

        private static $cont  = null;


        public static function connect()
        {
            // One connection through whole application
                if ( null == self::$cont )
                {
                    try
                    {
                        self::$cont =  new PDO( "mysql:host=".self::$dbHost.";"."dbname=".self::$dbName, self::$dbUsername, self::$dbUserPassword); 
                    }
                    catch(PDOException $e)
                    {
                        die($e->getMessage());
                    }
                }
                return self::$cont;
        }

        public static function query($query) {
            try {

                $q = self::$cont->prepare($query);
                $q->execute();

                $data = $q->fetchAll();

            } catch (PDOException $e) {
                echo $e;
            }
            return $data;
        }



        public static function disconnect()
        {
            self::$cont = null;
        }
    }
?>

And here my edita.php :

<?php
    require_once '../php/conector.php';

    $id                = $_POST['id'];
    $nombre      = $_POST['nombre'];
    $telefono      = $_POST['telefono'];
    $correo        = $_POST['correo'];
    $comuna          = $_POST['comuna'];
    $actualizado = $_POST['actualizado'];


    // function update_modal() {
        try {
            $pdo = new Database;
            $pdo->connect();
            $sql = "UPDATE 'menmbresia' 
                                                        SET 'nombre_apellido' =             $nombre         ,
                                                                'telefono' =                            $telefono       ,
                                                                'correo' =                              $correo         ,
                                                                'comuna' =                              $comuna         ,
                                                                'actualizado' =                     $actualizado,
                                                        WHERE id ='{$id}'";
            try {
                $w = $pdo->update($sql);
            } catch (PDOExecption $e) {
                $pdo->rollback();
            }
        } catch ( PDOExecption $e) {
        }
        // database::disconnect();
        $pdo->disconnect();
        return;
    // }

            ?>

I thought about adding this to miconector but I do not know if it's okay

    public static function update($sql) {
        try {

            $q = self::$cont->prepare($sql);
            $q->execute();

        } catch (PDOException $e) {
            echo $e;
        }
    }

But with that, I do not get an error, but I still do not edit anything, who knows what I could do? with some example or something that I am new in php and less with ajax and php pdo .-.

    
asked by Naoto 20.09.2017 в 16:11
source

1 answer

0

If you add that method you would not have the error. Although that method can execute other types of queries, such as DELETE. To limit this you can do a simple validation of the SQL query, for example:

public static function update( $sql ) {

    if ( self::isUpdate( $sql )) {
        $q = self::$cont->prepare( $sql );

        return $q->execute();
    } 

    return FALSE;
}

private static function isUpdate( $sql ) {

    return ( strpos( $sql, "UPDATE " ) == 0 );
}

'isUpdate ()' will only validate that "UPDATE" appears in the first position of the string with your SQL statement, from there you can add the improvements that you think or create similar methods to validate other statements, in a class independent for this purpose.

Greetings.

    
answered by 20.09.2017 в 16:29