Input validation

How to validate if the input data in existing on the DB…What is the proper way to do this?

Thanks…

Basic way:


$sql = 'SELECT * FROM table WHERE input = "'. cleaned($_POST['user_input']) .'"';
$result = mysql_query($sql);
if( mysql_num_rows($result) > 0 ) {
	echo 'Oops the data already exists';
}

i want to execute it if the cursor lost the focus on the textbox.

You want to do it via AJAX. Here is a quick example:


<?php

db_conn();
function db_conn() {
	mysql_connect('dbHost', 'dbUser', 'dbPass');
	mysql_select_db('test');	
} 

function clean( $str ) {
	if(get_magic_quotes_gpc()) {
		$str = stripslashes($str);
	}
	return mysql_real_escape_string($str);
}

function checkUserName() {
	// From Ajax
	if (isset($_POST['name'])) {
		$sql = 'SELECT userName 
				FROM users 
				WHERE userName = "'. clean($_POST['name']) .'"';
		$result = mysql_query($sql);
		if( mysql_num_rows($result) > 0 ) {
			echo 'Oops the data already exists';
		}
		die();
	}
}
checkUserName();
 

?>
<!doctype html>
<html>
	<head>
		<title>Test</title>
	</head>
	<body>
		<form method="post" name="test" action="#">
			<label for="userName">User Name</label>
			<input id="userName" name="userName" type="text" />
			<input type="submit" name="formSubmit" value="Submit" />
		</form>
		<div id="formResult"></div>	
		<script>
			window.onload = function() {
				checkName();
			};
		
			function XHR() {
				var xhr = false;
				xhr = window.ActiveXObject
					? new ActiveXObject("Microsoft.XMLHTTP")
					: new XMLHttpRequest();
				return xhr;
			}
		
			function request( url, snd, callback, type ) {
				var http = XHR();
				if (http) {
					http.onreadystatechange = callback;
				};
				http.open(type, url, true);
				if (type == "POST") {
					http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
					http.setRequestHeader("Content-length", snd.length);
					http.setRequestHeader("Connection", "close");
				}		
				http.send(snd);
			}		

			function checkName() {
				var frm = document.forms.test;	
				frm.userName.onblur = function() {
					request(
						"test.php", 
						"name=" + encodeURIComponent(frm.userName.value), 
						function() {
							var res = document.getElementById("formResult");
							res.innerHTML = this.responseText;
						}, 
						'POST'
					);		
					return false;
				}
			}		
		</script>
	</body>
</html>

Hi…can your please elaborate what the code means…
Thanks…

Which part do you need elaborating on?

The example is basic AJAX:

Javascript checks for the event (checkName),
Asks for the server for a response back (checkUserName()),
Then the output (this.responseText) is shown in the noted div (document.getElementById(“formResult”)) via innerHTML (res.innerHTML = this.responseText)

db_conn & clean should be self explainitory
XHR is the XMLHTTP (AJAX) object
Request is a function to handle GET & POST requests in the even you are not using a library like jQuery

Sorry for not clarifying my question…

when i input a data that exist it said that “undefined”.