Here are the Practical questions and answers for BIM IV Web Technology II.
1. Write php code to check if a number is even or odd.
<?php $number = 12; // Change the value of $number to the desired number if ($number % 2 == 0) { echo $number . " is even."; } else { echo $number . " is odd."; } ?>
2. Write php code to display today’s date and time in 12- Hours format.
<?php // Get the current date and time $currentDateTime = date('Y-m-d h:i:s A'); // Display the current date and time echo "Current date and time: " . $currentDateTime; ?>
3. Write php code to find the length of given string.
<?php $string = "Hello, World!"; // Change the value of $string to the desired string $length = strlen($string); echo "Length of the string: " . $length; ?>
4. Write code to create database. Apply your own assumptions.
<?php $servername = "localhost"; // Replace with your database server name $username = "root"; // Replace with your database username $password = ""; // Replace with your database password // Create a connection to the MySQL server $conn = new mysqli($servername, $username, $password); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } // Create a new database $databaseName = "myDatabase"; // Replace with your desired database name $sql = "CREATE DATABASE $databaseName"; if ($conn->query($sql) === TRUE) { echo "Database created successfully"; } else { echo "Error creating database: " . $conn->error; } // Close the database connection $conn->close(); ?>
5. Write code to connect database and show message for connection status.
<?php $host = 'your_host'; $database = 'your_database'; $username = 'your_user'; $password = 'your_password'; // Create a connection object $conn = new mysqli($host, $username, $password, $database); // Check if the connection was successful if ($conn->connect_error) { // Display an error message if connection fails die("Error while connecting to the database: " . $conn->connect_error); } // Display a success message echo "Connected to the database!"; // Close the connection $conn->close(); ?>
6. Create input forms for user registration. Apply server validation for the following and store in the database.
– Name is required
– Phone must be only digit
– Username is required and min 5 character
– Password is required and exactly 8 character
<?php $errors = []; if ($_SERVER['REQUEST_METHOD'] === 'POST') { if (empty($_POST['name'])) { $errors['name'] = 'Name is required'; } if (empty($_POST['phone']) || !ctype_digit($_POST['phone'])) { $errors['phone'] = 'Phone must be numeric'; } if (empty($_POST['username']) || strlen($_POST['username']) < 5) { $errors['username'] = 'Username is required and should be at least 5 characters long'; } if (empty($_POST['password']) || strlen($_POST['password']) !== 8) { $errors['password'] = 'Password is required and should have exactly 8 characters'; } if (empty($errors)) { $name = $_POST['name']; $phone = $_POST['phone']; $username = $_POST['username']; $password = $_POST['password']; $host = 'your_host'; $database = 'your_database'; $db_username = 'your_username'; $db_password = 'your_password'; $conn = new mysqli($host, $db_username, $db_password, $database); if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } $stmt = $conn->prepare("INSERT INTO users (name, phone, username, password) VALUES (?, ?, ?, ?)"); $stmt->bind_param("ssss", $name, $phone, $username, $password); if ($stmt->execute()) { echo "User registration successful!"; } else { echo "Error: " . $stmt->error; } $stmt->close(); $conn->close(); } } ?>
7. Write code to login using username and password from existing data.
<?php session_start(); require_once 'db_conn.php'; if ($_SERVER['REQUEST_METHOD'] === 'POST') { $username = $_POST['username']; $password = $_POST['password']; $stmt = $conn->prepare("SELECT * FROM users WHERE username = ?"); $stmt->bind_param("s", $username); $stmt->execute(); $result = $stmt->get_result(); if ($result->num_rows === 1) { $row = $result->fetch_assoc(); $storedPassword = $row['password']; if ($password === $storedPassword) { $_SESSION['username'] = $row['username']; $_SESSION['name'] = $row['name']; header("Location: dashboard.php"); exit(); } else { $loginError = "Invalid username or password"; } } else { $loginError = "Invalid username or password"; } $stmt->close(); } ?>
8. Update existing data to change password to user’s phone number.
<?php session_start(); require_once 'db_conn.php'; if ($_SERVER['REQUEST_METHOD'] === 'POST') { if (isset($_SESSION['username'])) { $username = $_SESSION['username']; $stmt = $conn->prepare("SELECT phone FROM users WHERE username = ?"); $stmt->bind_param("s", $username); $stmt->execute(); $result = $stmt->get_result(); if ($result->num_rows === 1) { $row = $result->fetch_assoc(); $phone = $row['phone']; $updateStmt = $conn->prepare("UPDATE users SET password = ? WHERE username = ?"); $updateStmt->bind_param("ss", $phone, $username); $updateStmt->execute(); echo "Password successfully updated to the phone number."; } else { echo "User not found."; } $stmt->close(); $updateStmt->close(); } else { echo "User not logged in."; } } $conn->close(); ?>
9. Delete a data from database.
<?php require_once 'db_conn.php'; if ($_SERVER['REQUEST_METHOD'] === 'POST') { $id = $_POST['id']; $stmt = $conn->prepare("DELETE FROM users WHERE id = ?"); $stmt->bind_param("i", $id); $stmt->execute(); if ($stmt->affected_rows > 0) { echo "Record deleted successfully."; } else { echo "No records found with the given ID."; } $stmt->close(); } $conn->close(); ?>
10. Write PHP code to write “BIM 4th semester” in myfile.txt file.
<?php $filename = "myfile.txt"; if (!file_exists($filename)) { $file = fopen($filename, "w"); fclose($file); } $file = fopen($filename, "w"); if ($file) { fwrite($file, "BIM 4th semester"); fclose($file); echo "Data has been written to $filename"; } else { echo "Unable to open the file."; } ?>