- Edited
Fatal error: Uncaught mysqli_sql_exception: Incorrect integer value: '' for column 'id' at row 1 in /var/www/html/session/session/function.php:27 Stack trace: #0 /var/www/html/session/session/function.php(27): mysqli_query(Object(mysqli), 'INSERT INTO tb_...') #1 /var/www/html/session/session/registration.php(7): Register->registration('David Garcia', 'davidgrcias', 'david6@gmail.co...', '123', '123') #2 {main} thrown in /var/www/html/session/session/function.php on line 27
oop_reglog.sql
CREATE DATABASE oop_reglog;
USE oop_reglog
CREATE TABLE tb_user (
id int(11) NOT NULL AUTO_INCREMENT,
name varchar(50) NOT NULL,
username varchar(50) NOT NULL,
email varchar(50) NOT NULL,
password varchar(50) NOT NULL,
PRIMARY KEY (id)
);
INSERT INTO tb_user(id,name,username,email, password) VALUES
(4,'David Garcia Saragih','david','david@gmail.com','123');
registration.php
<?php
require 'function.php';
$register = new Register();
if(isset($_POST["submit"])){
$result = $register->registration($_POST["name"], $_POST["username"], $_POST["email"], $_POST["password"], $_POST["confirmpassword"]);
if($result == 1){
echo
"<script> alert('Registration Successful'); </script>";
}
elseif($result == 10){
echo
"<script> alert('Username or Email Has Already Taken'); </script>";
}
elseif($result == 100){
echo
"<script> alert('Password Does Not Match'); </script>";
}
}
?>
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Registration</title>
</head>
<body>
<h2>Registration</h2>
<form class="" action="" method="post" autocomplete="off">
<label for="">Name : </label>
<input type="text" name="name" required value=""> <br>
<label for="">Username : </label>
<input type="text" name="username" required value=""> <br>
<label for="">Email : </label>
<input type="email" name="email" required value=""> <br>
<label for="">Password : </label>
<input type="password" name="password" required value=""> <br>
<label for="">Confirm Password : </label>
<input type="password" name="confirmpassword" required value=""> <br>
<button type="submit" name="submit">Register</button>
</form>
<br> <br>
<a href="login.php">Login</a>
</body>
</html>
function.php
<?php
session_start();
class Connection{
public $host = "localhost";
public $user = "root";
public $password = "P-11fl32fg14";
public $db_name = "oop_reglog";
public $conn;
public function __construct(){
$this->conn = mysqli_connect($this->host, $this->user, $this->password, $this->db_name);
}
}
class Register extends Connection{
public function registration($name, $username, $email, $password, $confirmpassword){
$duplicate = mysqli_query($this->conn, "SELECT * FROM tb_user WHERE username = '$username' OR email = '$email'");
if(mysqli_num_rows($duplicate) > 0){
return 10;
// Username or email has already taken
}
else{
if($password == $confirmpassword){
//ERROR IN THIS LINE
$query = "INSERT INTO tb_user VALUES('', '$name', '$username', '$email', '$password')";
mysqli_query($this->conn, $query);
return 1;
// Registration successful
}
else{
return 100;
// Password does not match
}
}
}
}
it's working one time with id 1 but I tried without this field and with field with to '' is not working in all two cases
$query = "INSERT INTO tb_user VALUES('1', '$name', '$username', '$email', '$password')";