You are on page 1of 6

Home task

Task 1
Practice work
Step 1. Open the xampp and start all modules (!if you not start the xampp the the phpmyadmin not working)

Step 2. Open the link http://localhost/phpmyadmin and in phpmyadmin, create a database called library.

library

Also create a table called books in the library database with the following fields:
Book_id
Bookname
Booknumber
Bookauthor
The datatype for book_id is an auto incrementing integer value.
Set appropriate data types for the rest of the fields
localhost / 127.0.0.1 / library / books | phpMyAdmin 5.2.1
Where author write the field name Bookauthor.

Task 2
Design and create html form to register books in a library. The form should have the following fields
Bookname
Booknumber
Bookauthor

Save this file as newbooks.php in the file root in the xammp/maral/newbooks.php

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>creating forms</title>
</head>
<body>
<table>
<form method="POST" action="newbooks.php" name="user_registration">
<tr><td>Bookname</td><td><input type="text" name="Bookname"></td></tr>
<tr><td>Booknumber</td><td><input type="text" name="Booknumber"></td></tr>
<tr><td>Bookauthor</td><td><input type="text" name=" Bookauthor "></td></tr>
<tr><td><input type="submit" name="submit" value="submit"></td><td></td></tr>
</form>
</table>

<?php
//posting form values to php variables
$Bookname=$_POST['Bookname'];
$Booknumber=$_POST['Booknumber'];
$Bookauthor =$_POST[' Bookauthor '];

//connecting to database
$server='localhost';
$user='root';
$password='';
$db='library';

$conn=mysqli_connect($server,$user,$password,$db);

//inserting records to database

$insert_records=mysqli_query($conn,"INSERT INTO books(bookname,booknumber,


bookauthor)VALUES('$Bookname','$Booknumber','$Bookauthor')");
if($insert_records){
echo 'records inserted';
}else{
echo 'error';
}

?>

</body>
</html>

After then you can see the result, for this you must write the URL address like this

Task 3

In library database, create another table called users with the following fields:
User_id which is an auto incrementing integer value.
Firstname
Lastname
Email
Password
Set appropriate data types for the rest of the fields
Task 4
Design and create html form to register library users. The form should have the following fields
Firstname
Lastname
Email
Password
Save this file as users.php

Step1. Copy the html and php code and put the sublime text in new file and then past the following code and
save this file as users.php

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>creating forms</title>
</head>
<body>
<table>
<form method="POST" action="myform.php" name="user_registration">
<tr><td>firstname</td><td><input type="text" name="firstname"></td></tr>
<tr><td>lastname</td><td><input type="text" name="ln"></td></tr>
<tr><td>email</td><td><input type="text" name="email"></td></tr>
<tr><td>password</td><td><input type="password" name="password"></td></tr>
<tr><td><input type="submit" name="submit" value="submit"></td><td></td></tr>

</form>
</table>
<?php
$fn=$_POST['firstname'];
$ln=$_POST['ln'];
$email=$_POST['email'];
$pass=$_POST['password'];

?>
</body>
</html>

Task 5
In users.php write php script to:
a) Connect to library database
b) Post form data from the form created in (2) above
c) Insert form data to the users table

Step 1. You can see the example to insert record the database from php code and do next task.
Step2. Test your project by inserting at least 5 records to the database through the form

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>creating forms</title>
</head>
<body>
<table>
<form method="POST" action="myform.php" name="user_registration">
<tr><td>firstname</td><td><input type="text" name="firstname"></td></tr>
<tr><td>lastname</td><td><input type="text" name="ln"></td></tr>
<tr><td>email</td><td><input type="text" name="email"></td></tr>
<tr><td>password</td><td><input type="password" name="password"></td></tr>
<tr><td><input type="submit" name="submit" value="submit"></td><td></td></tr>

</form>
</table>
<?php
$fn=$_POST['firstname'];
$ln=$_POST['ln'];
$email=$_POST['email'];
$pass=$_POST['password'];

//connecting to database
$server='localhost';
$user='root';
$password='';
$db='library';

$conn=mysqli_connect($server,$user,$password,$db);

//inserting records to database

$insert_records=mysqli_query($conn,"INSERT INTO
users(fn,ln,email,password)VALUES('$fn','$ln','$email','$pass')");
if($insert_records){
echo 'records inserted';
}else{
echo 'error';
}

?>

</body>
</html>

Task 6
Create a new php file called mybooks.php
Write php code to display all books from the books table in the browser
If select query is correct, you see on the browser the number of records
<?php
//connecting to database
$server='localhost';
$user='root';
$password='';
$db='library';

$conn=mysqli_connect($server,$user,$password,$db);

$library=mysqli_query($conn,"SELECT * FROM books");


$records=mysqli_num_rows($library);
echo $records;

//$all_records=mysqli_num_rows($library);
?>

You might also like