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

<?php
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;
die();
if($insert = $conn->query("
INSERT INTO User (username, email, contact_number, password)
VALUES ('{$this->name}', '{$this->email}', {$this->contact_number}, '{$this->password}')
"))
{
print_r($insert);
}
// 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","mnksingh1996@gmail.com","9810352160","123456"));
// $user = new User("Mayank","mnksingh1996@gmail.com","9810352160","123456");
// print_r($user);
// $user->mySqlConnect();

?>


allforms.php page

<?php
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

<?php
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">
<head>
<meta charset="UTF-8">
<title>Schedular</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" >
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" >
</head>
<body>
<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>
<div class="form-group">
<label for="Email">Email</label>
<input type="email" class="form-control" name="email">
</div>
<div class="form-group">
<label for="Contact-info">Contact</label>
<input type="number" class="form-control" name="contact_number">
</div>
<div class="form-group">
<label for="Password">Password</label>
<input type="password" class="form-control" name="password">
</div>
<input type="hidden" name="type" value="register">
<button type="submit" class="btn btn-default">Register</button>
</form>
</div>
<script src="http://code.jquery.com/jquery-3.2.1.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</body>
</html>


End of code

Answer Source

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

User.php

  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').

dbconnect.php

  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