0

My assignment is to create a form that will have options to Show data from a table in a database, add new items to the table and delete the contents of the table. I have created the database under the name "PHPAssignment" and added a table under the name "Data". The table has two columns ID (primary key and set to auto increment) and First Name.

The problem I am having is this, when I click on any button, it just opens the page with all of my PHP code written all over it and doesn't execute any of it. I have created the database with sqlbuddy tool on WAMPSERVER 3.0.4 and it is a local database. I have tested the connection and it is working. Both my .html and .php file are in the same folder. I am on a basic PHP course so I would appreciate if your help would include beginner concepts.

Here is my HTML code:

<!DOCTYPE html>
<html>
<head>
<title> Form for managing data </title>
</head>
<body>
<p>Click on "Show" to list table contents</p> 
<p>Click on "Add" to add a new name </p> 
<p>Click on "Erase" to delete contents from table</p> 
<form method = "POST" action = "forAssignment.php">
<table>
    <tr>
    <td><input type = "submit" value = "Show" name = "showBtn"/> <input type = "submit" value = "Erase" name = "eraseBtn" /> </td>
    </tr>
    <tr>
    <td><input type = "text" size = "20" name = "newName" /></td>
    <td><input type = "submit" value = "Add" name = "addBtn" /></td>
    </tr>
</table>
</form>
</body>
</html> 

And PHP code:

<?php 
if (isset($_POST['showBtn']))
{
    $conn = mysqli_connect('localhost', 'root', '');
    $db = mysqli_select_db($conn, 'PHPAssignment');
    $sql = "SELECT * FROM Data";    
    $rs1 = mysqli_query($conn, $sql);

    if (mysqli_num_rows($rs1) == 0)
    {
        echo "No Data";
    }
    else{
        while ($row = mysqli_fetch_array($rs1))
        {
            echo "Name: " .$row[First Name] . "<br/">;
        }
    }
    mysqli_close($conn);
}

else if (isset($_POST['eraseBtn']))
{
    $conn = mysqli_connect('localhost', 'root', '');
    $db = mysqli_select_db($conn, 'PHPAssignment');
    $sql1 = "DELETE * FROM Data";
    $rs3 = mysqli_query($conn, $sql1);
    echo $rs3;
    mysqli_close($conn);
}
else (isset ($_POST['addBtn']))
{
    $name = $_POST['newName'];
    $conn = mysqli_connect('localhost', 'root', '');
    $db = mysqli_select_db($conn, 'PHPAssignment');
    $sql2 = "INSERT INTO Data (First Name) VALUES ($name)";
    $rs2 = mysqli_query($conn, $sql2);
    echo $rs2;
    mysqli_close($conn);
}
?> 

P.S Here is a screenshot of what my browser shows View of browser

0 Answers0