0

I'm setting to do a simple query using PDO. However, when I run it, it does not insert. The database is called "famous" and the table is called "pessoas" containing only two columns called (codigo and nome).The connection works, but when I execute it return "Error to save".

<?php

function getConnection(){
    $dsn = 'mysql:host=localhost;bdname=pessoas';
    $user = 'root';
    $password = 'init4289';

    try{
        $pdo = new PDO($dsn, $user, $password);
        echo 'SUCESSO AO CONECTAR!';
        return $pdo;
    }catch(PDOExeption $ex){
        echo 'erro: '. $ex->getMessage();
    }
}
?>
#end page "conexao_pdo.php"


<?php
include 'conexao_pdo.php';

$conn = getConnection();

$sql = "INSERT INTO famosos (codigo, nome) VALUES (?, ?)";

$stmt = $conn->prepare($sql);
$stmt->bindValue(1, 6);
$stmt->bindValue(2, 'Antonio');


if($stmt->execute()){
echo 'Success to save';
}else{
    echo '<p>'.'Error to save';
}
?>
messier
  • 65
  • 1
  • 7
  • your one `bindValue` contains integer while other contains string – Zain Farooq Apr 22 '19 at 04:25
  • And `bind_param` is also missing – Zain Farooq Apr 22 '19 at 04:26
  • You seem to be trying to insert into a table called 'famosos' where you say the table is called 'pessoas'. If this is a typo please correct it (and other typos) so that we can see what you're actually working with. –  Apr 22 '19 at 04:28
  • Also, what does $stmt->errorInfo() return when you encounter an error? –  Apr 22 '19 at 04:31

1 Answers1

1

You may consider the following:

  • probably it's just a typing error - bdname should be dbname in 'mysql:host=localhost;bdname=pessoas'
  • database and table names - 'famous' and 'pessoas' in the question, 'pessoas' and 'famous' in the code
  • include exception handling with PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
  • your function getConnection() should return false on failure

Code, based on your question:

<?php
function getConnection(){
    $dsn = 'mysql:host=localhost;dbname=pessoas';
    $user = 'root';
    $password = 'init4289';

    try {
        $pdo = new PDO(
            $dsn, 
            $user, 
            $password,
            array(
                PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
            )           
        );
        echo 'SUCESSO AO CONECTAR!';
    } catch (PDOExeption $ex){
        echo 'Error: '. $ex->getMessage();
        return false;
    }

    return $pdo;
}
?>

<?php
include 'conexao_pdo.php';

// Connection
$conn = getConnection();
if ($conn === false) {
    exit;
}   

// Statement
try
    $sql = "INSERT INTO famosos (codigo, nome) VALUES (?, ?)";
    $stmt = $conn->prepare($sql);
    $stmt->bindValue(1, 6);
    $stmt->bindValue(2, 'Antonio');
    if ($stmt->execute()) {
        echo 'Success to save';
    } else {
        echo '<p>'.'Error to save';
    }
} catch (PDOExeption $ex){
    die ('Error: '. $ex->getMessage());
}
?>
Zhorov
  • 28,486
  • 6
  • 27
  • 52
  • Yes. Unfortunately it took me couple days trying to understand out the error. Need to change "bd" by "db". Thanks for the contributions! – messier Apr 22 '19 at 18:13