PHP Mysql and PHP Form Processing
PHP Mysql and PHP Form Processing
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->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
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