【问题标题】:insert data into database using oops and mysqli使用 oops 和 mysqli 将数据插入数据库
【发布时间】:2016-05-15 06:29:17
【问题描述】:

这是我的数据库连接文件。 (db_configenter 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>

我无法将数据插入数据库,当我提交按钮时什么也没发生。请帮我解决问题。我是 php 新手。 还想在同一字段中添加图像。 提前致谢。

【问题讨论】:

    标签: php mysqli


    【解决方案1】:

    您的查询中有一个不需要的,

     $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"/>
    

    【讨论】:

      【解决方案2】:
      <?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);
      }
      
      ?>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-11-21
        • 2015-02-27
        • 2016-11-27
        • 2011-01-09
        • 1970-01-01
        • 2022-06-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多