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

PHP

How 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
Ricky Catron
13,023 Points

Following 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

it;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...

Here 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
Ricky Catron
13,023 Points

The 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