• Thu, Nov 2024

How To Insert Data In Mysql Using PHP

How To Insert Data In Mysql Using  PHP

How To Insert Data In Mysql Using PHP

Inserting data into a MySQL database using PHP is a crucial operation for many web applications. This process allows developers to dynamically manage and store data

In this post , We will learn about How to Insert data using  MySQL Database in detail

Inserting into MySQL Database Using PHP

   Inserting data into a MySQL database with PHP is an essential function for every online application. Using PHP, we can dynamically add records to our database, allowing our application to store user inputs, manage content, and do other data-driven operations.
   To ensure data integrity and application stability, the procedure includes connecting to the MySQL database, creating the SQL INSERT statement, running the query, and dealing with any potential issues. 


Syntax :
INSERT INTO table_name (column1,column2,column3,column4)VALUES(value1,value2,value3,value4);

curd operation insert code 

<?php

$email = $_POST['email'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$address = $_POST['address'];
$gender = $_POST['gender'];
$hobbies = isset($_POST['hobbies']) ? implode(", ", $_POST['hobbies']) : '';
$phonenumber = $_POST['phonenumber'];
$DOB = $_POST['DOB'];

if (isset($_FILES['image']) && $_FILES['image']['error'] == 0) {

    $imageTmp = $_FILES['image']['tmp_name'];
    $imageOriginalName = $_FILES['image']['name'];
    $imageExtension = pathinfo($imageOriginalName, PATHINFO_EXTENSION);
    $imageName = time() . '.' . $imageExtension;
    $imagePath = 'uploads/' . $imageName;
    move_uploaded_file($imageTmp, $imagePath);
    
}

$query = "INSERT INTO phpmaincrud (email, firstname, lastname, address, gender, hobbies, img, phonenumber, DOB) VALUES ('$email', '$firstname', '$lastname', '$address', '$gender', '$hobbies', '$imageName', '$phonenumber', '$DOB')";

$data = mysqli_query($con, $query);

?>

 

Download Full Code HERE 

Download Fome Here