向通过php/html脚本生成的按钮添加功能 [英] Adding functionality to buttons generated via a php/html script

查看:35
本文介绍了向通过php/html脚本生成的按钮添加功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对PHP/HTML/CSS编程非常陌生,在下面附加的代码中,我试图创建网站管理面板的基本功能.

I am very new to PHP/HTML/CSS programming and in the code I have attached bellow is my attempt to create the basic functionality of an administration panel for a website.

我想要做的是打印出表及其所有行和列,并增加一个带有控件的列,该控件使我可以从数据库中删除该行.最终,我希望可以更改每个用户的名称,密码和管理员特权.

What I want to do is to print out the table with all of its rows and columns and to have an additional column with controls which would allow me to delete said row from a database. Eventually I would like to make it possible to change the name, password and administrator privileges for each user.

此外,我真的不知道如何使每个按钮保持一个将其连接到其相应行的值.

Additionally, I really have no idea how to make each button to hold a value connecting it to its respective row.

也许是由于我是一位经验不足的程序员,所以我的所有尝试都失败了或删除了最后一行(也许因为它是$email变量名下的最后一个值).一位朋友建议使用JavaScript或改用其他平台(他的建议是Angular JS)以实现我的目标,但现在我真的想用PHP使其保持简单(如果真的是这样的话)和CSS.

Perhaps due to me being an astoundingly inexperienced programmer all of my attempts have either failed or deleted the last row (perhaps as it was the last value under the $email variable name). A friend suggested to use either JavaScript or to move to a different platform (Angular JS was his suggestion) in order to achieve my goals but for now I would really like to keep it simple (if that is really the word for it) with PHP and CSS.

以下是管理面板的图像:

Here is an image of what the administration panel looks like:

这是我的表生成器(或者与我设法得到的一样好):

Here is my table generator (or as good as I managed to get it):

<?php
    include "connection.php"; 

    $sql = "SELECT * FROM users;";
    $result = $conn->query($sql);

    if ($result->num_rows > 0) 
    {
        echo "<table class='sqltable'>
                <tr class='sqltable'>
                    <th class='sqltable'>ID</th>
                    <th class='sqltable'>EMAIL</th>
                    <th class='sqltable'>NAME</th>
                    <th class='sqltable'>IS ADMIN</th>
                    <th class='sqltable'>PASSWORD</th>
                    <th class='sqltable'>CONTROLS</th>
                </tr>";

        // output data of each row
        while($row = $result->fetch_assoc()) 
        {
            echo "<tr class='sqltable'>                            
                    <td class='sqlcell'>".$row["ID"]."</td>
                    <td class='sqlcell'>".$row["EMAIL"]."</td>
                    <td class='sqlcell'>".$row["FIRST_NAME"]." ".$row["MID_NAME"]." ".$row["LAST_NAME"]."</td>
                    <td class='sqlcell'>".$row["IS_ADMIN"]."</td> 
                    <td class='sqlcell'>".$row["PASSWORD"]."</td>
                    <td class='sqlcell'>
                        <center>
                            <div style='border: 1px solid lightgray;' method='POST'>
                                <input type='hidden' name='ID' value='".$row['ID']." '/>
                                <input type='button' name='delete' value='DEL ".$row['ID']." '/>
                            </div>
                        </center>
                    </td>
                </tr>";
        } 
        echo "</table>";
    }

    else 
    {
        echo "DATABASE IS EMPTY!";
    }  

    $conn->close(); 

    if (isset($_POST['delete']))
    { //if a delete request received
        $id = $_POST['id']; //primary key of this row

        /////// Connectivity /////////
        $servername = "127.0.0.1";
        $username = "root";
        $password = ""; 
        $db = "myDB";    

        // Create connection
        $conn = new mysqli($servername, $username, $password, $db);

        //check connection
        if ($conn)
        {
            printf("Connect failed: %s\n", mysqli_connect_error());
            exit();
        }

        //compose sql statement
        $stmt = mysqli_prepare($conn, "DELETE FROM users WHERE ID=?");

        mysqli_stmt_bind_param($stmt,'i',$id); //now add the $id to the statement 'i' stands for integer

        mysqli_stmt_execute($stmt);

        mysqli_stmt_close($stmt);
        mysqli_close($conn); //connection closed
    }
?>


这就是我开始做的,我已经很确定自己走了错误的路线.


This is what I started doing and I am already pretty sure that I have taken the wrong route to do this.

function delete()
            {
                $del = "DELETE FROM '".$table."' WHERE EMAIL='".$email."';";
                $conn->query($del);
            }

推荐答案

现在,当我实现问题中发布的代码时,我已经做了几处更改以使其正常工作,所有小问题,我都会在此处发布在详细说明中.

now when I implemented your code posted in the question I've changed several things to get it work properly, all minor issues, I'll post them here with in comments elaboration.

