forked from dynamo3/php-db-forms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
86 lines (63 loc) · 2.21 KB
/
index.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
error_reporting(E_ALL);
ini_set('display_errors', 'on');
$errors = array();
$firstname = '';
$lastname = '';
$username = '';
if($_POST) {
if(isset($_POST['firstname'])) {
$firstname = $_POST['firstname'];
}
if(isset($_POST['lastname'])) {
$lastname = $_POST['lastname'];
}
if(isset($_POST['username'])) {
$username = $_POST['username'];
}
print_r($_POST);
if($_POST['password'] != $_POST['password2']) {
array_push($errors, "passwords do not match");
}
$mysqli = new mysqli("localhost", "root", "", "userreg");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " .
$mysqli->connect_error;
}
$query = "SELECT * FROM user WHERE username = \"{$_POST['username']}\"";
echo $query;
$results = $mysqli->query($query);
if($results->num_rows > 0) {
array_push($errors, "username exists");
} else {
//echo "username available";
$insert = "INSERT INTO user (firstname, lastname, username, password) " .
"VALUES (\"{$_POST['firstname']}\", \"{$_POST['lastname']}\", " .
"\"{$_POST['username']}\", \"{$_POST['password']}\") " ;
echo $insert;
$mysqli->query($insert);
if($mysqli->errno) {
echo "there was an error: " . $mysqli->error;
}
}
//print_r($results);
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<?php include('header.php');?>
<form action="index.php" method="POST">
First: <input type="text" name="firstname" value="<?php echo $firstname; ?>"><br>
Last: <input type="text" name="lastname" value="<?php echo $lastname; ?>"><br>
Username: <input type="text" name="username" value="<?php echo $username; ?>"><br>
Password: <input type="text" name="password"><br>
Password 2: <input type="text" name="password2"><br>
<button type="submit">Go</button>
</form>
</body>
</html>