Simple CRUD with PHP & MySQLi
It’s all well & good being able to get info from a database, but what you really want to do it add, edit and remove it.
To save you tons of time looking for the code (like I did), here’s some snippets that should make life a lit easier when working with MySQL.
I’m also going to be using the more up-to-date MySQLi methods. They’re just better all round.
Note that you’ll need to understand how to make a database and add columns in them with a tool like phpMyAdmin or Sequel Pro (Mac).
I’ve added no validation either, as it varies each time it’s needed.
Connection to DB
<?php $db_server = "localhost"; // This value can change from host to host. $db_user = "root"; $db_pass = "root"; $db_name = "crud"; $mysqli = new MySQLi($db_server, $db_user, $db_pass, $db_name) or die(mysqli_error()); ?>
Create
This snippet just adds one row, with an ID that should auto increment. Adding the ‘id’ bit isn’t strictly needed, but it makes it easier to scam the code, and know it’s there. The ‘$name’ is assuming the info it’ll add to the DB is from a variable, just as you’d expect, $name = $_POST['inputname']; or just plain text $name = "Lorem Ipsum";
<?php require('_database.php'); $mysqli->query("INSERT INTO people (id, name) VALUES ('','$name')") or die ($mysqli_error($mysqli)); ?>
Read
This is the one you’ll no doubt use a million times. Getting info from a DB is probably the main reason you even want to work with a database anyway! Here’s how.
<?php require('_database.php'); $name_result = $mysqli->query("SELECT * FROM people") or die($mysqli_error($mysqli)); while($name_row = $name_result->fetch_object()) { echo '<p>'.$name_row->id.' - '.$name_row->name.'</p>'; // Looks like: <p>1 - Lorem Ipsum</p> } ?>
Using the method above, you can either get one row, or an infinite number. Very useful.
Update
This one is useful. Simply select the columns you want to edit and set their new value, but only where the row ID is whatever the $id value is. Be sure to not forget this. I’ve heard stories or people forgetting to and essentially loosing all their info where they replaced the column value on every row. A huge error when the DB contains 5 million rows!
<?php require('_database.php'); $mysqli->query("UPDATE people SET name='$name' WHERE id='$id'") or die($mysqli_error($mysqli)); ?>
Delete
Like the rest, this one is pretty easy. Be sure to select the row to delete, as you could accidently delete the whole table! Have you seen when stupid people add DROP TABLE people in tutorial tests? It’s like that, but your own fault.
<?php require('_database.php'); $mysqli->query("DELETE FROM people WHERE id='$id'") or die($mysqli_error($mysqli)); ?>
Conclusion
Well, hopefully you can now create, read, update and delete rows from a database. I’d love to see what you’ve made using these snippets, so leave a comment and show us all!
Tune in next week where I’ll show you simple SQL queries, so you can choose what data you get from the database.

Nice tutorial, I need to start using MySQLi
Thanks for your sharer!
er,what about snippets include “Prepare Statement”?
Hi, how do you cache queries in mysqli??
It all depends what you mean.
If you mean cacheing the SQL query, you can’t.
If you mean cacheing the results, there’s many ways. It’s a completely different kettle of fish however, and nothing to do with MySQLi. MySQLi is just a newer and ‘improved’ (that’s what the ‘i’ means) method of connecting to a MySQL database.
You have prepared a very simple but nice tutorial for switching mysql to mysqli in php. It’s good for its purpose, but i suggest you update this tutorial to include the prepared statements (bind_param and bind_result) just so people because it’s really what mysqli is for.
Nice tutorial though.