MySQL Create Table "registration_form"


Syntax:
CREATE TABLE table_name
(
column_name1 data_type,
column_name2 data_type,
column_name3 data_type,

)

We must add the CREATE TABLE statement to the mysql_query() function to execute the command.
So far I have created already my database next is to create table.
Create table name: registration_form.
A database must be selected before a table can be created. The database is selected with the mysql_select_db() function.

<?php
$con = mysql_connect(“localhost”,”root”,” “);
if (!$con)
{
die(‘Could not connect: ‘ . mysql_error());
}

// Create database
if (mysql_query(“CREATE DATABASE engr_department”,$con))
{
echo “Database created”;
}
else
{
echo “Error creating database: ” . mysql_error();
}

// Create table
mysql_select_db(“engr_department”, $con);
$sql = “CREATE TABLE registration_form
(
FirstName varchar(15),
LastName varchar(15),
Age int(2),
Emailadd varchar(30),
Address varchar(80)
)”;

// Execute query
mysql_query($sql,$con);

mysql_close($con);
?>

Glance for a while of the code above, so terrific right?
I’ve created a table named “registration_form” with five columns. The column names are “firstname“, “lastname“, “age“, “emailadd“, and “address“. This fields are already in the database engr_department.

The table registration_form already exist.
Each table should have a primary key field. A primary key is used to uniquely identify the rows in a table. Each primary key value must be unique within the table. Furthermore, the primary key field cannot be null because the database engine requires a value to locate the record.

The following example sets the personID field as the primary key field. The primary key field is often an ID number, and is often used with the AUTO_INCREMENT setting. AUTO_INCREMENT automatically increases the value of the field by 1 each time a new record is added. To ensure that the primary key field cannot be null, we must add the NOT NULL setting to the field.

$sql = “CREATE TABLE registration_form
(
personID int NOT NULL AUTO_INCREMENT,
PRIMARY KEY(personID),
FirstName varchar(15),
LastName varchar(15),
Age int(2),
Emailadd varchar(30),
Address varchar(80)
)”;

mysql_query($sql,$con);

MySQL Insert

Insert Data Into a Database Table

The INSERT INTO statement is used to add new records to a database table.

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

From the above program code we created a table named “registration_form“, with five columns. The column names are “firstname“, “lastname“, “age“, “emailadd“, and “address“. We will use the same table in this example. The following example below adds three new records to the “registration_form” table:

<?php
$con = mysql_connect(“localhost”,”root”,” “);
if (!$con)
{
die(‘Could not connect: ‘ . mysql_error());
}

mysql_select_db(“engr_department”, $con);

mysql_query(“INSERT INTO registration_form
(FirstName, LastName, Age, Emailadd, Address)
VALUES (‘Ivan’, ‘Smith’, ’15’, ‘ivansmith@yahoo.com’, ‘Los Angeles’)”);

mysql_query(“INSERT INTO registration_form
(FirstName, LastName, Age, Emailadd, Address)
VALUES (‘Draether’, ‘Uy’, ’17’, ‘draether17@yahoo.com’, ‘California’)”);

mysql_query(“INSERT INTO registration_form
(FirstName, LastName, Age, Emailadd, Address)
VALUES (‘Glenn’, ‘Lee’, ’19’, ‘lee19143@yahoo.com’, ‘South Korea’)”);

mysql_close($con);
?>



Insert Data From a Form Into a Database

As of now we will create an HTML form that can be used to add new records to the “registration_form” table.
Here is the HTML form:

<html>
<body>

<form action=”insert.php” method=”post”>
Firstname: <input type=”text” name=”firstname” />
Lastname: <input type=”text” name=”lastname” />
Age: <input type=”text” name=”age” />
Emailaddress: <input type=”text” name=”emailadd” />
Address: <input type=”text” name=”address” />
<input type=”submit” value=”submit” name=”submit” />
</form>

</body>
</html>

When a user clicks the submit button in the HTML form in the example above, the form data is sent to a php file “insert.php“. The “insert.php” file connects to a database, and retrieves the values from the form with the PHP $_POST variables. Then, the mysql_query() function executes the INSERT INTO statement, and a new record will be added to the “registration_form” table.

Here is the “insert.php” page:

<?php
$con=mysql_connect(“localhost”,”root”,””);
if(!$con)
{
die(‘could not connect:’.mysql_error());
}

mysql_select_db(“engr_department”,$con);

$sql= “INSERT INTO registration_form
(firstname, lastname, age, emailadd, address)
VALUES
(‘$_POST[firstname]’,’$_POST[lastname]’,’$_POST[age]’,’$_POST[emailadd]’, ‘$_POST[address]’)”;

if (!mysql_query($sql,$con))
{
die(‘Error: ‘ . mysql_error());
}

echo “1 record added”;
mysql_close($con);
?>