Friday 17 March 2023

code on data inserted in database in php

 Sure, here's an example of inserting data into a database using PHP:

First, you will need to establish a connection to your database using the mysqli_connect() function. Here's an example of connecting to a database:

php
<?php $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "myDB"; // Create connection $conn = mysqli_connect($servername, $username, $password, $dbname); // Check connection if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } ?>

Once you have established a connection to your database, you can insert data into a table using the mysqli_query() function. Here's an example of inserting data into a "users" table:

php
<?php // Insert data into table $sql = "INSERT INTO users (firstname, lastname, email) VALUES ('John', 'Doe', 'johndoe@example.com')"; if (mysqli_query($conn, $sql)) { echo "New record created successfully"; } else { echo "Error: " . $sql . "<br>" . mysqli_error($conn); } // Close connection mysqli_close($conn); ?>

In this example, we are inserting the values 'John', 'Doe', and 'johndoe@example.com' into the "users" table. The mysqli_query() function will return true if the query was successful, and false if there was an error.

Note that this is just a basic example, and you will need to modify the code to match the structure of your own database and tables. Additionally, you should always sanitize any user input before inserting it into a database to prevent SQL injection attacks.