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!"; ?>