mayank singh mayank singh -3 years ago 79
PHP Question

Cannot Insert Values into Database through mysqli

So i am trying around object oriented approach using plain php but i cant seem to insert values using sqli extension into mysql database.Its not throwing a single error and for some reason bootstrap isn't working either. If someone can push me into right direction that would be nice.

I created a class in dbconnect.php which and i am creating an instance in constructor of User class in User.php so that every method can use it.

user.php page

include 'dbconnect.php';

class User {
public $name;
public $email;
public $contact_number;
public $password;
public $connect;
public function __construct($name, $email, $contact_number, $password) {
$this->name = $name;
$this->email= $name;
$this->contact_number = $contact_number;
$this->password = $password;
$connection = new Connection();
$this->connect = $connection->connect();
public function register()
echo $this.username;
if($insert = $conn->query("
INSERT INTO User (username, email, contact_number, password)
VALUES ('{$this->name}', '{$this->email}', {$this->contact_number}, '{$this->password}')
// echo $sql;
// die;
// $result = $this->connect->query($sql);
// if($result) {
// return $this->email." has been created and saved in the system";
// }
// else {
// return $this->email . " was not registered, try again";
// }
public function login($email, $password)
// die(new User("Mayank","","9810352160","123456"));
// $user = new User("Mayank","","9810352160","123456");
// print_r($user);
// $user->mySqlConnect();


allforms.php page

include 'user.php';
$type = $_POST['type'];
if($type == 'register') {
$name = $_POST['username'];
$email = $_POST['email'];
$contact_number = $_POST['contact_number'];
$password = $_POST['password'];
$user = new User($name,$email, $contact_number, $password);

$result = $user->register();
echo $result;

dbconnect.php page

class Connection {
public $servername;
public $username;
public $password;
public $dbname;
function __construct(){
$this->servername = "localhost";
$this->username = "root";
$this->password = "123456";
$this->dbname = "scheduler";
public function connect()
$conn = new mysqli($this->servername, $this->username, $this->password,$this->dbname);
if ($conn->connect_errno) {
echo $conn->connect_error;
//die("Connection failed: " . mysqli_connect_error());

index.php page

<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">
<link rel="stylesheet" href="" >
<link rel="stylesheet" href="" >
<div class="container">
<form class="form-inline" action="allforms.php" method="POST">
<div class="form-group">
<label for="Username">Username</label>
<input type="text" class="form-control" name="username">
<div class="form-group">
<label for="Email">Email</label>
<input type="email" class="form-control" name="email">
<div class="form-group">
<label for="Contact-info">Contact</label>
<input type="number" class="form-control" name="contact_number">
<div class="form-group">
<label for="Password">Password</label>
<input type="password" class="form-control" name="password">
<input type="hidden" name="type" value="register">
<button type="submit" class="btn btn-default">Register</button>
<script src=""></script>
<script src=""></script>

End of code

Answer Source

There are bunch of small-small errors you have to fix them first.


  1. $this->connect will not have any value as $connection->connect is not retruing any value/object.
  2. In register method $conn->query won't work because $conn is not defined. it should be $this->connect->query('Query here').


  1. Add return to connect function
  2. check if this is right way to connect with mysqli.

Note: Read about function returns and other oops basic concepts

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download