-1

When I click a button to insert the data into the database by implementation of PHP code , but the problem is that it opens a new tab and shows PHP codes

html code

    <html>
        <body>
        <form action="InsertStudent.php" method="post">
        Student Id: <input type="text" name="SId" />
        Student Name:<input type="text" name="SName" />
        Marks: <input type="text" name="marks" />
        <input type="submit"  value= "Insert"/>
        </form>
        </body>
        </html>

PHP code

    <?php
    $con = mysql_connect("localhost","root","1234");
    if (!$con)
      {
      die('Could not connect:'.mysql_error());
      }
    mysql_select_db("my_db",$con);

    mysql_query("INSERT INTO Student(StudId,StudName,Marks)
    VALUES ('9876543', 'Ahmed', '90')");

    mysql_query("INSERT INTO Student(StudId,StudName,Marks)
    VALUES ('12344556', 'Mohammed', '95')");

    mysql_close($con);
    ?>
brombeer
  • 8,716
  • 5
  • 21
  • 27
  • 2
    Possible duplicate of [this](https://stackoverflow.com/questions/5121495/php-code-is-not-being-executed-instead-code-shows-on-the-page) ... and you **really** shouldn't be using the [mysql_*](https://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) extension, it's been obsolete for years. – CD001 Mar 31 '20 at 09:38

1 Answers1

-2

every php script must be run on server. In your case you are not using server. You should install xamp or wamp server and then run your file on localhost.

step 1: download and install xamp from https://www.apachefriends.org/

step 2: copy or move your folder which contain the html and php file to c:\xamp\htdocs directory step 3: after installation open the control panel and start apache server

step 4: open browser and type url localhost/your_folder_name

note: the folder name should not contain spaces or '-'. Name your html file as index.html