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 Integrating PHP with Databases Querying the Database with PHP Querying the Database

sameh salah
sameh salah
4,635 Points

Have a nice day , i wonder if you have video teach me how insert data in MySQL database ?

most courses in PHP is about retrieve data from database and i can't found video about inserting data in database , kindly help me

Thanks; Sameh

1 Answer

Robert Gouveia
PLUS
Robert Gouveia
Courses Plus Student 1,714 Points

The reason for this is because it all depends if you are using PDO or Mysqli.

The sql query is the same but the way you import the data in php differs slightly.

PDO for instance is:

$query = $pdo->prepare("INSERT INTO databasetable (column1, column2, column3) VALUES (:column1, :column2, :column3)");
$query->execute(array(':column1' => $variable1, ':column2' => $variable2, ':column3' => $variable3));

And Mysqli looks like this:

$query = $conn->prepare("INSERT INTO databasetable (column1, column2, column3) VALUES (?, ?, ?)");
$query->bind_param("sss", $variable1, $variable2, $variable3);

Notice they are not a million miles apart from they are different.

For instance the Mysqli in the bind_param is also asking what type each variable is (i,d,sb). For my query I just kept it as s which means string.

I personally have not gone through these courses as they are not what I need to learn but to be honest everything you will ever need you can find on the php website:

I am pretty sure I have seen a few courses about PDO on here, maybe you need to look at them, but to be honest if you do not know how to insert into the database, it means you really need to learn the basics of PHP first.

Php Website

Hope this helps :)