PHP Script Problem: Troubleshoot the Error

  • Context: PHP 
  • Thread starter Thread starter Dave Ritche
  • Start date Start date
  • Tags Tags
    Php
Click For Summary
SUMMARY

The forum discussion centers on troubleshooting a PHP script that fails to process form data correctly. The user reports that upon submitting a form, the script's code is displayed instead of executing. Key issues identified include the use of an incorrect variable name in the SQL query and improper handling of the mysql_query function. The solution involves correcting these errors and transitioning from the deprecated mysql_connect to PDO for database interactions, which enhances security and flexibility.

PREREQUISITES
  • Basic understanding of PHP scripting
  • Familiarity with MySQL database management
  • Knowledge of HTML forms and POST method
  • Awareness of deprecated PHP functions, specifically mysql_connect
NEXT STEPS
  • Learn how to implement PDO for MySQL database interactions
  • Explore prepared statements in PDO for enhanced security
  • Review error handling techniques in PHP to troubleshoot scripts effectively
  • Study best practices for form validation and sanitization in PHP
USEFUL FOR

Web developers, PHP programmers, and database administrators looking to improve their skills in PHP scripting and database connectivity.

Dave Ritche
Messages
70
Reaction score
6
Hello everyone!
I have designed a page set all it's input to the php Post variable.The form's method is set to POST and action to
process.php which is the script i created.Both the script and the page are in htsocs directory.when i submit the form,the contents of the script simply appear in the browser window.what could be the possible error?why the script is not working?
my dbname is html,host is localhost and password is root.
 
Technology news on Phys.org
Dave Ritche said:
Hello everyone!
I have designed a page set all it's input to the php Post variable.The form's method is set to POST and action to
process.php which is the script i created.Both the script and the page are in htsocs directory.when i submit the form,the contents of the script simply appear in the browser window.what could be the possible error?why the script is not working?
my dbname is html,host is localhost and password is root.
Difficult to answer without seeing the HTML and php code.

I assume you have already checked that your XAMP executes php scripts. If not, try a simple php script with only the instruction:
PHP:
<?php
echo 'Yes, the server is working fine!';
and see it that executes correctly.
 
  • Like
Likes   Reactions: Dave Ritche
Samy_A said:
Difficult to answer without seeing the HTML and php code.

I assume you have already checked that your XAMP executes php scripts. If not, try a simple php script with only the instruction:
PHP:
<?php
echo 'Yes, the server is working fine!';
and see it that executes correctly.
Code:
<?php
$host ='localhost';
$user ='root';
$pass ='';
$dbname ='html';
$tabname ="users";
$conn =mysql_connect('localhost','root','');

$dbselect =mysql_select_db($dbname,$conn);
if(!$dbselect){
    echo "can't connect to the database";
}
/*main*/
$username =$_POST["name"];
$email =$_POST["email"];
$class =$_POST["class"];
$roll =$_POST["roll"];
$gender =$_POST["gender"];
$pass =$_POST["password"];
$query ="
INSERT into users(name,email,class,roll,gender,password) VALUES('".$username."','".$email."','".$class."','".$roll."','".$gender."','".$password."');

";
mysql_query($query);
if(mysql_query){
    echo'record saved successfully';
}

?>
 
And here is the html:
Code:
<!DOCTYPE HTML>
<html lang="en-US">
<head>
    <meta charset="UTF-8">
    <title>labels</title>
    <link rel="stylesheet" type="text/css" href="label.css" media="all" />
</head>
<body>
<h1>REGISTER TO GPGC</h1>
  <div class="form">
  <h3>PLEASE ENTER YOUR INFORMATION BELOW</h3>
    <form action="process.php" method="post">
     <label for="name">Please enter your name</label>
    <input type="text" name="name" id="name" placeholder="please enter your name"/><br/>
    <label for="email">Please enter your email</label>
    <input type="text" name="email" id="email"placeholder="Please enter your email here" /><br/>
   
    <label for="email">Please enter your class</label>
    <input type="text" name="class" id="class" placeholder="Please enter your class here.."/><br/>
   
    <label for="email">Please enter your roll no.</label>
    <input type="text" name="roll" id="roll" placeholder="Please enter your roll here..." />      <br/>                    
   
    <label for="gender">PLEASE SELECTYOUR GENDER</label>
    <select name="gender" id="gender">
   
    <option value="female" name="gender">FEMALE</option>
    <option value="male" name="gender">MALE</option>
    </select><br/>
    <label for="password">Please enter your password</label>

    <input type="password" name="password" id="password" placeholder="Please enter a secure password here.."/><br/>
   
    <input type="submit" value="SUBMIT MY INFO" id="sub"/>
   
    </form>
    </div>
