-
Notifications
You must be signed in to change notification settings - Fork 0
/
view.php
45 lines (39 loc) · 1.67 KB
/
view.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$db = mysqli_connect('localhost', 'root', '', 'temphumidnew');
if (!$db) {
echo json_encode("Database connection failed");
} else {
if (isset($_POST['username']) && isset($_POST['password'])) {
$username = $_POST['username'];
$password = $_POST['password'];
// Check if the username already exists
$sql = "SELECT username FROM user WHERE username = ?";
$stmt = mysqli_prepare($db, $sql);
mysqli_stmt_bind_param($stmt, 's', $username);
mysqli_stmt_execute($stmt);
mysqli_stmt_store_result($stmt);
$count = mysqli_stmt_num_rows($stmt);
if ($count > 0) {
echo json_encode("Error: Username already exists");
} else {
// Hash the password securely
$hashedPassword = password_hash($password, PASSWORD_BCRYPT);
// Insert the username and hashed password into the database
$insert = "INSERT INTO user (username, password) VALUES (?, ?)";
$stmt = mysqli_prepare($db, $insert);
mysqli_stmt_bind_param($stmt, 'ss', $username, $hashedPassword);
$query = mysqli_stmt_execute($stmt);
if ($query) {
echo json_encode("Success");
} else {
echo json_encode("Error: Unable to insert data");
}
}
} else {
echo json_encode("Error: Username and password are required.");
}
mysqli_close($db);
}
}
?>