Scripting Language
Scripting Language
Write a PHP script that connects to a MySQL database and retrieves the names and email
addresses of users from a table named 'users'. Display this information in an HTML table
format.
<?php
$servername = "localhost";
$username = "admin";
$password = "admin007";
$database = "consumers";
if ($conn->connect_error) {
$result = $conn->query($sql);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>User Information</title>
<style>
table {
border-collapse: collapse;
width: 100%;
th, td {
padding: 8px;
text-align: left;
th {
background-color: #f2f2f2;
</style>
</head>
<body>
<h2>User Information</h2>
<table>
<tr>
<th>Name</th>
<th>Email</th>
</tr>
<?php
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "</tr>";
} else {
?>
</table>
<?php
$conn->close();
?>
</body>
</html>
2. Develop a web page with a form that includes fields for username, email, and password. Write
JavaScript code to validate the form inputs on submission, ensuring that the username is at
least 3 characters long, the email follows a valid format, and the password is at least 8
characters long. Display appropriate error messages if any input fails validation.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Form Validation</title>
<style>
.error {
color: red;
</style>
</head>
<body>
<h2>Registration Form</h2>
<label for="username">Username:</label><br>
<label for="email">Email:</label><br>
<label for="password">Password:</label><br>
</form>
<script>
function validateForm() {
usernameError.innerHTML = "";
emailError.innerHTML = "";
passwordError.innerHTML = "";
if (username.length < 3) {
isValid = false;
if (!emailRegex.test(email)) {
isValid = false;
if (password.length < 8) {
isValid = false;
}
return isValid;
</script>
</body>
</html>
3. Create a PHP script that retrieves the latest 5 posts from a MySQL table named 'posts' and
formats them as JSON. Write JavaScript code to fetch this JSON data asynchronously and
display the posts dynamically on a web page. Each post should include the title, author, and
content.
• Source Code: PHP
<?php
$servername = "localhost";
$username = "admin";
$password = "admin007";
$database = "jason3";
if ($conn->connect_error) {
$sql = "SELECT title, author, content FROM posts ORDER BY created_at DESC LIMIT 5";
$result = $conn->query($sql);
$posts = [];
if ($result->num_rows > 0) {
$posts[] = $row;
$conn->close();
header('Content-Type: application/json');
echo json_encode($posts);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Latest Posts</title>
</head>
<body>
<h2>Latest Posts</h2>
<div id="postsContainer"></div>
<script>
fetch('latest_posts.php')
.then(posts => {
const postsContainer = document.getElementById('postsContainer');
posts.forEach(post => {
postElement.innerHTML = `
<h3>${post.title}</h3>
<p><strong>Author:</strong> ${post.author}</p>
<p>${post.content}</p>
<hr>
`;
postsContainer.appendChild(postElement);
});
})
</script>
</body>
</html>
4. Implement a PHP script that allows users to register by providing a username, email, and
password. Ensure that the username and email are unique and not already registered in the
database. Store the user information securely in a MySQL database table named 'users'.
<?php
/$servername = "localhost";
$username = "admin";
$password = "admin007";
$database = "4users";
if ($conn->connect_error) {
function sanitizeInput($input) {
return htmlspecialchars(strip_tags(trim($input)));
function hashPassword($password) {
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$username = sanitizeInput($_POST["username"]);
$email = sanitizeInput($_POST["email"]);
$password = sanitizeInput($_POST["password"]);
$stmt = $conn->prepare($sql);
$stmt->bind_param("s", $username);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows > 0) {
exit();
}
$sql = "SELECT * FROM users WHERE email = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("s", $email);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows > 0) {
exit();
$hashedPassword = hashPassword($password);
$sql = "INSERT INTO users (username, email, password) VALUES (?, ?, ?)";
$stmt = $conn->prepare($sql);
if ($stmt->execute()) {
} else {
$stmt->close();
$conn->close();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>User Registration</title>
</head>
<body>
<h2>User Registration</h2>
<label for="username">Username:</label><br>
<label for="email">Email:</label><br>
<label for="password">Password:</label><br>
</form>
</body>
</html>
5. Write SQL queries to create the 'users' table with appropriate columns for username, email,
and password storage.
• Ans :
);