AgerNic.com
WEB DEVELOPER SITE, HTML, CSS, PHP, SQL

PHP MySQLi insert


<< Previous Page
PHP tutorial
Next Page >>
PHP tutorial

PHP Tutorial » PHP MySQLi insert

To insert data into a MySQL table, you must use SQL INSERT INTO. You can enter data in the already informed MySQL table using the mysql> prompt or another PHP script

Here is a SQL syntax of INSERT INTO command to add new records to MySQL table:

Syntax:

INSERT INTO my_table_name ( my_field1, my_field2, my_field3.... )
VALUES ( value1, value2, value3 .....);

In this chapter as well as in the following chapters we will show you three methods of working with PHP and MySQL INSERT INTO:

 

MySQL insert data MySQLi (object oriented)

Before accessing data from the MySQL database, we must be able to connect to the server:

Example (this is HTML - PHP editor, change text on this window)

<?php
$server_name = "localhost";
$user_name = "my_username";
$password = "my_password";
$db_name = "my_DB";

// Create connection
$link = new mysqli($server_name, $user_name,
$password, $db_name);
// Check connection
if ($link->connect_error) {
die("Connection failed: " . $link->connect_error);
}

$sql = "INSERT INTO Members (name, user_name, email)
VALUES ('Johny', 'johnyboss', '[email protected]')";

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

$link->close();
?>

 

Related subjects:
MySQLi select data example PHP connect to MySQL example Delete file in PHP

Tags: php mysqli insert: id, query, multiple rows, return id, query not working, blob, result, date, row, prepared statement, variables into database, code for inserting data into database from form, insert data from form into mysql, mysqli lasr insert id, insert query in php

 

MySQL insert data MySQLi (Procedural)

Attempt MySQL server connection. Assuming you are running MySQL server with default setting ("localhost", "root", "your_password", "DB_name")

Example (this is HTML - PHP editor, change text on this window)

<?php
$server_name = "localhost";
$user_name = "my_username";
$password = "my_password";
$db_name = "my_DB";

// Create connection
$link = mysqli_connect($server_name, $user_name,
$password, $db_name);
// Check connection
if (!$link) {
die("Connection failed: " . mysqli_connect_error());
}

$sql = "INSERT INTO Members (name, user_name, email)
VALUES ('Johny', 'johnyboss', '[email protected]')";
if (mysqli_query($link, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br />" . mysqli_error($conn);
}

mysqli_close($link);
?>

 

MySQL insert data using (PDO) extension.

PDO extension. Attempt MySQL server connection. Assuming you are running MySQL server with default setting ("localhost", "root", "your_password")

Example (this is HTML - PHP editor, change text on this window)

<?php
$server_name = "localhost";
$user_name = "my_username";
$password = "my_password";
$db_name = "my_DB";

try {
$link = new PDO("mysql:host=$server_name;dbname=$db_name",
$user_name, $password);
// set the PDO error mode to exception
$link->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$sql = "INSERT INTO Members (name, user_name, email)
VALUES ('Johny', 'johnyboss', '[email protected]')";
// use exec() because no results are returned
$link->exec($sql);
echo "New record created successfully";
} catch(PDOException $e) {
echo $sql . "<br />>" . $e->getMessage();
}

$link = null;
?>

 



php mysqli insert: id, query, multiple rows, return id, query not working, blob, result, date, row, prepared statement,
variables into database, code for inserting data into database from form, insert data from form into mysql, mysqli lasr insert id, insert query in php
PHP MySQLi insert query - php tutorial

Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


HTML Templates
news templates
Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


Home
PHP Introduction
PHP Install
PHP Syntax
PHP Variables
PHP Echo and Print
PHP Data Types
PHP String Function
PHP Constants
PHP Operator Types
PHP If Else and Elseif
PHP Switch
PHP While Loops
PHP Loop For Do Foreach
PHP Array
Convert Array to String
PHP Function
PHP GET and POST
PHP Date/Time Functions
PHP Login
PHP Delete Element
PHP eregi_replace()
PHP mysql_query()
PHP Errors to Display
PHP Loop For Do Foreach

PHP forms
PHP Form Example
Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


HTML Templates
news templates
Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


jQuery replace...
jQuery replaceClass...
jQuery click ()...
jQuery switchClass()...
jQuery draggable()...
jQuery HTML...
jQuery scroll smooth...
jQuery scroll()...
jQuery stop()...
jQuery animate()...
jQuery SlideToggle()...
jQuery slideDown()...
jQuery slideUp()...
jQuery fadeToggle...
jQuery fadeOut()...
jQuery fadeIn()...
jQuery Effects - hide () and show ()...
jQuery Events...
jQuery Attributes...
jQuery Selectors...