Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial![](https://ecs-static.teamtreehouse.com/assets/views/marketing/shared/community-banner-white-47072046c51352fe6a69f5e691ff5700b28bb11d45197d7bdf066d9ea3f72d0c.webp)
![Pirvan Marian](https://secure.gravatar.com/avatar/50ab654c1248b3052137300a3a11325d?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Pirvan Marian
14,609 PointsHow i can insert data to mysql from a form ?
I see many tutorial here about php and mysql but i wanna know... how can i insert null data from a form not a script. For exemple i have box : Firstname and Lastname and i wanna insert what name i need it... I find few tutorial whith html form and php but not work for me ... Thank you!
3 Answers
![Ricky Catron](https://uploads.teamtreehouse.com/production/profile-photos/108204/micro_1010818_679270375218_643096572_n.jpg)
Ricky Catron
13,023 PointsFollowing through this course and studying MySQL should get you ready for creating dynamic database based website in PHP.
There are lots of simple easy solutions but that is one of the problems with PHP. There is so much old deprecated code with errors and vulnerabilities which make simple solutions difficult to trust. I would recommend taking these courses and studying them to make sure you have a well done secure connection.
Goodluck! --Ricky
![Pirvan Marian](https://secure.gravatar.com/avatar/50ab654c1248b3052137300a3a11325d?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Pirvan Marian
14,609 Pointsit;s useless really... i know i can i insert whith line like :
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', 'john@example.com')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
But i need insert values from a box not from a file...
![thomascawthorn](https://uploads.teamtreehouse.com/production/profile-photos/654142/micro_11350743_1668650660030200_2146547820_n.jpg)
thomascawthorn
22,986 PointsHere is the most recent php + databases course - but I think it uses sqlite not mysql, so maybe not useful.
Now you've got your database connection set up, you can start to work on the form side.
This course will take your through that particular aspect via the contact form tutorial.
Although the contact form doesn't connect to the database, it does tell you how to take form values and use them for functionality.
You're going to want to watch "adding a contact form" badge, but the whole course would be pretty helpful ;)
![Ricky Catron](https://uploads.teamtreehouse.com/production/profile-photos/108204/micro_1010818_679270375218_643096572_n.jpg)
Ricky Catron
13,023 PointsThe second course Tom Cawthorn mentions is exactly what you need. It modify it to work for you simply use the variables you get from the form and add them to this piece of code:
$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', 'john@example.com')";
Goodluck! --Ricky