# Php script problem

1. Feb 7, 2016

### Dave Ritche

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.

2. Feb 7, 2016

### Samy_A

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.

3. Feb 7, 2016

### Dave Ritche

Code (Text):

<?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. Feb 7, 2016

### Dave Ritche

And here is the html:
Code (Text):

<!DOCTYPE HTML>
<html lang="en-US">
<meta charset="UTF-8">
<title>labels</title>
<link rel="stylesheet" type="text/css" href="label.css" media="all" />
<body>
<h1>REGISTER TO GPGC</h1>
<div class="form">
<form action="process.php" method="post">

<select name="gender" id="gender">

<option value="female" name="gender">FEMALE</option>
<option value="male" name="gender">MALE</option>
</select><br/>

<input type="submit" value="SUBMIT MY INFO" id="sub"/>

</form>
</div>
</body>
</html>

[/code/

5. Feb 7, 2016

### Samy_A

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: Feb 7, 2016
6. Feb 7, 2016

### Dave Ritche

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. Feb 7, 2016

### Samy_A

Do other php scripts execute correctly? Like the simple script with just one echo command?

8. Feb 7, 2016

### Dave Ritche

yeah thanks it's working now i.e it says connected to the database but why doesn't it store data?

9. Feb 7, 2016

### Samy_A

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.

10. Feb 7, 2016

### Dave Ritche

Thank you very much Samy_A.My database and script is working very fine..

11. Feb 8, 2016

### Samy_A

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.

12. Feb 11, 2016

### Dave Ritche

Thank you but i really don't know what is PDO..Is it a plug-in?

13. Feb 11, 2016

### Samy_A

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:

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.