Parašė IPaulius· 2011 Bir. 2 12:06:44
#1
Siunčiu pilną kodą.
Registracijos forma:
<?php
if( isset($_SESSION['ERRMSG_ARR']) && is_array($_SESSION['ERRMSG_ARR']) && count($_SESSION['ERRMSG_ARR']) >0 ) {
echo '<ul class="err">';
foreach($_SESSION['ERRMSG_ARR'] as $msg) {
echo '<li>',$msg,'</li>';
}
echo '</ul>';
unset($_SESSION['ERRMSG_ARR']);
}
?>
Klaidų tikrinimo kodas:
<?php
//Start session
session_start();
//Include database connection details
require_once('config.php');
//Array to store validation errors
$errmsg_arr = array();
//Validation error flag
$errflag = false;
//Connect to <span style="border-bottom: 1px dotted black;">mysql</span> server
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link) {
die('Failed to connect to server: ' . mysql_error());
}
//Select database
$db = mysql_select_db(DB_DATABASE);
if(!$db) {
die("Unable to select database");
}
//Function to sanitize values received from the form. Prevents SQL injection
function clean($str) {
$str = @trim($str);
if(get_magic_quotes_gpc()) {
$str = stripslashes($str);
}
return mysql_real_escape_string($str);
}
//Sanitize the POST values
$fname = clean($_POST['fname']);
$lname = clean($_POST['lname']);
$login = clean($_POST['login']);
$password = clean($_POST['password']);
$cpassword = clean($_POST['cpassword']);
//Input Validations
if($fname == '') {
$errmsg_arr[] = 'First name missing';
$errflag = true;
}
if($lname == '') {
$errmsg_arr[] = 'Last name missing';
$errflag = true;
}
if($login == '') {
$errmsg_arr[] = 'Login ID missing';
$errflag = true;
}
if($password == '') {
$errmsg_arr[] = 'Password missing';
$errflag = true;
}
if($cpassword == '') {
$errmsg_arr[] = 'Confirm password missing';
$errflag = true;
}
if( strcmp($password, $cpassword) != 0 ) {
$errmsg_arr[] = 'Passwords do not match';
$errflag = true;
}
//Check for duplicate login ID
if($login != '') {
$qry = "SELECT * FROM members WHERE login='$login'";
$result = mysql_query($qry);
if($result) {
if(mysql_num_rows($result) > 0) {
$errmsg_arr[] = 'Login ID already in use';
$errflag = true;
}
@mysql_free_result($result);
}
else {
die("Query failed");
}
}
//If there are input validations, redirect back to the registration form
if($errflag) {
$_SESSION['ERRMSG_ARR'] = $errmsg_arr;
session_write_close();
header("location: register-form.php");
exit();
}
//Create INSERT query
$qry = "INSERT INTO members(firstname, lastname, login, passwd) VALUES('$fname','$lname','$login','".md5($_POST['password'])."')";
$result = @mysql_query($qry);
//Check whether the query was successful or not
if($result) {
header("location: register-success.php");
exit();
}else {
die("Query failed");
}
?>
Kaip padaryt ,kad kai registracijos formoje laukeliai yra tušti rodytų ne visas klaidas ,bet tik po vieną?
Pavizdys: facebook.com --- Registracija. Paliekate turšius langelius r spaudžiate Registracija. Rodys vieną klaidą.
Kas padės +11 ir didelis ačiū.
....................................................................................................................................................................................................
Redagavo IPaulius· 2011 Bir. 2 12:06:30
Parašė Pakartoti slaptažodį· 2011 Bir. 2 12:06:07
#2
Paskutinis pasiūlymas klaidų if'us sujungti ir padaryti iš jų elseif'us. :)
if($fname == "") {
$errmsg_arr[] = "First name missing";
$errflag = true;
}elseif($lname == "") {
$errmsg_arr[] = "Last name missing";
$errflag = true;
}elseif($login == "") {
$errmsg_arr[] = "Login ID missing";
$errflag = true;
}elseif($password == "") {
$errmsg_arr[] = "Password missing";
$errflag = true;
}elseif($cpassword == "") {
$errmsg_arr[] = "Confirm password missing";
$errflag = true;
}elseif(strcmp($password, $cpassword) != 0) {
$errmsg_arr[] = "Passwords do not match";
$errflag = true;
}
Redagavo Pakartoti slaptažodį· 2011 Bir. 2 12:06:45
Parašė IPaulius· 2011 Bir. 2 12:06:40
#4
3 diena nemelavo. Ačiū abiems. Tylesburg kodas tiko ir algiuxo ,net nebandysiu ,nes manau ,kad per daug else. O šiaip tai vistiek +11 gauni.
PAGALIAU!
PS. Tylesburg jau turi +11 , o antrą karta neleidžia.