0

Before anyone says this is a duplicate, I have checked and tried the solutions from this previously asked question. My question is different, I believe, because I don't have a separate php file - it's coded with tags in my HTML (so everything is in the same document).

Here is my PHP (database info left empty):

<?php

session_start();
    $dbhost = '****';
    $dbuser = '****';
    $dbpass = '****';
    $dbname = '****';

$connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);


$email=$_POST['email'];

$query="INSERT INTO tableName(email)
VALUES('$email')";

mysqli_free_result($result);

mysqli_close($connection);

?>

Here is my Materialize/HTML form:

<form action="/thankyou.php" method="POST">
        <p class="input-header">Enter Your Email:</p>
        <input id="email" type="email" name= "email" class="validate" required>
        <br></br>
        <input class="waves-light btn indigo lighten-2" type="submit">
        <br></br>
        <br></br>
        <br></br>

</form>

Any ideas for why it's not working? I checked my MAMP phpmyadmin database and nothing is getting added. Please let me know if you have any suggestions! Thanks!

Yikes
  • 87
  • 1
  • 11

1 Answers1

0

This will help you: As you haven't added mysqli_query and because of that it wasn't adding data. Also here I am checking whether the form is submitted as you mentioned it's a single file.

<?php
// check if form is submitted
if(!empty($_POST['email'])) {
    $dbhost = '****';
    $dbuser = '****';
    $dbpass = '****';
    $dbname = '****';

    $connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
    // this line prevents sql injection
    $email = mysqli_real_escape_string($connection, $_POST['email']);

    $query="INSERT INTO tableName(email) VALUES('$email')";
    // this statement runs your query and actually adds data to mysql
    if (mysqli_query($connection, $query)) {
        echo "New record created successfully";
    } else {
        echo "Error: " . $sql . "<br>" . mysqli_error($connection);
    }
    mysqli_close($connection);
}
?>
pravindot17
  • 1,199
  • 1
  • 15
  • 32