Main menu

Pages

PHP Connect to MySQL And Insert Data
 
FISRT: 
if  you want to insert data or get data from mysql database.
first thing you must connect to database.
this example to show how can i connecting to database.


Example:
<?php
$servername = "localhost";
$username = "username";
$password = "password";

// Create connection
$conn = new mysqli($servername, $username, $password);

// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>


Second: 

Now you can add the data to database ,this example using to insert data to database first thing must connected to database.
 
In the previous chapter we created an empty table named "table1" with five columns: "id", "firstname", "lastname", "email" and "reg_date". Now, let us fill the table with data.


Example:
<?php

$sql = "INSERT INTO table1(firstname, lastname, email)
VALUES ('John', 'Doe', 'john@example.com')"
;

if ($conn->query($sql) === TRUE) {
  echo "New record created successfully";
else {
  echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?>

Note: If a column is AUTO_INCREMENT (like the "id" column) or TIMESTAMP with default update of current_timesamp (like the "reg_date" column), it is no need to be specified in the SQL query; MySQL will automatically add the value.





You are now in the first article
reactions

Comments

3 comments
Post a Comment

Post a Comment