Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 13

Form processing

Php and mysql

Lab 5
1. Connection to the Server
2. Creating a Database
3. Selecting a Database
4. Listing Database
5. Listing Table Names
6. Creating a Database Table
7. Inserting Data
Form processing addition of 2 numbers
<html> $number1 = $_POST['number1'];
<body> $number2 =
<form method="post"> $_POST['number2'];
Enter First Number: $sum = $number1+
<input type="number" $number2;
name="number1" /><br><br>
$min = $number1-$number2;
Enter Second Number:
<input type="number"
name="number2" /><br><br> echo "The sum of $number1 and
<input type="submit" $number2 is: ".$sum;echo
name="submit" "<br>";echo "The subtraction of
value="perfprma"> $number1 and $number2 is: ".
</form> $min;
<?php }
if(isset($_POST['submit'])) ?>
{ </body>
</html>
MySQL
 With PHP, you can connect to and manipulate databases.
 MySQL is the most popular database system used with PHP.
 What is MySQL?
 MySQL is a database system used on the web
 MySQL is a database system that runs on a server
 MySQL is ideal for both small and large applications
 MySQL is very fast, reliable, and easy to use
 MySQL uses standard SQL
 MySQL compiles on a number of platforms
 MySQL is free to download and use
 MySQL is developed, distributed, and supported by Oracle
Corporation
 MySQL is named after co-founder Monty Widenius's daughter:
My
PHP Connect to MySQL

$conn = new mysqli("hostname", "username", "password", "database");


 The hostname parameter specify the host name (e.g. localhost), or
IP address of the MySQL server,
 the username and password parameters specifies the credentials to access
MySQL server, and
 the database parameter, if provided will specify the default MySQL
database to be used when performing queries.
Dbconnect.php
<?php
$conn=mysqli_connect('localhost', 'root', '', 'IPDB');
if(!$conn){
die("Error: Can't connect to database!");
}
?>
Create MySQL Database Using MySQLi createDB.php 
<?php
$servername = "localhost"; // Create database
$sql = "CREATE DATABASE IPDB";
$username = “root";
if ($conn->query($sql) === TRUE) {
$password = ""; echo "Database created successfully";
$dbname = "myDB"; } else {
// Create connection echo "Error creating database: " .
$conn = new mysqli($servername, $conn->error;
$username, $password,$dbname); }

$conn->close();
if ($conn->connect_error) { ?>
die("Connection failed: " . $conn->connect_error);
}

// (Use your own servername, username and password if they are different.)
// $conn allows us to keep referring to this connection after it is established
PHP Create MySQL Tables createtable.php

<?php firstname VARCHAR(30) NOT


$servername = "localhost"; NULL,
$username = "root"; lastname VARCHAR(30) NOT
$password = ""; NULL,
$dbname = "IPDB"; gander char(5) NOT NULL,
email char(20)NOT NULL,
// Create connection
password char(50)NOT NULL,
$conn = new mysqli($servername, $username, subjects VARCHAR(50)NOT NULL,
$password,$dbname); reg_date TIMESTAMP DEFAULT
CURRENT_TIMESTAMP ON
// Check connection UPDATE CURRENT_TIMESTAMP
if ($conn->connect_error) { )";
die("Connection failed: " . $conn->connect_error); if ($conn->query($sql) === TRUE) {
echo "Table student created
}
successfully";
} else {
// sql to create table echo "Error creating table: " . $conn-
$sql = "CREATE TABLE student ( >error;
id INT(6) UNSIGNED AUTO_INCREMENT }
PRIMARY KEY,
department VARCHAR(30) NOT NULL, $conn->close();
Html form (index.php)
<!DOCTYPE html>
<html lang="en">
<head></head>
<body>
<h3 class="text-primary">PHP -form processing</h3>
<hr style="border-top:1px dotted #ccc;"/>
<form method="POST" action="save.php">
<label>Department </label>
<select name="dept">
<Option value="CS">CS</Option>
<Option value="IT">IT</Option>
<Option value="SW">SW</Option>
</select><br>
<label>Firstname</label> <br>
<input type="text" name="firstname" required="required"/>
<br>
Html form
<label>Lastname</label> <br>
<input type="text" name="lastname" required="required"/><br>
<label>gander</label> <br>
<input type="radio" name="gander" value="male" required="required"/>male
<input type="radio" name="gander" value="female" required="required"/>female<br>
<br>
<label>email</label> <br>
<input type="email" name="email" required="required"/><br>
<label>password</label> <br>
<input type="password" name="password" required="required"/><br>
<label>Favorite programing:</label> <br>
<br />
<input type="checkbox" name="subject[]" value="C++"/>C++
<br />
<input type="checkbox" name="subject[]" value="OOP"/>C++ with OOP
<br />
<input type="checkbox" name="subject[]" value="ojava"/>oop with java
<br />
<input type="checkbox" name="subject[]" value="History"/>advanced java
<br /><br />
<button name="save" >Save</button>
</form>
</body>
</html>
Processing form (php code) with insert into save.php

<?php
error_reporting(); $sql = "INSERT INTO student
/* allows you to control how many and which PHP (department,firstname, lastname,
errors will be reported*/ gander,email,password,subjects)
?> VALUES('$dept','$firstname',
<?php '$lastname','$gander','$email','$password',
$conn=mysqli_connect('localhost', 'root', '', 'IPDB'); '$subjects')";
if(!$conn){ if(mysqli_query($conn, $sql)){
die("Error: Can't connect to database!"); echo "Records added successfully.";
} } else{
echo "ERROR: Could not able to execute
$sql. " . mysqli_error($conn);
if(ISSET($_POST['save'])){
}
$dept = $_POST['dept'];
$firstname = $_POST['firstname'];
// Close connection
$lastname = $_POST['lastname'];
mysqli_close($conn);
$gander = $_POST['gander'];
}
$email = $_POST['email'];
$password = $_POST['password']; ?>
$subjects=implode(", ", $_POST['subject']);
• Before actually use a database we need to
select it as below
• <?php
– mysql_select_db(“DemoDB”) or die (“Can not
Select Database”);
• ?>
• The die command stops further script
processing with an error message if the
database cannot be selected.
Exercise
Write php code that implements the following
1. Altering Tables
2. Deleting Databases
3. Select Queries
4. Accessing the Result
• In SQL show databases will display all the current databases.
• One way to do the same in PHP is :
<?php
$result = mysql_list_dbs($connect);
for($row=0;$row<mysql_num_rows($result);$row++)
{
$dbases .= mysql_tablename($result,$row) . “<br/>”;
}
echo($dbases);
?>
– Note: mysql_tablename is generalized function for table/dastabase
name
– Note: mysql_tablename is depricated function and should not be used

You might also like