How to store HTML form input in MySQL Database using PHP



Share this page:



  

<?php $servername = "{ENDPOINT-TO-MYSQL-DATABASE}"; $username = "{USERNAME-FOR-DATABASE}"; $password = "{PASSWORD-FOR-DATABASE}"; $dbname = "{DATABASE-NAME}"; $vemail = $_POST['{ID-OF-HTML-FORM-INPUT(email)}']; // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } echo "Connected successfully"; $sql = "INSERT INTO subs (firstName, email) VALUES ('', '{$vemail}')"; if ($conn->query($sql) === TRUE) { echo "New record created successfully"; } else { echo "Error: " . $sql . "<br>" . $conn->error; } header("Location: https://computersciencex.com/indexSuccess.html", true, 301); $conn->close(); ?>
  • Replace the & character with "& a m p ;" WITH NO SPACES"&"
  • Replace the < character with "& l t ;" WITH NO SPACES"<"
  • Replace the > character with "& g t ;" WITH NO SPACES ">"
  • Optionally surround your HTML sample with <pre> and/or <code> tags.

  • Output:

    Sign up for our email subscription

    We'll never share your email with anyone else.

    This is a very simple HTML form that gets the user to enter an email address and then it will 'POST' that data to the 'action_page.php' file so that we can use/manipulate the data that the user has provided how we want using PHP.

    How to gather user input with a HTML email subscription form and store that data in a MySQL Database using PHH


    Page Sections:

    1. What are the 'src' and 'public' files used for?
    2. Javscript example
    3. SCSS example

    Share this page:


    
      
    
    <article class="card card-outline mb-4"> <div class="card-body"> <h4 class="card-title">Sign up for our email subscription</h4> <form method="POST" action="action_page.php"> <div class="mb-3"> <label for="email">Email:</label> <input type="email" class="form-control" id="email" placeholder="Enter email" name="email" data-ddg-inputtype="identities.emailAddress" data-ddg-autofill="true" style="background-size: auto 24px !important; background-position: right center !important; background-repeat: no-repeat !important; background-origin: content-box !important; !important; transition: background !important;"> <div id="emailHelp" class="form-text">We'll never share your email with anyone else.</div> </div> <button type="submit" class="btn btn-primary">Submit</button> </form> </div> </article>

    Output:

    Sign up for our email subscription

    We'll never share your email with anyone else.

    This is a very simple HTML form that gets the user to enter an email address and then it will 'POST' that data to the 'action_page.php' file so that we can use/manipulate the data that the user has provided how we want using PHP.