Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Php script problem

  1. Feb 7, 2016 #1
    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. jcsd
  3. Feb 7, 2016 #2

    Samy_A

    User Avatar
    Science Advisor
    Homework Helper

    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.
     
  4. Feb 7, 2016 #3
    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';
    }

    ?>
     
     
  5. Feb 7, 2016 #4
    And here is the html:
    Code (Text):

    <!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/
     
  6. Feb 7, 2016 #5

    Samy_A

    User Avatar
    Science Advisor
    Homework Helper

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

    Samy_A

    User Avatar
    Science Advisor
    Homework Helper

    Do other php scripts execute correctly? Like the simple script with just one echo command?
     
  9. Feb 7, 2016 #8
    yeah thanks it's working now i.e it says connected to the database but why doesn't it store data?
     
  10. Feb 7, 2016 #9

    Samy_A

    User Avatar
    Science Advisor
    Homework Helper

    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.
     
  11. Feb 7, 2016 #10
    Thank you very much Samy_A.My database and script is working very fine..
     
  12. Feb 8, 2016 #11

    Samy_A

    User Avatar
    Science Advisor
    Homework Helper

    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.
     
  13. Feb 11, 2016 #12
    Thank you but i really don't know what is PDO..Is it a plug-in?
     
  14. Feb 11, 2016 #13

    Samy_A

    User Avatar
    Science Advisor
    Homework Helper

    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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Php script problem
  1. Understanding PHP (Replies: 4)

Loading...