</body>
</html>

[/code/
 
First error I get is in the sql statement. You use the variable $password instead of the variable $pass you set before.

Second error is here:
PHP:
mysql_query($query);
if(mysql_query){
     echo'record saved successfully';
}
Should be something like:
PHP:
$l=mysql_query($query);
if($l){
     echo'record saved successfully';
}

With these changes the code works for me, the records are added to the database.
 
Last edited:
  • Like
Likes   Reactions: Dave Ritche
Samy_A said:
First error I get is in the sql statement. You use the variable $password instead of the variable $pass you set before.

Second error is here:
PHP:
mysql_query($query);
if(mysql_query){
     echo'record saved successfully';
}
Should be something like:
PHP:
$l=mysql_query($query);
if($l){
     echo'record saved successfully';
}

With these changes the code works for me, the records are added to the database.
Thank you very much but the problem still exists..
As i press submit,the script simply appears ..really can't figure out what's the problem...
 
Do other php scripts execute correctly? Like the simple script with just one echo command?
 
  • Like
Likes   Reactions: Dave Ritche
Samy_A said:
Do other php scripts execute correctly? Like the simple script with just one echo command?
yeah thanks it's working now i.e it says connected to the database but why doesn't it store data?
 
Dave Ritche said:
yeah thanks it's working now i.e it says connected to the database but why doesn't it store data?
Then it could be something with the table. Check that the table name and the field names are the same in the database and in the php script.
 
  • Like
Likes   Reactions: Dave Ritche
  • #10
Samy_A said:
Then it could be something with the table. Check that the table name and the field names are the same in the database and in the php script.
Thank you very much Samy_A.My database and script is working very fine..
 
  • #11
Dave Ritche said:
Thank you very much Samy_A.My database and script is working very fine..
Glad it worked out. Just to be clear, that the code works is only the beginning of the adventure.

I know that this code is just an exercise and not intended as a real website, but nevertheless I would like to make one remark.
You use mysql_connect to connect to the database. That's a rather old and now deprecated extension. Since you are starting with php anyway, better learn and use PDO to access your MySQL databases.
 
  • Like
Likes   Reactions: Dave Ritche
  • #12
Thank you but i really don't know what is PDO..Is it a plug-in?
 
  • #13
Dave Ritche said:
Thank you but i really don't know what is PDO..Is it a plug-in?
It is an "extension" for php. For all practical purposes you can consider it a part of php. The link I gave is to the official php documentation.

I use WampServer, and in WampServer you can chose which extensions you want to use:
phppdo.jpg

I can't remember whether the php_pdo_mysql was checked by default or not.

Using PDO instead of mysql_connect is not more difficult (or less difficult). The syntax is somewhat different. Many older books and online examples use mysql_connect as a matter of fact, but as it is now deprecated I think someone new to php can better start with PDO. If you want to use PDO, give "prepared statements" a look.
One of the advantages of PDO is that you can use it with different databases. You can see that I also checked the php_pdo_odbc extension, allowing me to access our older databases with the same methods and syntax I use for MySQL. (No idea why php_pdo_sqlite is also checked, never used that one.)

But that is just my opinion, of course. I'm sure other users of php and MySQL may have equally valid reasons to use another extension.
 
  • Like
Likes   Reactions: Dave Ritche

Similar threads

  • · Replies 6 ·
Replies
6
Views
6K
  • · Replies 7 ·
Replies
7
Views
6K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 5 ·
Replies
5
Views
4K
Replies
6
Views
3K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 2 ·
Replies
2
Views
9K
  • · Replies 15 ·
Replies
15
Views
7K
Replies
2
Views
2K
Replies
1
Views
4K