<?php
    if (isset($_POST['delete'])) //first: test if any delete request, delete and then render the table 
    { //if a delete request received
        $id = $_POST['id']; //primary key of this row, where 'id' index must be case-sensitively equal to the hidden input name 'id'

        /////// Connectivity /////////
        $servername = "localhost";
        $username = "root";
        $password = "root"; 
        $db = "user_delete";    

        // Create connection (procedural style)
        $conn = mysqli_connect($servername, $username, $password, $db);

        //check connection
        if (!$conn) //if NOT connected
        {
            printf("Connect failed: %s\n", mysqli_connect_error()); //print error
            exit(); //exit the program 'in this case you wouldn't see the table either'
        }

        //compose sql statement
        $stmt = mysqli_prepare($conn, "DELETE FROM users WHERE ID=?"); //notice that sql statements are NOT case sensitive

        mysqli_stmt_bind_param($stmt,'i',$id); //now add the $id to the statement 'i' stands for integer

        mysqli_stmt_execute($stmt);

        mysqli_stmt_close($stmt);
        mysqli_close($conn); //connection closed, row deleted
    }

    include "connection.php"; 

    $sql = "SELECT * FROM users;";
    $result = $conn->query($sql);

    if ($result->num_rows > 0) 
    {
        echo "<table class='sqltable'>
                <tr class='sqltable'>
                    <th class='sqltable'>ID</th>
                    <th class='sqltable'>EMAIL</th>
                    <th class='sqltable'>NAME</th>
                    <th class='sqltable'>IS ADMIN</th>
                    <th class='sqltable'>PASSWORD</th>
                    <th class='sqltable'>CONTROLS</th>
                </tr>";

        // output data of each row
        while($row = $result->fetch_assoc()) 
        {
            echo "<tr class='sqltable'>";
            echo   "<td class='sqlcell'>".$row["id"]."</td>";   //php is case-sensitive so you should use $row['ID'] according to your scheme
            echo   "<td class='sqlcell'>".$row["email"]."</td>";//php is case-sensitive so you should use $row['EMAIL'] according to your scheme
            echo   "<td class='sqlcell'>".$row["name"]."</td>";//for simplicity, I made one field, change it according to your scheme
            echo   "<td class='sqlcell'>".$row["is_Admin"]."</td>";//php is case-sensitive so you should use $row['IS_ADMIN'] according to your scheme
            echo   "<td class='sqlcell'>".$row["password"]."</td>";//same as above
            echo    "<td class='sqlcell'>
                        <center>
                            <div style='border: 1px solid lightgray;'>";
                    echo    "<form method='POST'>"; //must be added in a form with method=post
                    echo        "<input type='hidden' name='id' value='".$row['id']." '/>"; //differntiate between input name `id` and mysql field name you have `ID`, input field name is the index you will fetch in line 4: $_POST['id']
                    echo        "<input type='submit' name='delete' value='DEL ".$row['id']." '/>"; //type: submit, not button
                    echo    "</form>
                            </div>
                        </center>
                    </td>
                </tr>";
        } 
        echo "</table>";
    }

    else 
    {
        echo "DATABASE IS EMPTY!";
    }  
//all done
    $conn->close(); 
?>

更新:现在,这是相同的代码,都采用OOP样式,并且可以重复使用连接:

update: now this is the same code, all in OOP style and reusing connection:

<?php
    include "connection.php"; 
    if (isset($_POST['delete'])) //first: test if any delete request, delete and then render the table 
    { //if a delete request received
        $id = $_POST['id']; //primary key of this row, where 'id' index must be case-sensitivly equal to the hidden input name 'id'

        //check connection
        if (mysqli_connect_errno()) //if connection error existed
        {
            printf("Connect failed: %s\n", mysqli_connect_error()); //print error
            exit(); //exit the program 'in this case you wouldn't see the table either'
        }

        //compose sql statement
        $stmt = $conn->prepare("DELETE FROM users WHERE ID=?"); //notice that sql statements are NOT case sensitive

        $stmt->bind_param('i',$id); //now add the $id to the statement 'i' stands for integer

        $stmt->execute();

        $stmt->close();
    }

    $sql = "SELECT * FROM users;";
    $result = $conn->query($sql);

    if ($result->num_rows > 0) 
    {
        echo "<table class='sqltable'>
                <tr class='sqltable'>
                    <th class='sqltable'>ID</th>
                    <th class='sqltable'>EMAIL</th>
                    <th class='sqltable'>NAME</th>
                    <th class='sqltable'>IS ADMIN</th>
                    <th class='sqltable'>PASSWORD</th>
                    <th class='sqltable'>CONTROLS</th>
                </tr>";

        // output data of each row
        while($row = $result->fetch_assoc()) 
        {
            echo "<tr class='sqltable'>";
            echo   "<td class='sqlcell'>".$row["id"]."</td>";   //php is case-sensitive so you should use $row['ID'] according to your scheme
            echo   "<td class='sqlcell'>".$row["email"]."</td>";//php is case-sensitive so you should use $row['EMAIL'] according to your scheme
            echo   "<td class='sqlcell'>".$row["name"]."</td>";//for simplicity, I made one field, change it according to your scheme
            echo   "<td class='sqlcell'>".$row["is_Admin"]."</td>";//php is case-sensitive so you should use $row['IS_ADMIN'] according to your scheme
            echo   "<td class='sqlcell'>".$row["password"]."</td>";//same as above
            echo    "<td class='sqlcell'>
                        <center>
                            <div style='border: 1px solid lightgray;'>";
                    echo    "<form method='POST'>"; //must be added in a form with method=post
                    echo        "<input type='hidden' name='id' value='".$row['id']." '/>"; //differntiate between input name `id` and mysql field name you have `ID`, input field name is the index you will fetch in line 4: $_POST['id']
                    echo        "<input type='submit' name='delete' value='DEL ".$row['id']." '/>"; //type: submit, not button
                    echo    "</form>
                            </div>
                        </center>
                    </td>
                </tr>";
        } 
        echo "</table>";
    }

    else 
    {
        echo "DATABASE IS EMPTY!";
    }  
//all done
    $conn->close(); 
?>

提示:在现实世界中,永远不要将密码存储为纯文本,不要搜索和阅读更多关于散列

Hint: in real world, never store passwords as plain text, search and read more about hashing

这篇关于向通过php/html脚本生成的按钮添加功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