PHP Script Problem: Troubleshoot the Error

  • PHP
  • Thread starter Dave Ritche
  • Start date
  • Tags
    Php
In summary: 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.
  • #1
Dave Ritche
70
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
  • #2
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 Dave Ritche
  • #3
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';
}

?>
 
  • #4
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/
 
  • #5
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 Dave Ritche
  • #6
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...
 
  • #7
Do other php scripts execute correctly? Like the simple script with just one echo command?
 
  • Like
Likes Dave Ritche
  • #8
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?
 
  • #9
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 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 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 Dave Ritche

Related to PHP Script Problem: Troubleshoot the Error

1. What is a PHP script?

A PHP script is a programming code written in the PHP language that is used to create dynamic web pages. It is typically embedded within HTML code and is executed on the server side.

2. What is a PHP script problem?

A PHP script problem is an error or issue that occurs when a PHP script is not functioning as intended. This can be due to a variety of factors, such as incorrect syntax, missing or outdated libraries, or server configuration issues.

3. How do I troubleshoot a PHP script problem?

To troubleshoot a PHP script problem, you can start by checking for any syntax errors in the code. You can also use a debugging tool or enable error reporting to identify any specific errors. It is also helpful to check the server logs for any relevant information.

4. What are some common causes of PHP script problems?

Some common causes of PHP script problems include incorrect syntax, missing or outdated libraries, server misconfiguration, and conflicts with other scripts or code on the website. It is important to carefully review the code and check for any potential conflicts or errors.

5. How can I prevent PHP script problems in the future?

To prevent PHP script problems, it is important to write clean and organized code, use the latest versions of libraries and frameworks, and regularly update your server software. It is also helpful to use a version control system to track changes and revert to previous versions if necessary.

Similar threads

  • Programming and Computer Science
Replies
6
Views
5K
  • Programming and Computer Science
Replies
8
Views
236
  • Programming and Computer Science
Replies
7
Views
5K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
4
Views
6K
  • Programming and Computer Science
Replies
5
Views
3K
Replies
2
Views
2K
  • Programming and Computer Science
Replies
6
Views
3K
Replies
1
Views
3K
  • Programming and Computer Science
Replies
15
Views
6K
Back
Top