Get checked Checkboxes value with PHP

Checkboxe­s are a versatile tool found on many we­bsites, allowing users to sele­ct their preferre­d choices among different options available­ with ease.

When you use it in your form and try to read all checked values as any other elements like –  text box, text area, radio button, etc.

echo $_POST['lang'];  // Checkbox element

you will get the last checked value.

You need to send the checkboxes value in the form of an Array when the form gets submitted then you can loop over $_POST values.

In this tutorial, I show how you can read submitted checked checkboxes values with PHP and also show how you can save it to the MySQL database.

Get checked Checkboxes value with PHP

Read more

How to Break the nested loop in PHP

The break statement in PHP is utilized to terminate the execution of the current loop or switch case statement. However, when dealing with nested loops and the intention is to exit from one or more of the outer loops, you can achieve this by providing a numeric value with the break statement.

This tutorial demonstrates the process of terminating a specified number of nested foreach, for, while, and do-while loops in PHP by utilizing the break statement.

How to Break the nested loop in PHP

Read more

Get Last insert id from MySQL Table with PHP

There are various approaches to selecting the last insert ID from the MySQL table:

  • Fetch the ID by selecting a row in descending order and storing the ID.
  • Retrieve the maximum value.
  • The query below provides the subsequent AUTO_INCREMENT value for the chosen table, useful for acquiring the last ID.
SELECT AUTO_INCREMENT FROM INFORMATION_SCHEMA.TABLES 
WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'user'

For example, if your AUTO_INCREMENT column’s last value is 8, executing the query will return 9.

In PHP there is an inbuilt method that simply returns the last insert id according to the previous insert query.

Get Last insert id from MySQL Table with PHP

Read more

Multiple files upload at once with PHP

Whether you’re creating a file-sharing platform, a social media website, or any other type of web application, the ability to upload multiple files at once can significantly improve user experience.

In PHP, it is possible to upload multiple files using a single input file element. You just need to customize your single file upload PHP code and enable your file element to select multiple files.

In this tutorial, I show you how to implement multiple files upload with validation in PHP.

Multiple files upload at once with PHP

Read more