Gutanga akazi
Hypertext Preprocessor)
PHP (Hypertext Preprocessor) is a widely-used, open-source scripting language designed primarily for web development.
It is embedded within HTML and is well-suited for creating dynamic and interactive web pages.
PHP scripts are executed on the server side, and the output is sent to the client's web browser as plain HTML.
<?php
$servername = "your_servername";
$username = "your_username";
$password = "your_password";
$dbname = "your_database_name";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Prepare and bind
$stmt = $conn->prepare("INSERT INTO registration (first_name, last_name, email, phone_number, gender, age, password, confirm_password) VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
$stmt->bind_param("sssssiis", $first_name, $last_name, $email, $phone_number, $gender, $age, $password, $confirm_password);
// Set parameters and execute
$first_name = "John";
$last_name = "Doe";
$email = "john.doe@example.com";
$phone_number = "1234567890";
$gender = "Male";
$age = 30;
$password = password_hash("password123", PASSWORD_DEFAULT);
$confirm_password = password_hash("password123", PASSWORD_DEFAULT);
$stmt->execute();
echo "New record created successfully";
$stmt->close();
$conn->close();
?>
//login
<?php
$servername = "your_servername";
$username = "your_username";
$password = "your_password";
$dbname = "your_database_name";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Retrieve user input
$input_username = $_POST['username'];
$input_password = $_POST['password'];
// Prepare and bind
$stmt = $conn->prepare("SELECT password FROM registration WHERE username = ?");
$stmt->bind_param("s", $input_username);
// Execute the statement
$stmt->execute();
// Store the result
$stmt->store_result();
// Check if the username exists
if ($stmt->num_rows > 0) {
// Bind the result to a variable
$stmt->bind_result($hashed_password);
$stmt->fetch();
// Verify the password
if (password_verify($input_password, $hashed_password)) {
echo "Login successful!";
// Here you can start a session, redirect to a dashboard, etc.
} else {
echo "Invalid username or password.";
}
} else {
echo "Invalid username or password.";
}
$stmt->close();
$conn->close();
?>
//Select
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "service_db";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Fetch data from the database
$sql = "SELECT * FROM registration";
$result = $conn->query($sql);
// Check if there are any records
if ($result->num_rows > 0) {
echo "<table border='1'>";
echo "<tr><th>ID</th><th>First Name</th><th>Last Name</th><th>Username</th><th>Email</th><th>Phone Number</th><th>Gender</th><th>Age</th></tr>";
// Output data of each row
while ($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>" . $row["id"] . "</td>";
echo "<td>" . $row["first_name"] . "</td>";
echo "<td>" . $row["last_name"] . "</td>";
echo "<td>" . $row["username"] . "</td>";
echo "<td>" . $row["email"] . "</td>";
echo "<td>" . $row["phone_number"] . "</td>";
echo "<td>" . $row["gender"] . "</td>";
echo "<td>" . $row["age"] . "</td>";
echo "</tr>";
}
echo "</table>";
} else {
echo "No records found";
}
$conn->close();
?>
//with delete
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "service_db";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Handle deletion
if(isset($_GET['delete_id'])){
$delete_id = $_GET['delete_id'];
$delete_sql = "DELETE FROM registration WHERE id=?";
$stmt = $conn->prepare($delete_sql);
$stmt->bind_param("i", $delete_id);
$stmt->execute();
$stmt->close();
echo "Record deleted successfully";
}
// Fetch data from the database
$sql = "SELECT * FROM registration";
$result = $conn->query($sql);
// Check if there are any records
if ($result->num_rows > 0) {
echo "<table border='1'>";
echo "<tr><th>ID</th><th>First Name</th><th>Last Name</th><th>Username</th><th>Email</th><th>Phone Number</th><th>Gender</th><th>Age</th><th>Action</th></tr>";
// Output data of each row
while ($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>" . $row["id"] . "</td>";
echo "<td>" . $row["first_name"] . "</td>";
echo "<td>" . $row["last_name"] . "</td>";
echo "<td>" . $row["username"] . "</td>";
echo "<td>" . $row["email"] . "</td>";
echo "<td>" . $row["phone_number"] . "</td>";
echo "<td>" . $row["gender"] . "</td>";
echo "<td>" . $row["age"] . "</td>";
echo "<td><a href='?delete_id=" . $row["id"] . "'>Delete</a> | <a href='update.php?id=" . $row["id"] . "'>Update</a></td>";
echo "</tr>";
}
echo "</table>";
} else {
echo "No records found";
}
$conn->close();
?>
//UPDATE
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "service_db";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if (isset($_POST['update'])) {
$id = $_POST['id'];
$first_name = $_POST['fname'];
$last_name = $_POST['lname'];
$username = $_POST['username'];
$email = $_POST['mail'];
$phone_number = $_POST['number'];
$gender = $_POST['gender'];
$age = $_POST['age'];
$update_sql = "UPDATE registration SET first_name=?, last_name=?, username=?, email=?, phone_number=?, gender=?, age=? WHERE id=?";
$stmt = $conn->prepare($update_sql);
$stmt->bind_param("ssssssii", $first_name, $last_name, $username, $email, $phone_number, $gender, $age, $id);
$stmt->execute();
$stmt->close();
echo "Record updated successfully";
}
if (isset($_GET['id'])) {
$id = $_GET['id'];
$sql = "SELECT * FROM registration WHERE id=?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $id);
$stmt->execute();
$result = $stmt->get_result();
$record = $result->fetch_assoc();
$stmt->close();
}
?>
<form method="POST" action="">
<input type="hidden" name="id" value="<?php echo $record['id']; ?>">
First Name: <input type="text" name="fname" value="<?php echo $record['first_name']; ?>"><br>
Last Name: <input type="text" name="lname" value="<?php echo $record['last_name']; ?>"><br>
Username: <input type="text" name="username" value="<?php echo $record['username']; ?>"><br>
Email: <input type="email" name="mail" value="<?php echo $record['email']; ?>"><br>
Phone Number: <input type="text" name="number" value="<?php echo $record['phone_number']; ?>"><br>
Gender: <input type="text" name="gender" value="<?php echo $record['gender']; ?>"><br>
Age: <input type="number" name="age" value="<?php echo $record['age']; ?>"><br>
<input type="submit" name="update" value="Update">
</form>
<?php
$conn->close();
?>