0% found this document useful (0 votes)
47 views

An Example of Code To Insert Data To Database Using PHP MYSQL

This document provides an example PHP code to insert data into a MySQL database table. The code connects to the database, defines sample data to insert, constructs an SQL INSERT query to add the data to a specified table, executes the query, and displays success or error messages. Users need to replace placeholders in the code with their own database credentials and adjust variables and columns as needed.

Uploaded by

lahimpagnahy
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

An Example of Code To Insert Data To Database Using PHP MYSQL

This document provides an example PHP code to insert data into a MySQL database table. The code connects to the database, defines sample data to insert, constructs an SQL INSERT query to add the data to a specified table, executes the query, and displays success or error messages. Users need to replace placeholders in the code with their own database credentials and adjust variables and columns as needed.

Uploaded by

lahimpagnahy
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

An example of how you can insert data into a MySQL database using PHP:

```php

<?php

// Replace these with your actual database connection details

$servername = "localhost";

$username = "your_username";

$password = "your_password";

$dbname = "your_database";

// Create connection

$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection

if ($conn->connect_error) {

die("Connection failed: " . $conn->connect_error);

// Sample data to be inserted

$data1 = "John Doe";

$data2 = "john@example.com";

$data3 = "New York";

// SQL query to insert data into the database

$sql = "INSERT INTO your_table_name (column1, column2, column3) VALUES ('$data1', '$data2',
'$data3')";
if ($conn->query($sql) === TRUE) {

echo "New record created successfully";

} else {

echo "Error: " . $sql . "<br>" . $conn->error;

$conn->close();

?>

```

Replace the placeholders (`your_username`, `your_password`, `your_database`, `your_table_name`)


with your actual database information. Adjust the `$data1`, `$data2`, `$data3` variables and the columns
in the SQL query according to the data you want to insert.

This PHP code connects to a MySQL database and executes an INSERT query to add data to the specified
table. If the insertion is successful, it will display a success message; otherwise, it will display an error
message with details.

You might also like