PHP Script Problem: Troubleshoot the Error

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

Discussion Overview

The discussion revolves around troubleshooting a PHP script that is intended to process form data submitted via POST method. Participants explore potential errors in the script and the HTML form, focusing on database connectivity and data insertion issues.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Exploratory

Main Points Raised

  • One participant describes a problem where the PHP script's contents appear in the browser instead of executing, suggesting a possible issue with the server configuration.
  • Another participant recommends checking if PHP scripts execute correctly by testing a simple echo command.
  • Concerns are raised about the SQL statement in the script, specifically the use of an incorrect variable for the password.
  • Participants suggest that the database connection is established but question why data is not being stored, prompting checks on table and field names.
  • One participant mentions the deprecated nature of the mysql_connect function and suggests using PDO for database interactions instead, highlighting its advantages.
  • There is a discussion about the differences between mysql_connect and PDO, with one participant expressing unfamiliarity with PDO.

Areas of Agreement / Disagreement

Participants express differing views on the best practices for database connections, with some advocating for PDO while others continue to use mysql_connect. The discussion remains unresolved regarding the specific reasons for data not being stored in the database.

Contextual Notes

There are unresolved issues related to the correctness of the SQL query and the database schema, as well as the implications of using deprecated functions in PHP.

Who May Find This Useful

Individuals working with PHP and MySQL, particularly those troubleshooting form handling and database connectivity issues.

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
7K
  • · Replies 7 ·
Replies
7
Views
6K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 5 ·
Replies
5
Views
4K
Replies
6
Views
4K
  • · 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