Erasing Data with Surgical Precision The last of the operations is the delete operation. Just like with our update functionality, there is no delete method. We can submit a hidden method with delete as the value. We can use post and specify a value, but we’re building up for Laravel. Laravel accepts various requests and can process them easily. Here’s a list from their documentation. https://laravel.com/docs/9.x/controllers#actions-handled-by-resource-controller As you can see, if we wanted to delete a photo, we need to provide the {photo} resource. In our case, we’ll need to provide the author’s id. Previous Code Review Make sure that you’re up to date on the previous code. Author.php DB.php UpdateAuthorEmail.php
Tag: PHP Programming
Revamping Data with Precision The next logical step in our MySQL progression is updating an existing resource. We’re going to introduce the differences between PUT and PATCH and put it to rest once and for all. It really is a simple concept and I’m not sure why people find it so confusing. https://blog.devgenius.io/php-p88-mysql-select-d7a633e736b PUT vs PATCH Let’s take a look at our authors table. It contains the following fields: id first_name last_name email If we update a resource with the PUT request, we need to send all of the fields. That means that if we update the email, we need to send the id, first_name, last_name, and email. If
The Art of Data Retrieval We’ve gone through the insertion process and have a few records inserted inside of our authors table. Now what? A common task is to retrieve those entries and display them on the screen. That’s where the SELECT statement comes through. https://dinocajic.medium.com/php-p87-mysql-insert-multiple-records-with-prepared-statements-c5c7bcfb42a3 A Simple Select Statement For the selection to work, we need to: Connect to the database Pass our select query Retrieve the results, which is just the rows of data. Loop through each of the results and extract data for each row that was returned. Close the connection Straight forward. Let’s isolate the selection process first. It
MySQL Prepared Statements for Bulk Data Inserts You may notice a small buildup between articles, and that’s intentional. No reason to overwhelm or dilute the topic. In this article, we’ll answer one additional question related to inserting data with prepared statements: do you need to keep binding variables for each set of data that you’re inserting? Let’s just spoil that response: you don’t. Setup the binding once, change the variables, and execute. It may look a little strange, but as long as you know the flow, it will work. https://blog.devgenius.io/php-p86-mysql-insert-with-prepared-statements-57f37daeb109 Recap <?php namespace Authors; require_once(“DB.php”); use Database\DB; class Author {
MySQL’s Shielded Data Transformation There’s one big flaw in our previous MySQL insert statement: it’s open to SQL Injection Attacks. Prepared statements virtually eliminate that concern. You tell the SQL server what you’re intending to do and then you give it the data. For example, you can tell the server that you’re planning on inserting data. If you send a delete statement disguised in your insert statement, that doesn’t make sense and will not have the same kind of effect that it would without prepared statements. https://blog.devgenius.io/php-p85-mysql-insert-51a9e3bfedd3 https://medium.com/geekculture/cybersecurity-p9-sql-injections-471de647e8d7 Recap Our Author class had an insert statement that accepted data and inserted it into the authors table. <?php namespace
Infusing Tables with Fresh Data Energy We have our database connection, our database, and our table. We just need to start adding some data to it. This is a basic functionality that you’ll need to do constantly. Each time a form is submitted, where’s that data going to go? Will it be emailed or will it be stored in a database? Let’s do this the right way: the object oriented way. Our assignment for this article will be to connect to a database and insert an author into our authors table. If you need to review how we got to this point,
MySQL tables neatly store data in rows and columns In the previous article, we covered how to create a database and establish a connection to it. It’s time to go a step deeper now. It’s time to create some tables. If you remember the previous analogy, the database can be thought of as an Excel Workbook and the tables can be thought of as sheets within the Workbook. https://blog.devgenius.io/php-p83-mysql-db-connection-8a34c4056863 Tables are part art part science. Architects must decide how far they want to normalize a database without losing readability. We’ll cover the basics of tables in this article and will
MySQL Connection: Building Bridges to Data Dreams The first step to reading or inserting data is to establish a connection to your server/database. It’s a fairly straightforward process and it’s something that doesn’t change. The steps are always the same. Once you connect to it once, you’ll be able to reuse the code each time. The only thing that you’ll need to modify is your server, username, password, and database name. Creating a Database I’m assuming that you’re following along with the series. We have a Docker container and that container has the traditional LAMP setup (Linux, Apache, MySQL, and
MySQL is a popular open-source database system You get to a certain point in most programming languages and you ask yourself, “how do I save data?” I looked whether to tackle sessions and cookies next, but realized that it would make more sense to tackle data-persistence first before moving into those topics. What is MySQL? It is an Oracle distributed database system that runs on a server. It uses SQL syntax and is paired with PHP to the point that it’s not common to learn one without the other. If you’re new to programming, and have been following along this
Transforming a script into a class enhances code modularity and reusability We covered file uploads in the last few articles, and purposefully strayed away from object oriented programming. Why? Because most of the time that you find a solution online to a problem like this, it won’t be laid out nicely for you. It’s going to be just a blob of code and not going to fit your architecture perfectly. I remember when I was a junior developer, that used to give me some anxiety. “Couldn’t they just have created this as a class instead of this script,” I found