Absolute beginner needs help setting up HTML form to database connection

Avatar
  • Answered
Hi,

I've set up a database table in phpMyAdmin, and have copied and pasted the code below (from a website that I found) into my own HTML file.

I'd like to learn what I need to do to connect this HTML to the "username_myTable" database table that I set up.

The database table has the following columns: firstname, lastname, email, phone, and comment.

My ultimate goal is to be able to collect visitor's information from my website so that I can do email marketing campaigns using the data that would get stored in the database.

Let me know if I should include more information. Any help would be greatly appreciated.










You will receive our response
with a few minutes



first name*





last name*





email*





phone












Avatar
JacobIMH
Hello alexinmo28, and thank you for your question. Unfortunately I'm not seeing any code that your pasted so I'm unable to directly follow along with what you're trying to accomplish with the code. We do have an article on using PHP to insert data into a MySQL database that you might find handy. It sounds like you'd want to setup <input> tags for your various fields in your HTML form first. As an example lets say you used something like:

<form action="YourPHPScript.php" method="POST"> First name: <input type="text" name="fname"><br> Last name: <input type="text" name="lname"><br> E-mail: <input type="text" name="email"><br> Phone: <input type="text" name="phone"><br> Comment: <input type="text" name="comment"><br> <input type="submit" value="Submit">

Then in your PHP script you'd want to grab these values that are getting sent via a POST request, with something like:

$firstName = htmlspecialchars($_POST['fname']); $lastName = htmlspecialchars($_POST['lname']); $email = htmlspecialchars($_POST['email']); $phone = htmlspecialchars($_POST['phone']) $comment = htmlspecialchars($_POST['comment']);

Then build up a SQL query to insert the data into your database in a table called comments with something like:

$query = "INSERT INTO `username_myTable`.`comments` (`firstname`, `lastname`, `email`, `phone`, `comment`) VALUES ('$firstName', '$lastName', '$email', '$phone', '$comment');";

Also if your end goal is for email marketing campaigns, you might be interested in skipping doing this manually into your own database. We have a phpList education channel that I'm actually in the process of putting the final touches on. For a quick glance at what I'm talking about, the relevant section of that channel for you would be how to create a subscribe page in phpList. Basically phpList is about the most popular open source mailing list manager on the web, and it supports setting up subscription pages that users can come to on your site. You can specify certain attributes that they must include when signing up for a mailing list, such as their name and phone number, and then it even handles sending them a confirmation email to confirm their interest in your mailings (referred to as opt-in which is required by our terms of service). Hope some of this information gets you pointed in the right direction. If you have any further questions at all please let us know! - Jacob