PHP script is not getting executed

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nupur2609
Forum Newbie
Posts: 5
Joined: Sun May 10, 2009 6:41 am

PHP script is not getting executed

Post 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.
Last edited by Benjamin on Sun May 10, 2009 12:44 pm, edited 1 time in total.
Reason: Fixed code tags.
User avatar
codex-m
Forum Newbie
Posts: 15
Joined: Sun May 10, 2009 8:08 am

Re: PHP script is not getting executed

Post 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...
Last edited by Benjamin on Sun May 10, 2009 12:45 pm, edited 1 time in total.
Reason: Removed quote of entire post.
nupur2609
Forum Newbie
Posts: 5
Joined: Sun May 10, 2009 6:41 am

Re: PHP script is not getting executed

Post by nupur2609 »

I changed it to POST but it is still not working!! Thanks for the quick reply !!
User avatar
codex-m
Forum Newbie
Posts: 15
Joined: Sun May 10, 2009 8:08 am

Re: PHP script is not getting executed

Post 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.
Last edited by codex-m on Mon May 11, 2009 10:10 am, edited 1 time in total.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: PHP script is not getting executed

Post 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.
User avatar
codex-m
Forum Newbie
Posts: 15
Joined: Sun May 10, 2009 8:08 am

Re: PHP script is not getting executed

Post 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.
nupur2609
Forum Newbie
Posts: 5
Joined: Sun May 10, 2009 6:41 am

Re: PHP script is not getting executed

Post 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!!!!
User avatar
susrisha
Forum Contributor
Posts: 439
Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India

Re: PHP script is not getting executed

Post 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';
}
 
nupur2609
Forum Newbie
Posts: 5
Joined: Sun May 10, 2009 6:41 am

Re: PHP script is not getting executed

Post 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
Last edited by Benjamin on Tue May 12, 2009 11:29 am, edited 1 time in total.
Reason: Changed code type from text to php.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: PHP script is not getting executed

Post by onion2k »

Do other PHP scripts work?
nupur2609
Forum Newbie
Posts: 5
Joined: Sun May 10, 2009 6:41 am

Re: PHP script is not getting executed

Post by nupur2609 »

Hi,
Yes,other scripts are working.
Thanks.
Post Reply