dongpaocuan7498 2019-02-27 06:44
浏览 97
已采纳

为通过php / html脚本生成的按钮添加功能

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.

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:

Click here to see an ugly administration panel.


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
", 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);
            }
  • 写回答

2条回答 默认 最新

  • dongshi3605 2019-03-01 07:58
    关注

    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
    ", 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(); 
    ?>
    

    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
    ", 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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大