PDO安全插入/读取具有爆炸功能

时间:2018-01-29 17:53:15

标签: php html mysql database pdo

Database Structure Screenshot

<html>
<head>
    <link rel="stylesheet" type="text/css" href="style.css">
    <title>Database Test</title>
    <meta charset="UTF-8">
</head>

<body>

    <div id='box1'>
        <h1>Insert Info Here</h1>
            <hr/>
            <form action="" method="post">
            <label>Client name :</label>
            <input type="text" name="name" id="name" required="required" placeholder="Please Enter Name"/><br /><br />
            <label>Client surname :</label>
            <input type="text" name="surname" id="surname" required="required" placeholder="Please Enter Surname"/><br /><br />
            <label>Client email :</label>
            <input type="text" name="email" id="email" required="required" placeholder="Please Enter Email"/><br /><br />
            <input type="submit" value=" Submit " name="submit"/><br />
            </form>
    </div>

    <?php
        echo '<pre>';
        print_r($_POST);
        echo '</pre>';

        include_once "connect_bdd.php"; 
        include_once "fonctions_bdd.php";
        $bdd=info_bdd();
        $dbh=connectBase($bdd);


    ?>

</body>

</html>

这是我的主要HTML代码,我试图在PDO中询问/输入数据库

<?php

function info_bdd(){
$bdd="mydb";
return($bdd);
}


function connectBase($bdd){


//base locale
$user="root";$pass="";$dbh="erreur";
try {
    $dbh = new PDO('mysql:host=localhost;dbname='.$bdd, $user, $pass, array( PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $dbh->exec("SET CHARACTER SET utf8");
    }
catch (PDOException $e){print "Erreur !: " . $e->getMessage() . "<br/>";die();}
return $dbh;

}
function closeConnectBase(){$dbh = null;}
?>

这是连接数据库的PHP文件(connect_bdd.php)

<?php
include_once "connect_bdd.php"; 

function ask($req,$param,$bdd){
//echo "<hr>count tbl param = ".count($param)."<br>req = ".$req;
$dbh=connectBase($bdd);
//Préparer la requête
$prep = $dbh->prepare($req);
//Associer des valeurs aux place holders
$count_prep=0;
foreach($param as $key=>$val){
    $count_prep++;
    $parametre=explode("µ",$val);
    switch($parametre[0]){
        case "str":$prep->bindValue($count_prep, $parametre[1], PDO::PARAM_STR);break;
        case "int":$prep->bindValue($count_prep, $parametre[1], PDO::PARAM_INT);break;
        case "bool":$prep->bindValue($count_prep, $parametre[1], PDO::PARAM_BOOL);break;
        case "null":$prep->bindValue($count_prep, $parametre[1], PDO::PARAM_NULL);break;
        default:$prep->bindValue($count_prep, $parametre[1], PDO::PARAM_STR);break;
    }
}
//Compiler et exécuter la requête
$prep->execute();
//Récupérer toutes les données retournées
$rep = $prep->fetchAll();
//Clore la requête préparée
$prep->closeCursor();
$prep = NULL;
closeConnectBase();
return $rep;
}

function make($req){
try{
$dbh=connectBase(info_bdd("name"));
$prep = $dbh->prepare($req);
$prep->execute();
}
catch (PDOException $e)
{
// exit ($e -> getMessage());
}
}

?>

这是包含ask和make类的php文件(fonctions_bdd.php)。

我在进行研究时发现了两个额外的php文件,但现在我陷入困境,无法找到一种方法来使用爆炸功能插入数据库并从数据库中提取(I&#39; ll也会在那里添加CSS文件,以防万一)。在此先感谢您的帮助!

#box1{width:200;
float:left;
border-radius:8px;
font-family: Arial;
border: 2px solid #ccc;
padding: 10px 40px 25px;
margin-top: 70px;
margin-right: 20px;
}

0 个答案:

没有答案