Php Mysqlkritika
Php Mysqlkritika
<!DOCTYPE html>
<html>
<head><title>MySQL Connection</title></head>
<body>
<?php
if ($conn->connect_error) {
?>
</body>
</html>
OUTPUT :
Question: Create an HTML form that takes name and email, then write a
PHP script to insert the data into a MySQL table.
<!DOCTYPE html>
<html>
<head>
<title>Insert User</title>
</head>
<body>
</form>
<br>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if ($conn->connect_error) {
$name = $_POST['name'];
$email = $_POST['email'];
} else {
$conn->close();
?>
</body>
</html>
OUTPUT :
Question: Write PHP code to fetch and display all records from a MySQL
table in an HTML table.
<!DOCTYPE html>
<html>
<head><title>Display Users</title></head>
<body>
<h2>User Records</h2>
<table border="1">
<tr><th>ID</th><th>Name</th><th>Email</th></tr>
<?php
$conn = new mysqli("localhost", "root", "", "php_journal");
$result = $conn->query("SELECT * FROM users");
while ($row = $result->fetch_assoc()) {
echo "<tr><td>{$row['id']}</td><td>{$row['name']}</td><td>{$row['email']}</
td></tr>";
}
?>
</table>
</body>
</html>
OUTPUT :
<!DOCTYPE html>
<html>
<head><title>Update User Email</title></head>
<body>
<h2>Update User Email</h2>
<br>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$conn = new mysqli("localhost", "root", "", "php_journal");
if ($conn->connect_error) {
die("Connection failed");
}
$name = $_POST['name'];
$email = $_POST['email'];
$conn->close();
}
?>
</body>
</html>
OUTPUT :
Question: Write a PHP script to delete a specific record from a table based
on the user ID passed via GET.
<!DOCTYPE html>
<html>
<head><title>Delete User Record</title></head>
<body>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$conn = new mysqli("localhost", "root", "", "php_journal");
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$name = $_POST['name'];
}
} else {
echo "Error: " . $conn->error;
}
$conn->close();
}
?>
</body>
</html>
OUTPUT :
Question: Build a user registration form and store the user's data (name,
email, password) in the database securely using password hashing.
<!DOCTYPE html>
<html>
<head><title>User Registration</title></head>
<body>
<h2>Register</h2>
<form method="post">
Name: <input type="text" name="name" required><br><br>
Email: <input type="email" name="email" required><br><br>
Password: <input type="password" name="password" required><br><br>
<input type="submit" value="Register">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$conn = new mysqli("localhost", "root", "", "php_journal");
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$name = $_POST["name"];
$email = $_POST["email"];
$password = password_hash($_POST["password"], PASSWORD_DEFAULT);
} else {
echo "<p>Error: " . $conn->error . "</p>";
}
$conn->close();
}
?>
</body>
</html>
OUTPUT :
Question: Create a login form and authenticate users using data stored in
the MySQL table.
<!DOCTYPE html>
<html>
<head><title>User Login</title></head>
<body>
<h2>Login</h2>
<form method="post">
Email: <input type="email" name="email" required><br><br>
Password: <input type="password" name="password" required><br><br>
<input type="submit" value="Login">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$conn = new mysqli("localhost", "root", "", "php_journal");
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$email = $_POST["email"];
$password = $_POST["password"];
} else {
echo "<p>Email not found.</p>";
}
$conn->close();
}
?>
</body>
</html>
OUTPUT :
Question: Build a search form that filters and shows records matching the
entered keyword from a MySQL table.
<!DOCTYPE html>
<html>
<head>
<title>Search by Name</title>
</head>
<body>
<form method="get">
Name: <input type="text" name="keyword" required>
<input type="submit" value="Search">
</form>
<?php
if (isset($_GET['keyword']) && !empty($_GET['keyword'])) {
$conn = new mysqli("localhost", "root", "", "php_journal");
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$keyword = $conn->real_escape_string($_GET['keyword']);
<th>ID</th>
<th>Name</th>
<th>Email</th>
</tr>";
while ($row = $result->fetch_assoc()) {
echo "<tr>
<td>{$row['id']}</td>
<td>{$row['name']}</td>
<td>{$row['email']}</td>
</tr>";
}
echo "</table>";
} else {
echo "<p>No users found with that name.</p>";
}
$conn->close();
}
?>
</body>
</html>
OUTPUT :
<!DOCTYPE html>
<html>
<head>
<title>Paginated User List</title>
</head>
<body>
<?php
$conn = new mysqli("localhost", "root", "", "php_journal");
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$conn->close();
?>
</body>
</html>
OUTPUT :
Question: Create an image upload form. Save the image to a folder and
store the file name/path in a MySQL table.
<!DOCTYPE html>
<html>
<head><title>Image Upload</title></head>
<body>
<h2>Upload an Image</h2>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$conn = new mysqli("localhost", "root", "", "php_journal");
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if (move_uploaded_file($imageTmpName, $uploadPath)) {
$stmt = $conn->prepare("INSERT INTO images (image_path) VALUES
(?)");
$stmt->bind_param("s", $uploadPath);
if ($stmt->execute()) {
echo "Image uploaded successfully!";
} else {
echo "Error: Could not save to database.";
}
$stmt->close();
} else {
echo "Error: Could not upload the image.";
}
} else {
echo "Error: Invalid image file type.";
}
} else {
echo "Error: No file selected or file upload error.";
}
$conn->close();
}
?>
</body>
</html>
OUTPUT :