PHP – MYSQL – Insert Records using a HTML Form

PHP – MYSQL – Insert Records using a HTML Form

I Records sono le righe della tabella.

insert-form.php (the HTML Form)

<html>
<body>

<!-- Send data to insert.php -->
<form action="insert-engine.php" method="post">
Firstname: <input type="text" name="firstname">
Lastname: <input type="text" name="lastname">
Age: <input type="text" name="age">
<input type="submit">
</form>

</body>
</html>

insert-engine.php (the PHP engine)

Syntax:

INSERT INTO table_name
VALUES (value1, value2, value3,…)

or

INSERT INTO table_name (column1, column2, column3,…)
VALUES (value1, value2, value3,…)

<?php
// Create connection
// Statement: mysqli_connect(host,username,password,dbname)
// NOTICE: se lo script è installato nello stesso server del Data Base, host->localhost
$con=mysqli_connect("localhost","lucedigi_user","mypassword","lucedigi_testphp");

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
else
  {
  echo "Great! Connect to MySQL!";
  }
// Aggiungere nuovi record alle colonne con POST - START
$sql="INSERT INTO Persons (FirstName, LastName, Age)
VALUES
('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";

if (!mysqli_query($con,$sql))
  {
  die('Error: ' . mysqli_error($con));
  }
echo "1 record added";
// Aggiungere nuovi record alle colonne con POST - END

mysqli_close($con); 
echo "Great! Connection Closed!"; 
?>
By |MySQL, PHP|Commenti disabilitati su PHP – MYSQL – Insert Records using a HTML Form

PHP – MYSQL – Insert Records

PHP – MYSQL – Insert Records

I Records sono le righe della tabella.

Syntax:

INSERT INTO table_name
VALUES (value1, value2, value3,…)

or

INSERT INTO table_name (column1, column2, column3,…)
VALUES (value1, value2, value3,…)

<?php
// Create connection
// Statement: mysqli_connect(host,username,password,dbname)
// NOTICE: se lo script è installato nello stesso server del Data Base, host->localhost
$con=mysqli_connect("localhost","lucedigi_user","mypassword","lucedigi_testphp");

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
else
  {
  echo "Great! Connect to MySQL!";
  }
// Aggiungere nuovi record alle colonne - START
// Statement:
// INSERT INTO table_name (column1, column2, column3,...) 
// VALUES (value1, value2, value3,...)
mysqli_query($con,"INSERT INTO Persons (FirstName, LastName, Age)
VALUES ('Peter', 'Griffin',35)");

mysqli_query($con,"INSERT INTO Persons (FirstName, LastName, Age) 
VALUES ('Glenn', 'Quagmire',33)");
echo "Great! New Record Inserted!"; 
// Aggiungere nuovi record alle colonne - END

mysqli_close($con); 
echo "Great! Connection Closed!"; 
?>

phpMyAdmin Dashboard:

mysql-0003

By |MySQL, PHP, Web Design|Commenti disabilitati su PHP – MYSQL – Insert Records

PHP – MYSQL – Create a Table

PHP – MYSQL – Create a Table

1. You can create a table using phpMyAdmin

Blue Host users:

CPanel> phpMyAdmin> Enter with User name and password

LEFT COLUMN> you will see ‘Create Table’ button

2. You can create a table using PHP script

<?php
// Create connection
// Statement: mysqli_connect(host,username,password,dbname)
// NOTICE: se lo script è installato nello stesso server del Data Base, host->localhost
$con=mysqli_connect("localhost","lucedigi_user","mypassword","lucedigi_testphp");

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
else
  {
  echo "Great! Connect to MySQL!";
  }
  
// Create table
$sql="CREATE TABLE Persons(FirstName CHAR(30),LastName CHAR(30),Age INT)";

// Execute query
if (mysqli_query($con,$sql))
  {
  echo "Table persons created successfully";
  }
else
  {
  echo "Error creating table: " . mysqli_error($con);
  }
?>

Execute the script and check the Database with phpMyAdmin, you will see on TOP LEFT a small icon with a GREEN ARROW that will refreshes the table list.

Click over the new Table ‘Persons’, you must see:

– a table named “Persons”
– three columns: “FirstName” – “LastName” – “Age”

AN ERROR MESSAGE: Nessun indice definito!

Ogni tabella necessita di un indice UNIVOCO per identificare le righe in una tabella.
Per creare una tabella con un indice possiamo utilizzare il seguente codice:

$sql = "CREATE TABLE Persons 
(
PID INT NOT NULL AUTO_INCREMENT, 
PRIMARY KEY(PID),
FirstName CHAR(15),
LastName CHAR(15),
Age INT
)";

Eliminiamo la tabella da phpMyAdmin:
phpMyAdmin> In ALTO localhost> lucedigi_testphp> spuntiamo la tabella> ‘Elimina’ il messaggio di conferma sarà “Drop Table…”> OK

Eseguiamo lo script:

<?php
// Create connection
// Statement: mysqli_connect(host,username,password,dbname)
// NOTICE: se lo script è installato nello stesso server del Data Base, host->localhost
$con=mysqli_connect("localhost","lucedigi_user","mypassword","lucedigi_testphp");

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
else
  {
  echo "Great! Connect to MySQL!";
  }
  
// Create table with an INDEX
// PID is: Primary Key ID
// it is an INT integer number
// it is not NULL
// AUTO_INCREMENT automatically increases the value of the field by 1 each time a new record is added.
$sql = "CREATE TABLE Persons 
(
PID INT NOT NULL AUTO_INCREMENT, 
PRIMARY KEY(PID),
FirstName CHAR(15),
LastName CHAR(15),
Age INT
)";

// Execute query
if (mysqli_query($con,$sql))
  {
  echo "Table Persons created successfully";
  }
else
  {
  echo "Error creating table: " . mysqli_error($con);
  }
?>

Controllare il database with phpMyAdmin: il messaggio di errore è scomparso, sotto è stata aggiunta la voce ‘Indice’ dove la chiave primaria è il ‘Campo’ ‘PID’

By |MySQL, PHP, Web Design|Commenti disabilitati su PHP – MYSQL – Create a Table

PHP – MYSQL – Open and Close a connection to the MySQL server

PHP – MYSQL – Open and Close a connection to the MySQL server

1. Create a Data Base and assign a User

Blue Host users:

CPanel> Database Tools> MySQL databases>
– Create a Database
– Create a User
– Assign User to Database

2. Manage the DB

Blue Host users:

CPanel> phpMyAdmin> Enter with User name and password

LEFT COLUMN> you will see your new empty Database.

A Database is a data structure with tables, every table has rows and columns.

3. Write PHP code

Open a connection, it will be closed automatically when the script ends

<?php
// Create connection
// Statement: mysqli_connect(host,username,password,dbname)
// NOTICE: se lo script è installato nello stesso server del Data Base, host->localhost
$con=mysqli_connect("localhost","lucedigi_user","mypassword","lucedigi_testphp");

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
else
  {
  echo "Great! Connect to MySQL!";
  }
?>

Statement: mysqli_connect(host,username,password,dbname);

NOTICE: se lo script è installato nello stesso server del Data Base, host->localhost

To close a connection using script:

<?php
// Create connection
// Statement: mysqli_connect(host,username,password,dbname)
// NOTICE: se lo script è installato nello stesso server del Data Base, host->localhost
$con=mysqli_connect("localhost","lucedigi_user","mypassword","lucedigi_testphp");

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
else
  {
  echo "Great! Connect to MySQL!";
  }

// Close connection
mysqli_close($con); 
echo "Connection Closed";
?>

NOTICE: PHP function – mysqli_close() –

By |MySQL|Commenti disabilitati su PHP – MYSQL – Open and Close a connection to the MySQL server