Page 1 of 1

PHP script is not getting executed

Posted: Sun May 10, 2009 6:49 am
by nupur2609
Hi I am quite new to PHP.
I have a simple login page ,which validates the text fields using Java Script and then submits the data to a php page .
Here is my login page:

Code: Select all

 
<html>
<head>
<SCRIPT LANGUAGE="javascript">
<!--
 
function focus()
{
  document.forms[0].username.focus();
}
 
function checkme() //check for required fields
{
    var flag=0;
 
    if (document.Login.username.value == "")
    {alert("You did not enter your user name. Please provide it.");
    document.Login.username.focus();
    flag=1;return(false)
    
    }
     if (document.Login.username.value.length<6)
      {alert("Username is too short. ");
    document.Login.username.focus();
    flag=1;return(false)
    }
 
    if (document.Login.password.value == "")
    {alert("You did not password. Please provide it.");
    document.Login.password.focus();
    flag=1;return(false)
    }
 
    if (document.Login.password.value.length<6)
      {alert("Password is too short.");
    document.Login.password.focus();
   flag=1;return(false)
    }
      
    
}
//-->
 
</SCRIPT>
</head>
<body onLoad="focus()">
 
<div class="tdstyle">
<table width ="100%"  border="0">
    <tr>
       <td width="20%"><a href"#">GUEST?Click here to Start</a>
       </td>
           <td width="80%" align="left"><a href"#">Take the Baseline Test</a>
       </td>    
    </tr>
</table>
 
 
<form action="a1.php" method="GET" name="Login">
<table width="100%" height="80%" border="0">
       
 
    <tr width="100%">
       <td>
        
        <p>Username: <input type="text" name="username" ><br></p>
        <p>Password:  <input type="password" name="password"></p>
                 
        <p><input type="Submit" value="Login"size="16" class="myButton" onclick="return checkme()"></p>
        
 
       </td>
    </tr>
</table>
 
</form>
 
</div>
<table border="0"width="100%">
  <tr>
       <td align ="left" width="10%">
       <a href"#">About Us</a></td>
       <td align="left" width="10%"><a href"#">Contact</a>
    </td>   
    <td align="right" width="80%">
        <a href"#">Legal</a>
    </td>
  </tr>
</table>       
 
 
 
</body>
</html>
 
Here is the code for the a1.php page which is called by the previous form:

Code: Select all

<?php
   
 /********  DATABASE SETTING *******/
    
$dbhost = 'hodt'; // database host 
 
$dbuser = 'user'; // database username
    
$dbpass = 'pwd'; // database password
    
$dbname = 'dbname'; // database name
    
$mysql = mysql_connect($dbhost,$dbuser,$dbpass);
    
mysql_select_db($dbname);
   $tuname=mysql_real_escape_string($_POST['username']);    
$tpwd=mysql_real_escape_string($_POST['password']);   
    
/*INSERT INTO DB*/
   
$Query='INSERT INTO login(`username`,`password`)VALUES (\''.$tuname.'\',\''.$tpwd.'\')';   
 
 mysql_query($Query);
  echo $tuname;
// ECHO TO FLASH    
if(mysql_query($Query))
    
{
        
    $answer='ok';
        
    echo "answer=".$answer;
 
        echo $tuname;
   
}
    else
    
{
        $answer='nope';
        
    echo "answer=".$answer;
    }
 
 
mysql_close($mysql);
?> 
 
Can anyone tell me what am I doing wrong here?? When I call the same script from flash,it gets executed.
Thanks.

Re: PHP script is not getting executed

Posted: Sun May 10, 2009 9:20 am
by codex-m
--QUOTE OF ENTIRE POST REMOVED--

I think the problem is that:
Your form action method is using GET:

Code: Select all

 
<form action="a1.php" method="GET" name="Login">
 
But you are receiving data from your form in your PHP script using POST instead of GET such as here:

Code: Select all

 
$tuname=mysql_real_escape_string($_POST['username']);    
$tpwd=mysql_real_escape_string($_POST['password']);   
 
So I suggest changing your form action from GET to POST like this:

Code: Select all

 
<form action="a1.php" method="POST" name="Login">
 
And lets see if that works...

