使用oops和mysqli将数据插入数据库

时间:2016-02-05 10:12:41

标签: php mysqli

这是我的数据库连接文件。 (DB_CONFIG enter code here。PHP)

<?php
    $con = mysqli_connect("localhost","root","","oop");
    if(mysqli_connect_errno())
    {
        echo "failed to connect to mysql:" . mysqli_connect_error();
    }
?>

这是我的user.class.php代码

<?php
include "db_config.php";

class User
{
    public function registration($fname,$lname,$username,$email,$password)
    {
        $sql = $con->query("INSERT INTO users_registration(fname,lname,username,email,password) 
        VALUES('$fname','$lname','$username','$email','$password',)");
        return $sql;
    }
}
?>

最后我的registrtaion.php文件就在这里

<?php
include "class.user.php";
$user = new User();
if (isset($_POST['submit']))
{

    $fname = $_POST['fname'];
    $lname = $_POST['lname'];
    $username = $_POST['username'];
    $email = $_POST['email'];
    $password = $_POST['password'];

    $user->registration($fname,$lname,$username,$email,$password);
    echo "registration success";
}
?>

<!DOCTYPE HTML>
<html>
<head>
<title>Registration Form</title>
<link rel="stylesheet" href="css/style.css" type="text/css">
</head>

<body>
<div class="wrapper"><!--wrapper start here-->
<form action="" method="POST">
<input type="text" name="fname" placeholder="First name"/><br>
<input type="text" name="lname" placeholder="Lastst name"/><br>
<input type="text" name="username" placeholder="User name"/><br>
<input type="text" name="email" placeholder="Email id"/><br>
<input type="text" name="password" placeholder="Password"/><br>
<input type="text" name="cpassword" placeholder="Confirm Password"/><br>
<input type="button" value="submit" name="submit" class="btn"/>
</form>
</div><!--wrapper ends here-->
</body>
</html>

我无法将数据插入数据库,当我提交按钮时没有发生任何事情.Plz帮我解决了问题。我是新的php。 还想在同一个字段中添加图像。 提前谢谢。

2 个答案:

答案 0 :(得分:2)

您的查询中有一个不受欢迎的,

 $sql = $con->query("INSERT INTO users_registration(fname,lname,username,email,password) 
        VALUES('$fname','$lname','$username','$email','$password',)");
                                                                 ^

此外,要提交HTML表单,按钮类型应为submit。改变

<input type="button" value="submit" name="submit" class="btn"/>

<input type="submit" value="submit" name="submit" class="btn"/>

答案 1 :(得分:0)

<?php 

    class dbins
    {

        function insert_data($name,$age)``
        {
            $runn = mysqli_connect("localhost","root","","oop");
            $ins = "INSERT into ajax(name,age) values('$name','$age')";
            $run = mysqli_qyery($runn,$ins);            
            return $run;
        }
    }

?>
<form name="insert" method="POST">
    <table align="center">      
        <thead>
            <h1 align="center">Simple Insert Use OOP</h1>
            <tr>
                <th>Name</th>
                <td><input type="text" name="name" required="" placeholder="Enter Your Name"></td>
            </tr>
            <tr>
                <th>Age</th>
                <td><input type="text" name="age" required="" placeholder="Enter Your Age"></td>
            </tr>
            <tr align="center">
                <td><input type="submit" name="insert" value="Insert" ></td>
            </tr>
        </thead>
    </table>    
</form>
<?php 

$con = new dbins();

if(isset($_POST['insert']))
{
    $name = $_POST['name'];
    $age = $_POST['age'];

    $con->insert_data($name,$age);
}

?>