单击按钮时PHP代码未运行

时间:2016-06-09 21:57:21

标签: php html mysqli

我的任务是创建一个表单,该表单将包含从数据库中的表中显示数据,向表中添加新项以及删除表内容的选项。我创建了名为" PHPAssignment"的数据库。并在名称" Data"下添加了一个表格。该表有两列ID(主键和设置为自动增量)和名字。

我遇到的问题是,当我点击任何按钮时,它只是打开页面,我的所有PHP代码都写在它上面并且不会执行任何操作。我在WAMPSERVER 3.0.4上使用sqlbuddy工具创建了数据库,它是一个本地数据库。我测试了连接,它正在工作。我的.html和.php文件都在同一个文件夹中。我在基础PHP课程,所以如果你的帮助包括初学者概念,我将不胜感激。

这是我的HTML代码:

<!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> 

和PHP代码:

<?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以下是我的浏览器显示的屏幕截图 View of browser

0 个答案:

没有答案