0% found this document useful (0 votes)
26 views5 pages

2024

The document contains SQL queries for retrieving employee and project data from a database, including filtering by title, salary, and department. It also includes a JavaScript function for modifying paragraph content and a PHP script for handling user registration with validation and database insertion. The PHP script demonstrates secure practices such as input sanitization and password hashing.

Uploaded by

wiltorkuate10
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views5 pages

2024

The document contains SQL queries for retrieving employee and project data from a database, including filtering by title, salary, and department. It also includes a JavaScript function for modifying paragraph content and a PHP script for handling user registration with validation and database insertion. The PHP script demonstrates secure practices such as input sanitization and password hashing.

Uploaded by

wiltorkuate10
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

# SQL Query Solutions

## 5) Employees with title 'EE' or 'SA' and salary > 35,000


```sql
SELECT emp_number, emp_name
FROM employees
WHERE (title = 'EE' OR title = 'SA') AND salary > 35000;
```

## 6) Employees in department 'D1' ordered by decreasing salary


```sql
SELECT emp_name
FROM employees
WHERE dept_id = 'D1'
ORDER BY salary DESC;
```

## 7) Departments ordered by ascending name


```sql
SELECT *
FROM departments
ORDER BY dept_name ASC;
```

## 8) Employee name, department name, and employee title


```sql
SELECT e.emp_name, d.dept_name, e.title
FROM employees e
JOIN departments d ON e.dept_id = d.dept_id;
```

## 9) Project name, hours worked, and project number for hours > 10
```sql
SELECT p.proj_name, w.hours, w.proj_number
FROM works_on w
JOIN projects p ON w.proj_number = p.proj_number
WHERE w.hours > 10;
```

## 10) Project name, department name, and budget for projects with budget < 50,000
```sql
SELECT p.proj_name, d.dept_name, p.budget
FROM projects p
JOIN departments d ON p.dept_id = d.dept_id
WHERE p.budget < 50000;
```
## 11) Employee numbers and salaries in 'Consulting' department ordered by descending
salary
```sql
SELECT e.emp_number, e.salary
FROM employees e
JOIN departments d ON e.dept_id = d.dept_id
WHERE d.dept_name = 'Consulting'
ORDER BY e.salary DESC;
```

## 12) Employee name, project name, employee title, and hours for all works_on records
```sql
SELECT e.emp_name, p.proj_name, e.title, w.hours
FROM works_on w
JOIN employees e ON w.emp_number = e.emp_number
JOIN projects p ON w.proj_number = p.proj_number;
```

# Section C: Web Design Solutions

## a) JavaScript function to modify paragraph content


```html
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>JS DOM paragraph style</title>
<script>
function js_content() {
document.getElementById('text').innerHTML = "Content changed successfully!";
document.getElementById('text').style.color = "blue";
document.getElementById('text').style.fontWeight = "bold";
}
</script>
</head>
<body>
<p id='text'>JavaScript Exercises - w3resource</p>
<div>
<button id="jsstyle" onclick="js_content()">Style</button>
</div>
</body>
</html>
```

## b) Form Handling with PHP

### Form Drawing:


```
+--------------------------------+
| Username: [______________] |
| Email: [______________] |
| Password: [______________] |
| Mobile: [______________] |
| |
| [ Register ] |
+--------------------------------+
```

### PHP script (formHandler.php):


```php
<?php
// Database connection parameters
$servername = "localhost";
$username = "root";
$password = "usel2";
$dbname = "STUDENT";

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

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

// Get form data


$username = $_POST['username'];
$email = $_POST['email'];
$password = $_POST['password_1'];
$mobile = $_POST['mobile'];

// Prepare and bind


$stmt = $conn->prepare("INSERT INTO users (username, email, password, mobile)
VALUES (?, ?, ?, ?)");
$stmt->bind_param("ssss", $username, $email, $password, $mobile);

// Execute and check


if ($stmt->execute()) {
echo "New record created successfully";
} else {
echo "Error: " . $stmt->error;
}

// Close connections
$stmt->close();
$conn->close();
?>
```

### Enhanced version with validation:


```php
<?php
// Database connection
$servername = "localhost";
$username = "root";
$password = "usel2";
$dbname = "STUDENT";

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

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

// Initialize variables
$errors = [];
$username = $email = $password = $mobile = '';

// Validate and sanitize input


if ($_SERVER["REQUEST_METHOD"] == "POST") {
$username = test_input($_POST["username"]);
$email = test_input($_POST["email"]);
$password = test_input($_POST["password_1"]);
$mobile = test_input($_POST["mobile"]);

// Validation checks
if (empty($username)) {
$errors[] = "Username is required";
}

if (empty($email) || !filter_var($email, FILTER_VALIDATE_EMAIL)) {


$errors[] = "Valid email is required";
}

if (empty($password)) {
$errors[] = "Password is required";
} elseif (strlen($password) < 6) {
$errors[] = "Password must be at least 6 characters";
}

if (empty($mobile)) {
$errors[] = "Mobile number is required";
}

// If no errors, proceed with database operation


if (empty($errors)) {
// Hash password
$hashed_password = password_hash($password, PASSWORD_DEFAULT);

// Prepare and bind


$stmt = $conn->prepare("INSERT INTO users (username, email, password, mobile)
VALUES (?, ?, ?, ?)");
$stmt->bind_param("ssss", $username, $email, $hashed_password, $mobile);

// Execute and check


if ($stmt->execute()) {
echo "<p style='color:green;'>Registration successful!</p>";
} else {
echo "<p style='color:red;'>Error: " . $stmt->error . "</p>";
}

$stmt->close();
} else {
// Display errors
foreach ($errors as $error) {
echo "<p style='color:red;'>$error</p>";
}
}
}

$conn->close();

// Function to sanitize input


function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
```

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy