0% found this document useful (0 votes)
13 views10 pages

webd assignment-1

Web Development Assignment

Uploaded by

shellshock4549
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
13 views10 pages

webd assignment-1

Web Development Assignment

Uploaded by

shellshock4549
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 10

WEB-DESIGN

AND
DEVELOPMENT
Assigment-2

Name: Raghav Nanda


Class: B.A. Prog ()
Roll No.: 11024
Section: A
Ques 1. Write a PHP script to print the sum of even digits of a number.

Ans <?php

function sumOfEvenDigits($num) {

$sum = 0;

while ($num > 0) {

$digit = $num % 10;

if ($digit % 2 == 0) {

$sum += $digit;

$num = (int)($num / 10);

return $sum;

// Example usage:

$number = 123456;

$evenSum = sumOfEvenDigits($number);

echo "The sum of even digits in $number is: $evenSum\n";

Output :
Ques 2. Write a script in PHP to display a Multiplication Table.

Ans. <?php

$num = 10; // Consedring the no. as 10

echo "Here is the table of 10 ";

for ($i = 1; $i <= 10; $i++) {

echo $num . "x".$i . "=" . $num * $i .;

?>

Output:

Ques 3. Design a Student Registration form, using appropriate input fields consisting of following:

a. Roll Number

b. First Name

c. Last Name

d. Gender

e. Department

f. DOB

Submit and retrieve the form data using the $_POST variable and display it.

Ans. <!DOCTYPE html>

<html>

<head>

<title>Student Registration Form</title>

</head>
<body>

<h2>Student Registration Form</h2>

<form action="process_form.php" method="post">

<label for="roll_number">Roll Number:</label>

<input type="text" id="roll_number" name="roll_number" required><br><br>

<label for="first_name">First Name:</label>

<input type="text" id="first_name" name="first_name" required><br><br>

<label for="last_name">Last Name:</label>

<input type="text" id="last_name" name="last_name" required><br><br>

<label for="gender">Gender:</label>

<input type="radio" id="male" name="gender" value="male">

<label for="male">Male</label>

<input type="radio" id="female" name="gender" value="female">

<label for="female">Female</label>

<input type="radio" id="other" name="gender" value="other">

<label for="other">Other</label><br><br>

<label for="department">Department:</label>

<select id="department" name="department">

<option value="CSE">Computer Science Engineering</option>

<option value="ECE">Electronics and Communication Engineering</option>

<option value="ME">Mechanical Engineering</option>

</select><br><br>

<label for="dob">Date of Birth:</label>

<input type="date" id="dob" name="dob" required><br><br>

<input type="submit" value="Submit">

</form>

</body>

</html>
<?php

if ($_SERVER["REQUEST_METHOD"] == "POST") {

$roll_number = $_POST["roll_number"];

$first_name = $_POST["first_name"];

$last_name = $_POST["last_name"];

$gender = $_POST["gender"];

$department = $_POST["department"];

$dob = $_POST["dob"];

echo "<h2>Submitted Information:</h2>";

echo "<p>Roll Number: " . $roll_number . "</p>";

echo "<p>First Name: " . $first_name . "</p>";

echo "<p>Last Name: " . $last_name . "</p>";

echo "<p>Gender: " . $gender . "</p>";

echo "<p>Department: " . $department . "</p>";

echo "<p>Date of Birth: " . $dob . "</p>";

?>

Input:
Output (Html ):

Ques 4. Write PHP Code to create a database, connect to it, create tables, insert and access their

contents.

Ans. 1. Database Connection:

<?php

$servername = "localhost";

$username = "your_username";

$password = "your_password";

$dbname = "myDB";

// Create connection

$conn = new mysqli($servername, $username, $password, $dbname);


// Check connection

if ($conn->connect_error) {

die("Connection failed: " . $conn->connect_error);

echo "Connected successfully";

?>

2. Creating a Database:

<?php

$servername = "localhost";

$username = "your_username";

$password = "your_password";

// Create connection

$conn = new mysqli($servername, $username, $password);

// Check connection

if ($conn->connect_error) {

die("Connection failed: " . $conn->connect_error);

// Create database

$sql = "CREATE DATABASE myDB";

if ($conn->query($sql) === TRUE) {

echo "Database created successfully";

} else {

echo "Error creating database: " . $conn->error;

$conn->close();

?>
3. Creating a Table:

<?php

$servername = "localhost";

$username = "your_username";

$password = "your_password";

$dbname = "myDB";

// Create connection

$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection

if ($conn->connect_error) {

die("Connection failed: " . $conn->connect_error);

// Create table

$sql = "CREATE TABLE MyGuests (

id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,

firstname VARCHAR(30) NOT NULL,

lastname VARCHAR(30) NOT NULL,

email VARCHAR(50),

reg_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP

)";

if ($conn->query($sql) === TRUE) {

echo "Table MyGuests created successfully";

} else {

echo "Error creating table: " . $conn->error;

$conn->close();

?>
4. Inserting Data:

<?php

$servername = "localhost";

$username = "your_username";

$password = "your_password";

$dbname = "myDB";

// Create connection

$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection

if ($conn->connect_error) {

die("Connection failed: " . $conn->connect_error);

$sql = "INSERT INTO MyGuests (firstname, lastname, email)

VALUES ('John', 'Doe', 'john@example.com')";

if ($conn->query($sql) === TRUE) {

echo "New record created successfully";

} else {

echo "Error: " . $sql . "<br>" . $conn->error;

$conn->close();

?>

5. Retrieving Data:

<?php

$servername = "localhost";

$username = "your_username";

$password = "your_password";

$dbname = "myDB";
// Create connection

$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection

if ($conn->connect_error) {

die("Connection failed: " . $conn->connect_error);

$sql = "SELECT id, firstname, lastname FROM MyGuests";

$result = $conn->query($sql);

if ($result->num_rows > 0) {

// output data of each row

while($row = $result->fetch_assoc()) {

echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";

} else {

echo "0 results";

$conn->close();

?>

You might also like