使用PDO的OPP PHP CRUD [英] OPP PHP CRUD using PDO

查看:70
本文介绍了使用PDO的OPP PHP CRUD的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 new 到PHP并尝试学习OPP PHP。 

i我创建一个注册表单(一个简单的)来学习OPP PHP,我提交数据时,成功消息显示但是数据数据 base 中未插入 <= span> 。 p $ p>







如果有人知道如何执行基本的CRUD(插入,更新,删除,选择)使用OPP PHP和PDO在PHP中操作然后请提供链接,来源,示例,这样我就可以从中学习



我在互联网上搜索但是熊能够找到连接代码...寻找一个描述性的答案,因为我认为这个问题也将帮助很多尝试/想要学习OOP PHP的人。



任何帮助都将不胜感激。



谢谢。



我尝试过:



connection.php



  <   pre     lang   =  C# >  <? php 

class DBConnection扩展PDO
{
public function __construct()
{
$ host = ' mysql:host = localhost; dbname = OOP';
$ user = ' root';
$ password = ' ';
parent :: __ construct($ host,$ user,$ password);
$ this-> setAttribute(PDO :: ATTR_ERRMODE,PDO :: ERRMODE_EXCEPTION);
// 在使用MySQL驱动程序时始终禁用模拟的预准备语句
$ this - > setAttribute(PDO :: ATTR_EMULATE_PREPARES,false);
}
}

?>







Index.php



 <? php 
session_start();
/ * 包含'classes.php';
// $ dbHandle = new DBConnection();
$ ins = new basic_operation();
$ ins-> Insert_Data(); * /




?>


< div class = col-lg-12 >

< div class = col-lg-1 > < / div >

< div = col-lg-7 >
< 表格 action = user_data.php 方法 = 发布 >
< input 类型 = text name = username id = 用户名 class < span class =code-keyword> =
form-control > < span class =code-keyword>< br >
< 输入 类型 = email 名称 = email id = email class = form-control > < br >
< 输入 类型 = 密码 名称 = 密码 class = 表单控制 id = 密码 > < br > ;
< 输入 类型 = submit name = 提交 id = 提交 value = 提交 >
< / form >
< / div >

< / div >
< div >
<? php if (isset($ _ SESSION [' insert']))
{
echo $ _SESSION [' insert ];
unset($ _ SESSION [' insert']);
}

?> < / div >

< / div >
< / div >





User_data.php



 <?php  
session_start();
包括' classes.php';

$ insert = new basic_operation();
$ usr = $ insert-> Insert_Data();
// return $ insert;
$ _SESSION [' insert'] = ' 数据插入成功';
header(' location:index.php');

?>





Classes.php



 <?php  
包括' connection.php';

class basic_operation
{

公共函数Insert_Data()
{
if (isset($ _ POST [' submit'])){
$ user = $ _POST [' 用户名];
$ email = $ _POST [' email ];
$ pass = $ _POST [' password ];
$ smt = new DBConnection();
$ qry = $ smt-> prepare( insert into student(User_Name,Email,Password)VALUES(' $ user ',' $ email ',' $ pass '));
$ qry-> execute();

}
}
}
?>

解决方案

host = ' mysql:host = localhost; dbname = OOP ';


user = ' root' ;


password = ' ';
parent :: __ construct(


I am new to PHP and trying to learn OPP PHP.

i am creating a registration form (a simple one) to learn OPP PHP, when i submit data the success message shows up but data is not inserted in data base.





If any one knows how to perform basic CRUD (insert, update,delete,select) operation in PHP using OPP PHP and PDO Then please provide the link, source, Example, so i can learn from it

I searched on the internet but bearly able to find the connection code... Looking for a descriptive answer, as i think this question will also help a lot of people who try/want to learn OOP PHP.

Any help will appreciated.

Thanks.

What I have tried:

connection.php

<pre lang="C#"><?php

class DBConnection extends PDO
{
    public function __construct()
    {
        $host='mysql:host=localhost;dbname=OOP';
        $user='root';
        $password='';
        parent::__construct($host,$user,$password);
        $this->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        // always disable emulated prepared statement when using the MySQL driver
        $this->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
    }
}

?>




Index.php

<?php
session_start();
/*include 'classes.php';
//$dbHandle = new DBConnection();
$ins=new basic_operation();
$ins->Insert_Data();*/



?>


    <div class="col-lg-12">

        <div class="col-lg-1"></div>

        <div class="col-lg-7">
            <form action="user_data.php" method="post" >
                <input type="text" name="username" id="username" class="form-control"><br>
                <input type="email" name="email" id="email" class="form-control"><br>
                <input type="password" name="password" class="form-control" id="password"><br>
                <input type="submit" name="submit" id="submit" value="submit">
            </form>
                    </div>

                </div>
<div>
    <?php if (isset($_SESSION['insert']))
    {
        echo $_SESSION['insert'];
        unset($_SESSION['insert']);
    }

    ?></div>

        </div>
    </div>



User_data.php

<?php
session_start();
include 'classes.php';

    $insert=new basic_operation();
    $usr=$insert->Insert_Data();
    //return $insert;
    $_SESSION['insert']='data inserted successfuly';
    header('location:index.php');

?>



Classes.php

<?php
include 'connection.php';

class basic_operation
{

    public function Insert_Data()
    {
        if (isset($_POST['submit'])) {
            $user = $_POST['username'];
            $email = $_POST['email'];
            $pass = $_POST['password'];
            $smt = new DBConnection();
            $qry = $smt->prepare("insert into student(User_Name,Email,Password) VALUES ('" . $user . "','" . $email . "','" . $pass . "')");
            $qry->execute();

        }
    }
}
?>

解决方案

host='mysql:host=localhost;dbname=OOP';


user='root';


password=''; parent::__construct(


这篇关于使用PDO的OPP PHP CRUD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