0% found this document useful (0 votes)
117 views

Crud Mysqli

The document describes the process of performing CRUD (create, read, update, delete) operations on a student registration database using PHP and MySQL. It includes code snippets for a registration form to insert new student records, a table to select and display all records, a form to update existing records based on the student ID, and a script to delete a record based on the student ID. The code connects to a MySQL database called "mcs3efa21" and performs the necessary SQL queries to manage the student data in the database.

Uploaded by

Asfand Yar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
117 views

Crud Mysqli

The document describes the process of performing CRUD (create, read, update, delete) operations on a student registration database using PHP and MySQL. It includes code snippets for a registration form to insert new student records, a table to select and display all records, a form to update existing records based on the student ID, and a script to delete a record based on the student ID. The code connects to a MySQL database called "mcs3efa21" and performs the necessary SQL queries to manage the student data in the database.

Uploaded by

Asfand Yar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

CRUD INSERTION (1)

<!doctype html>
<html>
<head>
    <title>Document</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
<h1 class="align-center">Student Registration Form</h1>
<form action="./update.php" method="post">
    <div class="form-component">
        <label for="firstname">Please enter your first name:</label>
        <input type="text" class="input-width" id="firstname" name="firstname"
value="<?php echo $row['firstname']; ?>">
    </div>
    <div class="form-component">
        <label for="lastname">Please enter your last name:</label>
        <input type="text" class="input-width" id="lastname" name="lastname"
value="<?php echo $row['lastname']; ?>">
    </div>
    <div class="form-component">
        <label for="email">Please enter your email:</label>
        <input type="email" class="input-width" id="email" name="email"
value="<?php echo $row['email']; ?>">
    </div>
    <div class="form-component">
        <label for="age">Please enter your age:</label>
        <input type="number" class="input-width" id="age" name="age" min="10"
max="100" value="<?php echo $row['age']; ?>">
    </div>
    <div class="form-component">
        <label for="">Please enter your gender:</label>
        <input type="radio" id="male" value="male" name="gender" <?php echo
$row['gender']=="male"? "checked":"" ?>>
        <label for="male">Male</label>
        <input type="radio" id="female" value="female" name="gender" <?php echo
$row['gender']=="female"? "checked":"" ?>>
        <label for="female">female</label>
    </div>
    <div class="form-component">
        <label for="address">Please enter your address:</label>
        <textarea name="address" id="address" cols="20" rows="3"><?php echo
$row['address']; ?></textarea>
    </div>
    <div class="form-component">
        <label for="course">Please select your course:</label>
        <select name="course" id="course">
            <option value="Web" <?php echo $row['course']=="Web"? "selected":""
?>>Web</option>
            <option value="VP" <?php echo $row['course']=="VP"? "selected":"" ?
>>VP</option>
            <option value="ITCP" <?php echo $row['course']=="ITCP"?
"selected":"" ?>>ITCP</option>
            <option value="DBMS" <?php echo $row['course']=="DBMS"?
"selected":"" ?>>DBMS</option>
        </select>
    </div>
    <input type="hidden" name="id" value="<?php echo $id?>">
    <div class="form-component">
        <input type="submit" name="submit" value="Save" id="submit">
    </div>
</form>
</body>
</html>

DECLARING VARIABLE
<?php
$firstName = $_POST["firstname"];
$lastName = $_POST["lastname"];
$email = $_POST["email"];
$age = $_POST["age"];
$gender = $_POST["gender"];
$address = $_POST["address"];
$course = $_POST["course"];
/*echo "First Name: ".$firstName.'<br>';
echo "Last Name: ".$lastName.'<br>';
echo "Email: ".$email.'<br>';
echo "Age: ".$age."<br>";
echo "Gender: ".$gender."<br>";
echo "Address: ".$address."<br>";
echo "Course: ".$course."<br>";
*/
$serverName = "localhost";
$userName = "root";
$password = "123";
$dbName = "mcs3efa21";
$con = new mysqli($serverName,$userName,$password,
$dbName);
if($con->error){
die("Error: ".$con->error);
}
$qry = "INSERT INTO users
(firstname,lastname,email,age,gender,address,course)
VALUES ('".$firstName."', '".$lastName."', '".
$email."', ".$age.",'".$gender."','".$address."','".
$course."')";
$result = $con->query($qry);
if($result){
echo "Data inserted";
} else {
echo "Data not inserted";
}
$con->close();
echo "<a href='./index.php'>Home</a>";

?>
SELECTION

<!doctype html>
<html lang="en">
<head>
<title>Table Practice</title>
<link rel="stylesheet" href="style.css">

</head>
<body>
<a id="btnAddUser" href="./MyFirstWebForm.html">Add
User</a>
<?php
$serverName = "localhost";
$userName = "root";
$password = "123";
$dbName = "mcs3efa21";
$con = new mysqli($serverName,$userName,$password,
$dbName);
if($con->error){
die("Error: ".$con->error);
}
$qry = "Select * from users";
$result = $con->query($qry);
echo "<table>";
echo "<tr>";
echo "<th>Id</th>";
echo "<th>First Name</th>";
echo "<th>Last Name</th>";
echo "<th>Email</th>";
echo "<th>Age</th>";
echo "<th>Gender</th>";
echo "<th>Address</th>";
echo "<th>Course</th>";
echo "<th>Actions</th>";
echo "</tr>";
if($result->num_rows>0){
while($row = $result->fetch_assoc()){
echo "<tr>";
echo "<td>".$row["id"]."</td>";
echo "<td>".$row["firstname"]."</td>";
echo "<td>".$row["lastname"]."</td>";
echo "<td>".$row["email"]."</td>";
echo "<td>".$row["age"]."</td>";
echo "<td>".$row["gender"]."</td>";
echo "<td>".$row["address"]."</td>";
echo "<td>".$row["course"]."</td>";
echo "<td><a href='./updateFrom.php?
id=".$row["id"]."'>Update</a> | <a href='./delete.php?
id=".$row["id"]."'>Delete</a> </td>";
echo "</tr>";
}
} else {
echo "No record found";
}
echo "</table>";
$con->close();
?>
</body>
</html>

UPDATION
<?php
$id = $_POST['id'];
$firstName = $_POST["firstname"];
$lastName = $_POST["lastname"];
$email = $_POST["email"];
$age = $_POST["age"];
$gender = $_POST["gender"];
$address = $_POST["address"];
$course = $_POST["course"];
$serverName = "localhost";
$userName = "root";
$password = "123";
$dbName = "mcs3efa21";
$con = new mysqli($serverName,$userName,$password,
$dbName);
if($con->error){
die("Error: ".$con->error);
}
$qry = "Update users set firstname='".$firstName."',
lastname='".$lastName."',
email='".$email."',
age='".$age."',
gender='".$gender."',
address='".$address."',
course='".$course."'";
$result = $con->query($qry);
if($result){
echo "Data Updated";
} else {
echo "Data not Updated";
}
$con->close();
echo "<a href='./index.php'>Home</a>";
?>
DELECTION
<?php
if(!isset($_GET["id"])){
header("location: ./index.php");
}
$id=$_GET["id"];
$con = new mysqli("localhost","root", "123",
"mcs3efa21");
if($con->error){
die("Error:".$con->error);
}
$qry = "delete from users where id=".$id."";
$result = $con->query($qry);
if($result){
echo "Data deleted";
} else {
echo "Data not deleted";
}
$con->close();
echo "<a href='./index.php'>Home</a>";
?>

You might also like