Re: PHP script is not getting executed

Posted: Sun May 10, 2009 12:23 pm
by nupur2609
I changed it to POST but it is still not working!! Thanks for the quick reply !!

Re: PHP script is not getting executed

Posted: Mon May 11, 2009 10:00 am
by codex-m
nupur2609 wrote:I changed it to POST but it is still not working!! Thanks for the quick reply !!
I think you have faulty MySQL insert statement:
Try this as this works on my side:

Replace:

Code: Select all

 
/*INSERT INTO DB*/
$Query="INSERT INTO `login` (`username`,`password`)VALUES ('$tuname',''$tpwd')";
mysql_query($Query);
 
with this:

Code: Select all

 
/*INSERT INTO DB*/
mysql_query("INSERT INTO `login` (`username`,`password`) VALUES('$tuname','$tpwd')")
or die(mysql_error());
 

Also make sure you have POST in the form and receive also in POST.

Re: PHP script is not getting executed

Posted: Mon May 11, 2009 10:09 am
by onion2k
nupur2609 wrote:I have a simple login page ,which validates the text fields using Java Script
So if I turn off Javascript I can submit what I want and break your form? Cool.

Always validate on the server. You can do JS as well if you want to, but don't rely on it.

Re: PHP script is not getting executed

Posted: Mon May 11, 2009 10:12 am
by codex-m
onion2k wrote:
nupur2609 wrote:I have a simple login page ,which validates the text fields using Java Script
So if I turn off Javascript I can submit what I want and break your form? Cool.

Always validate on the server. You can do JS as well if you want to, but don't rely on it.
This is correct. Server validation also offers more security features. But javascript offers a quick client side validation, without consuming too much server resources and more user friendly depending on application.

Re: PHP script is not getting executed

Posted: Tue May 12, 2009 3:03 am
by nupur2609
Thanks everyone for the quick replies..I am planning to put in server side validations,but my PHP script just doesnt work..Even after the changes suggested. If I cant even insert dummy records in the table,then how will I proceed!! I am at my wits end and dont know what to do.
Please HELP!!!!

Re: PHP script is not getting executed

Posted: Tue May 12, 2009 3:16 am
by susrisha

Code: Select all

 
$Query='INSERT INTO login(`username`,`password`)VALUES (''.$tuname.'',''.$tpwd.'')';  
 
 mysql_query($Query);
  echo $tuname;
// ECHO TO FLASH    
if(mysql_query($Query)) //why do you need to execute the query twice??
 
I would suggest you put an error reporting statement at first query

Code: Select all

 
if(!mysql_query($Query))
{
echo 'error in mysql query '.mysql_error();
}
else
{
echo 'successfully inserted';
}
 

Re: PHP script is not getting executed

Posted: Tue May 12, 2009 6:56 am
by nupur2609
Hi,
I have changed my php script so it looks like this:

Code: Select all

 
<?php
   
 /********  DATABASE SETTING *******/
    
$dbhost = 'host'; // database host 
 
$dbuser = 'user'; // database username
    
$dbpass = 'pwd'; // database password
    
$dbname = 'db'; // database name
    
$mysql = mysql_connect($dbhost,$dbuser,$dbpass);
    
mysql_select_db($dbname);
 
   
   
 /* VARIABLES FROM  HTML FORM */
 
   
 $tuname=mysql_real_escape_string($_POST['username']);
    
$tpwd=mysql_real_escape_string($_POST['password']);
    
   
    
    
/*INSERT INTO DB*/
   
 
$Query='INSERT INTO login(`username`,`password`)VALUES (''.$tuname.'',''.$tpwd.'')';  
 
if(!mysql_query($Query))
{
echo 'error in mysql query '.mysql_error();
}
else
{
echo 'successfully inserted';
}
 
mysql_close($mysql);
?> 
 
Still,there is no output from the echo statement.All I get is a blank page..What am I doing wrong here??
Thanks

Re: PHP script is not getting executed

Posted: Wed May 13, 2009 3:12 am
by onion2k
Do other PHP scripts work?

Re: PHP script is not getting executed

Posted: Wed May 13, 2009 4:02 am
by nupur2609
Hi,
Yes,other scripts are working.
Thanks.