Jump to content

if else not working


mdvignesh

Recommended Posts

I dont know y if else not working

 

my questions is when i type username in the database it displays username exists but if type something else it does not displays the else part in the if statement

 

signup.php

 

<? include("db.php")?>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function valid()
{
var un=document.forms[0].dun.value;
//alert(un);
if(un=="")
{
	alert("Enter username");
	document.forms[0].dun.focus();
	return false;
}

var ps=document.forms[0].pass.value;
if(ps=="")
{
	alert("Enter password");
	document.forms[0].pass.focus();
	return false;
}

var mail=document.forms[0].em.value;
if(mail=="")
{
	alert("Enter email");
	document.forms[0].em.focus();
	return false;
}

var mobile=document.forms[0].mob.value;
if(mobile=="")
{
	alert("Enter mobile");
	document.forms[0].mob.focus();
	return false;
}
}

</script>
<script src="jquery-1.7.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){

  $('#feedback').load('chech.php').show();

  $('#username_input').keyup(function() {
    $.post('chech.php',{ dun:form.dun.value },
    function(result) {
    $('#feedback').html(result).show();
    });
    });
});

</script>
</head>

<body>
<form name="form" method="post">

<label>Desired Username:</label>
<input type="text" id="username_input" name="dun"><div id="feedback"></div>
<br><br>

<label>Password:</label>
<input type="password" name="pass"><br/><br/>
<label>Confirm:</label><input type="password" name="pass2">
<br><br>

<label>Email:</label>
<input type="text" name="em">
<br><br>

<label>Mobile:</label>
<input type="text" name="mob">
<br><br><br>
<input type="submit" name="sub" value="signup" onClick="return valid();">
</form>
<?

if(isset($_REQUEST['sub']))
{

$ins=mysql_query("insert into reg (username ,password ,email ,mobile) values ('".$_REQUEST['dun']."' ,'".
	$_REQUEST['pass']."','".$_REQUEST['em']."','".$_REQUEST['mob']."') ");

if(!$ins)
{
	echo "error";
}
else
?>
<a href="login.php">Login</a>
<?
}
?>
</body>
</html>

 

chech.php

 

<?
include("db.php");
  if(isset($_POST['dun'])) {
    //echo $_REQUEST['dun'];
    $sel=mysql_query("select username from reg where username='".$_REQUEST['dun']."' ");
    $count=mysql_num_rows($sel) or die(mysql_error());
    echo $count;

    if($count>0) {
      	echo "username exists";
    }
    else
    echo "not exists";
  }
?>

Link to comment
Share on other sites

1. Use proper brackets.

if($count>0) {
echo "username exists";
} else {
echo "not exists";
}

 

2. Don't use $_REQUEST

 

3. Sanitize input before using it with a database

if(isset($_POST['dun'])) {
    $dun = mysql_real_escape_string($_POST['dun']);
    $sel=mysql_query("select username from reg where username='$dun'");

 

Your problem is the fact that you are terminating the script if there are no rows returned, on this line: $count=mysql_num_rows($sel) or die(mysql_error());

 

If mysql_num_rows() returns 0, the script is terminated and, since there is no MySQL error, you get no output. So, change it to $count=mysql_num_rows($sel);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.