Created
June 21, 2017 14:18
-
-
Save phpfiddle/e8d8b885cac89637598233d433c3ac31 to your computer and use it in GitHub Desktop.
[ Posted by Hebert ] Let's try to convert Mysql to PDO
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
define( 'MYSQL_HOST', '//////' ); | |
define( 'MYSQL_USER', '//////' ); | |
define( 'MYSQL_PASSWORD', '//////' ); | |
define( 'MYSQL_DB_NAME', '//////' ); | |
try | |
{ | |
$PDO = new PDO( 'mysql:host=' . MYSQL_HOST . ';dbname=' . MYSQL_DB_NAME, MYSQL_USER, MYSQL_PASSWORD, array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8')); | |
} | |
catch ( PDOException $e ) | |
{ | |
echo 'Erro ao conectar com o MySQL: ' . $e->getMessage(); | |
} | |
$nome = addslashes ($_POST['nome']); | |
$foto = addslashes ($_POST['foto']); | |
$cpf = addslashes ($_POST['cpf']); | |
$rg = addslashes ($_POST['rg']); | |
$email = addslashes ($_POST['email']); | |
$telefone = addslashes ($_POST['telefone']); | |
$endereco = addslashes ($_POST['endereco']); | |
$bairro = addslashes ($_POST['bairro']); | |
$cidade = addslashes ($_POST['cidade']); | |
$estado = addslashes ($_POST['estado']); | |
$observacoes = addslashes ($_POST['observacoes']); | |
$curso = addslashes ($_POST['curso']); | |
$sql = 'SELECT * FROM curso WHERE cpf = :cpf'; | |
$data = $PDO->query($sql); | |
$rows = $data->fetchAll(); | |
$num_rows = count($rows); | |
if($num_rows > 0){ | |
echo '<script type="text/javascript">alert("CPF já cadastrado em nossa base de dados!");</script>'; | |
} | |
$sql = "INSERT INTO curso set foto = :foto, nome = :nome, email = :email, cpf = :cpf, rg = :rg, telefone = :telefone, endereco = :endereco, bairro = :bairro, cidade = :cidade, estado = :estado, observacoes = :observacoes, curso = :curso"; | |
$stmt = $PDO->prepare( $sql ); | |
$stmt->bindParam( ':foto', $foto ); | |
$stmt->bindParam( ':nome', $nome ); | |
$stmt->bindParam( ':email', $email ); | |
$stmt->bindParam( ':cpf', $cpf ); | |
$stmt->bindParam( ':rg', $rg ); | |
$stmt->bindParam( ':telefone', $telefone ); | |
$stmt->bindParam( ':endereco', $endereco ); | |
$stmt->bindParam( ':bairro', $bairro ); | |
$stmt->bindParam( ':cidade', $cidade ); | |
$stmt->bindParam( ':estado', $estado ); | |
$stmt->bindParam( ':observacoes', $observacoes ); | |
$stmt->bindParam( ':curso', $curso ); | |
$result = $stmt->execute(); | |
echo '<script type="text/javascript">alert("Matrícula realizada com sucesso!");</script>'; | |
echo "<script>window.location = 'https://pag.ae/bck57zw';</script>"; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment