使用 OOPS 和 MySQLi 将数据插入数据库


insert data into database using oops and 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();
    }
?>

这是我的用户.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;
    }
}
?>

最后我的注册.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>

我无法将数据插入数据库,当我提交按钮时没有任何反应。请帮助我解决我是PHP新手的问题。还想在同一字段中添加图像。提前谢谢。

您的

查询中有不需要的,

 $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"/>
<?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);
}
?>