0

I am taking simple user data from HTML page and inserting it into a database. But it is not working. When I click the submit button it redirect to submit.php page and shows the submit code. Where is the error? Here is my code.

HTML code:

<!DOCTYPE html>
<html>
<head>
    <title>
        Databse connection
    </title>
</head>
<body>
    <form action="submit.php" method="POST">
        <table>
            <tr>
                <td>
                    Name:
                </td>
                <td>
                    <input type="text" name="username">
                </td>
            </tr>
            <tr>
                <td>
                    <input type="submit" name="submit" value="submit">
                </td>
            </tr>
        </table>
    </form>
</body>
</html>

And PHP code:

<?php

    $servername = "localhost";
    $username = "username";
    $password = "password";

    // Create connection
    $conn = new mysqli($servername, $username, $password);

    // Create database
    $sql = "CREATE DATABASE myDB";
    $conn->query($sql);

    // sql to create table
    $sql = "CREATE TABLE usertable (id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, username VARCHAR(30) NOT NULL)";
    $conn->query($sql);

    // insert data
    // static data for test purpose
    $sql = "INSERT INTO usertable (username) VALUES ("firstuser")";
    $conn->query($sql);

    // close connection
    $conn->close();
?>
Patrick Q
  • 6,374
  • 2
  • 24
  • 34
Shah Alam
  • 23
  • 8
  • What IS the error? Turn on error reporting and report back : add to top of your script: `ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);` – IncredibleHat Jul 19 '18 at 17:09
  • Hint: `$sql = "INSERT INTO usertable (username) VALUES ("firstuser")";` – IncredibleHat Jul 19 '18 at 17:09
  • 2
    It shows the code? Sounds like you don't actually have PHP running. – Patrick Q Jul 19 '18 at 17:10
  • ^ or maybe that :) ... I didn't think that far up the chain lol. – IncredibleHat Jul 19 '18 at 17:10
  • When a user fills a form and clicks submit, the `` values are sent to the `action` page. The action page, here your PHP code, can access the text typed by the user via the `$_POST` variable. In your code, you set $username to "username", so the typed value will never be put in the database. Find a tutorial on how to process `
    ` using PHP to help. And you create a new database and table every time someone submits?
    – Nic3500 Jul 19 '18 at 17:12
  • @Nic3500 for just learning purpose, created database and table each time and useing static data just avoiding complexity. – Shah Alam Jul 19 '18 at 17:23
  • You are using double quotes inside double quotes here:$sql = "INSERT INTO usertable (username) VALUES ("firstuser")"; –  Jul 19 '18 at 17:29
  • @PatrickQ, when I go http://localhost/dashboard/phpinfo.php it shows php version. So I think php is running – Shah Alam Jul 19 '18 at 17:32
  • Your question says "redirect to submit.php page and _shows the submit code_". Please explain exactly what you mean by this. – Patrick Q Jul 19 '18 at 17:33
  • @PatrickQ , it shows the code as plain text. – Shah Alam Jul 19 '18 at 17:38
  • Then your form is submitting to an environment where PHP is _not_ running. – Patrick Q Jul 19 '18 at 17:41
  • @patricQ, I think I am doing wrong which @ shmeeps mentioned in his answer in another simillar question. He said, "Make sure you are accessing your file over your webserver using an URL like http://localhost/file.php not via local file access file://localhost/www/file.php" I don't know how to solve this. – Shah Alam Jul 19 '18 at 17:50
  • How are you opening/running your HTML file? – Patrick Q Jul 19 '18 at 18:39

0 Answers0