Blog Details

img
Development

How use PHP with MySQL insert records example

Spoke Right / 10 Nov, 2023

PHP uses mysqli_query function to insert records in table. This function takes three parameters and returns TRUE on success or FALSE on failure.

Syntax

mysqli_query ( mysqli $link, string $query, int $resultmode = MYSQLI_STORE_RESULT ) : mixed


Sr.No.Parameter & Description
1

$link

Required - A link identifier returned by mysqli_connect() or mysqli_init().

2

$query

Required - SQL query to drop a table.

2

$resultmode

Optional - Either the constant MYSQLI_USE_RESULT or MYSQLI_STORE_RESULT depending on the desired behavior. By default, MYSQLI_STORE_RESULT is used.

Example

This example will take three parameters from the user and will insert them into the MySQL table −

Copy and paste the following example as mysql_example.php −


Add New Record in MySQL Database

Tutorial Title
Tutorial Author
Submission Date [ yyyy-mm-dd ]



Output

Access the mysql_example.php deployed on apache web server, enter details and verify the output on submitting the form.

Entered data successfully

While doing a data insert, it is best to use the function get_magic_quotes_gpc() to check if the current configuration for magic quote is set or not. If this function returns false, then use the function addslashes() to add slashes before the quotes.

You can put many validations around to check if the entered data is correct or not and can take the appropriate action.

0 comments