Advertisement
luistavares

Inserindo em banco de dados com PHP

Feb 22nd, 2013
517
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.63 KB | None | 0 0
  1. <?php
  2.    
  3.     /* Conectando com o banco de dados */
  4.     $datasource = 'mysql:host=localhost;dbname=exemplo';
  5.     $user = 'root';
  6.     $pass = '';
  7.     $db = new PDO($datasource, $user, $pass);
  8.    
  9.     $query = "INSERT INTO aluno (
  10.                   nome,
  11.                   email,
  12.                   nota                 
  13.                 ) VALUES (?,?,?)
  14.             ";
  15.            
  16.     $nome = 'João Pedro';
  17.     $email = 'joao.pedro@hotmail.com';
  18.     $nota = 6.5;       
  19.            
  20.     $stm = $db->prepare($query);
  21.     $stm->bindParam(1, $nome);
  22.     $stm->bindParam(2, $email);
  23.     $stm->bindParam(3, $nota);
  24.    
  25.     if($stm->execute()) {
  26.         print '<p>Aluno cadastrado com sucesso!</p>';
  27.     }
  28.     else {
  29.         print '<p>Erro ao cadastrar aluno!</p>';
  30.     }  
  31. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement