Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Can somebody be able to solve my other computer science question? It is been aro

ID: 3708126 • Letter: C

Question

Can somebody be able to solve my other computer science question? It is been around for couple days. Now, Certainly, there is enough data in the question.

Maybe the question is hard, but can someone give the try? What I need is: how to merge all of the functionalities from our crud methods into one script. Write a script that implements read, update and delete functions to your contact data.

Link to the question: https://www.chegg.com/homework-help/questions-and-answers/last-week-created-table-getting-contact-information-user-files-attached--created-form-allo-q28364012

Explanation / Answer

Following is the crud operation code:

index.php

<?php require_once 'php_action/db_connect.php'; ?>

<!DOCTYPE html>

<html>

<head>

    <title>PHP CRUD</title>

    <style type="text/css">

        .manageMember {

            width: 50%;

            margin: auto;

        }

        table {

            width: 100%;

            margin-top: 20px;

        }

    </style>

</head>

<body>

<div class="manageMember">

    <a href="create.php"><button type="button">Add Member</button></a>

    <table border="1" cellspacing="0" cellpadding="0">

        <thead>

            <tr>

                <th>Name</th>

                <th>Email</th>

                <th>Contact</th>

                <th>Comments</th>

                <th>Option</th>

            </tr>

        </thead>

        <tbody>

            <?php

            $sql = "SELECT * FROM members WHERE active = 1";

            $result = $connect->query($sql);

            if($result->num_rows > 0) {

                while($row = $result->fetch_assoc()) {

                    echo "<tr>

                        <td>".$row['fname']." ".$row['lname']."</td>

                        <td>".$row['email']."</td>

                        <td>".$row['contact']."</td>

                        <td>".$row['comment']."</td>

                        <td>

                            <a href="edit.php?id=".$row["id']."'><button type='button'>Edit</button></a>

                            <a href="remove.php?id=".$row["id']."'><button type='button'>Remove</button></a>

                        </td>

                    </tr>";

                }

            } else {

                echo "<tr><td colspan='5'><center>No Data Avaliable</center></td></tr>";

            }

            ?>

        </tbody>

    </table>

</div>

</body>

</html>

create.php

<!DOCTYPE html>

<html>

<head>

    <title>Add Member</title>

    <style type="text/css">

        fieldset {

            margin: auto;

            margin-top: 100px;

            width: 50%;

        }

        table tr th {

            padding-top: 20px;

        }

    </style>

</head>

<body>

<fieldset>

    <legend>Add Member</legend>

    <form action="php_action/create.php" method="post">

        <table cellspacing="0" cellpadding="0">

            <tr>

                <th>First Name</th>

                <td><input type="text" name="fname" placeholder="First Name" /></td>

            </tr>       

            <tr>

                <th>Last Name</th>

                <td><input type="text" name="lname" placeholder="Last Name" /></td>

            </tr>

            <tr>

                <th>Email</th>

                <td><input type="email" name="email" placeholder="Email" /></td>

            </tr>

            <tr>

                <th>Contact</th>

                <td><input type="text" name="contact" placeholder="Contact" /></td>

            </tr>

            <tr>

                <th>Comment</th>

                <td><textarea cols="19" rows="7" name="comment"></textarea></td>

            </tr>

            <tr>

                <td><button type="submit">Save Changes</button></td>

                <td><a href="index.php"><button type="button">Back</button></a></td>

            </tr>

        </table>

    </form>

</fieldset>

</body>

</html>

edit.php

<!DOCTYPE html>

<html>

<head>

    <title>Add Member</title>

    <style type="text/css">

        fieldset {

            margin: auto;

            margin-top: 100px;

            width: 50%;

        }

        table tr th {

            padding-top: 20px;

        }

    </style>

</head>

<body>

<fieldset>

    <legend>Add Member</legend>

    <form action="php_action/create.php" method="post">

        <table cellspacing="0" cellpadding="0">

            <tr>

                <th>First Name</th>

                <td><input type="text" name="fname" placeholder="First Name" /></td>

            </tr>       

            <tr>

                <th>Last Name</th>

                <td><input type="text" name="lname" placeholder="Last Name" /></td>

            </tr>

            <tr>

                <th>Email</th>

                <td><input type="email" name="email" placeholder="Email" /></td>

            </tr>

            <tr>

                <th>Contact</th>

                <td><input type="text" name="contact" placeholder="Contact" /></td>

            </tr>

            <tr>

                <th>Comment</th>

                <td><textarea cols="19" rows="7" name="comment"></textarea></td>

            </tr>

            <tr>

                <td><button type="submit">Save Changes</button></td>

                <td><a href="index.php"><button type="button">Back</button></a></td>

            </tr>

        </table>

    </form>

</fieldset>

</body>

</html>

remove.php

<?php

require_once 'php_action/db_connect.php';

if($_GET['id']) {

    $id = $_GET['id'];

    $sql = "SELECT * FROM members WHERE id = {$id}";

    $result = $connect->query($sql);

    $data = $result->fetch_assoc();

    $connect->close();

?>

<!DOCTYPE html>

<html>

<head>

    <title>Remove Member</title>

</head>

<body>

<h3>Do you really want to remove ?</h3>

<form action="php_action/remove.php" method="post">

    <input type="hidden" name="id" value="<?php echo $data['id'] ?>" />

    <button type="submit">Save Changes</button>

    <a href="index.php"><button type="button">Back</button></a>

</form>

</body>

</html>

<?php

}

?>

create a folder php_action and then add below files in it.

php_action/create.php

<?php

require_once 'db_connect.php';

if($_POST) {

    $fname = $_POST['fname'];

    $lname = $_POST['lname'];

    $email = $_POST['email'];

    $contact = $_POST['contact'];

    $comment = $_POST['comment'];

    $sql = "INSERT INTO members (fname, lname, contact, email, comment, active) VALUES ('$fname', '$lname', '$contact', '$email', '$comment', 1)";

    if($connect->query($sql) === TRUE) {

        echo "<p>New Record Successfully Created</p>";

        echo "<a href="../create.php"><button type='button'>Back</button></a>";

        echo "<a href="../index.php"><button type='button'>Home</button></a>";

    } else {

        echo "Error " . $sql . ' ' . $connect->connect_error;

    }

    $connect->close();

}

?>

php_action/db_connect.php

<?php

$localhost = "127.0.0.1";

$username = "root";

$password = "";

$dbname = "php_crud";

// create connection

$connect = new mysqli($localhost, $username, $password, $dbname);

// check connection

if($connect->connect_error) {

    die("connection failed : " . $connect->connect_error);

} else {

    // echo "Successfully Connected";

}

?>

php_action/remove.php

<?php

require_once 'db_connect.php';

if($_POST) {

    $id = $_POST['id'];

    $sql = "UPDATE members SET active = 2 WHERE id = {$id}";

    if($connect->query($sql) === TRUE) {

        echo "<p>Successfully removed!!</p>";

        echo "<a href="../index.php"><button type='button'>Back</button></a>";

    } else {

        echo "Error updating record : " . $connect->error;

    }

    $connect->close();

}

?>

php_action/update.php

<?php

require_once 'db_connect.php';

if($_POST) {

    $fname = $_POST['fname'];

    $lname = $_POST['lname'];

    $email = $_POST['email'];

    $contact = $_POST['contact'];

    $comment = $_POST['comment'];

    $id = $_POST['id'];

    $sql = "UPDATE members SET fname = '$fname', lname = '$lname', email = '$email', contact = '$contact', comment = '$comment' WHERE id = {$id}";

    if($connect->query($sql) === TRUE) {

        echo "<p>Succcessfully Updated</p>";

        echo "<a href="../edit.php?id=".$id.""><button type='button'>Back</button></a>";

        echo "<a href="../index.php"><button type='button'>Home</button></a>";

    } else {

        echo "Erorr while updating record : ". $connect->error;

    }

    $connect->close();

}

?>