Created
March 24, 2018 20:52
-
-
Save npapratovic/73b2de1b5f88e19be8fbb03c92ac4b85 to your computer and use it in GitHub Desktop.
trikoder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
session_start(); | |
// variable declaration | |
$username = ""; | |
$country = ""; | |
$email = ""; | |
$first_name = ""; | |
$last_name = ""; | |
$birth_date = ""; | |
$spol = ""; | |
$country = ""; | |
$errors = array(); | |
// connect to database | |
$db = mysqli_connect('localhost', 'root', '', 'trikoder'); | |
// REGISTER USER | |
if (isset($_POST['reg_user'])) { | |
// receive all input values from the form | |
$username = mysqli_real_escape_string($db, $_POST['username']); //escape epecial strings.. | |
$country = mysqli_real_escape_string($db, $_POST['country']); //escape epecial strings.. | |
$first_name = mysqli_real_escape_string($db, $_POST['first_name']); //escape epecial strings.. | |
$last_name = mysqli_real_escape_string($db, $_POST['last_name']); //escape epecial strings.. | |
$birth_date = mysqli_real_escape_string($db, $_POST['birth_date']); //escape epecial strings.. | |
$spol = mysqli_real_escape_string($db, $_POST['spol']); //escape epecial strings.. | |
$email = mysqli_real_escape_string($db, $_POST['email']); //escape epecial strings.. | |
$password_1 = mysqli_real_escape_string($db, $_POST['password_1']); | |
$password_2 = mysqli_real_escape_string($db, $_POST['password_2']); | |
// Jako jednostavna validacija podataka. Provjera se samo da li je polje popunjeno. | |
if (empty($username)) { array_push($errors, "Korisničko ime je obavezno"); } | |
if (empty($country)) { array_push($errors, "Država je obavezno"); } | |
if (empty($first_name)) { array_push($errors, "Ime je obavezno"); } | |
if (empty($last_name)) { array_push($errors, "Prezime je obavezno"); } | |
if (empty($birth_date)) { array_push($errors, "Datum rođenja je obavezno"); } | |
if (empty($spol)) { array_push($errors, "Spol je obavezno"); } | |
if (empty($email)) { array_push($errors, "E-mail je obavezan"); } | |
if (empty($password_1)) { array_push($errors, "Lozinka je obavezna"); } | |
if ($password_1 != $password_2) { | |
array_push($errors, "Lozinke se ne podudaraju"); | |
} | |
// Ako nema greške, kreiraj novog korisnika. | |
if (count($errors) == 0) { | |
$password = md5($password_1);//encrypt the password before saving in the database | |
$query = "INSERT INTO users (username, first_name, last_name, email, birth_date, password, spol, country) | |
VALUES('$username', '$first_name', '$last_name', '$email', '$birth_date', '$password', '$spol', '$country')"; | |
echo "<pre>Debug: $query</pre> "; | |
$result = mysqli_query($db, $query); | |
if ( false===$result ) { | |
printf("error: %s\n", mysqli_error($db)); | |
} | |
else { | |
echo "<pre>korisnik registriran</pre> "; | |
} | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment