如何在我的php行中添加更新、删除和查看按钮

如何在我的php行中添加更新、删除和查看按钮,php,mysql,Php,Mysql,我想在php表的表行右侧的另一个页面按钮中添加更新、删除和查看。请帮我加上它。这是我的密码: <?php $conn = mysqli_connect('localhost','root','','dbname'); if(mysqli_connect_errno()){ echo 'Failed to connect: '.mysqli_connect_error(); } $query = "SELECT * FROM table"; $results = mysqli_qu

我想在php表的表行右侧的另一个页面按钮中添加更新、删除和查看。请帮我加上它。这是我的密码:

<?php
$conn = mysqli_connect('localhost','root','','dbname');
if(mysqli_connect_errno()){
    echo 'Failed to connect: '.mysqli_connect_error();
}

$query = "SELECT * FROM table";
$results = mysqli_query($conn,$results);

echo '<table border="1">';
    echo '<tr>';
        echo "<th>Firstname</th>";
        echo "<th>Lastname</th>";
    echo '</tr>';

    while($row=mysqli_fetch_array($results)){
        echo '<tr>';
        echo '<td>'.$row['Firstname'].'</td>';
        echo '<td>'.$row['Lastname'].'</td>';
        echo '</tr>';
    }
echo '</table>';
mysqli_close($conn);
?>

我发现您的代码中有一些错误:

$query = "SELECT * FROM table";
$results = mysqli_query($conn,$results);
应该是:

$query = "SELECT * FROM table";
$results = mysqli_query($conn,$query);
试试这个

echo '<table border="1">';
echo '<tr>';
    echo "<th>Firstname</th>";
    echo "<th>Lastname</th>";
    echo "<th></th>";
echo '</tr>';

while($row=mysqli_fetch_array($results)){
    echo '<tr>';
    echo '<td>'.$row['Firstname'].'</td>';
    echo '<td>'.$row['Lastname'].'</td>';
    echo "<td>
             <input type='submit' value='update'>
             <input type='submit' value='delete'>
             <input type='submit' value='view'>
         </td>";
    echo '</tr>';
}
echo '</table>';
2如果您想要不同的格式,请在view.php中执行相同的操作,只选择该记录

我想问一个简单的问题,什么是视图的必要性?当它已经在上表中时

要在Update.php中写入更新,请执行以下操作:

if(isset($_POST['id'])
{
         $id = $_POST['id'];
         $query = "SELECT * FROM table where id=$id";
}
并采取行动

<form method="post" action="<?php echo esc_url($_SERVER['PHP_SELF']); ?>">
尝试以下功能:

<?php
Class Db {
    protected $connection;

    public function __construct() {

        $this->connection = $connection;
    }

    function insert($table,array $data) {
        $fields = '';$values = '';

        foreach($data as $col => $value) {
            $fields.= $col.",";
        }

        foreach($data as $col => $value) {
            $values.= "'".replace_str($value)."',";
        }

        $fields = substr($fields,0,-1);
        $values = substr($values,0,-1);

        if(!$query = mysqli_query($this->connection,"insert into ".$table."(".$fields.") values(".$values.")")) {
            HandleDBError("Error inserting data to the table\query:$query");
        }

        return $query;
    }

    function update($table, array $data, $where) {
        $fields = '';$values = '';

        foreach($data as $col => $value) {
            $values.= $col."='".replace_str($value)."',";
        }

        $values = substr($values,0,-1);

        if(!$query = mysqli_query($this->connection,"update ".$table." set ".$values." where ".$where)) {
            HandleDBError("Error updating data to the table\query:$query");
        }

        return $query;
    }

    function delete($table, $where = '') {
        if ($where)
            return mysqli_query($this->connection,"delete from ".$table." where ".$where);

        return mysqli_query($this->connection,"delete from ".$table);
    }

    function get($strQuery) {
        if(!$query = mysqli_query($this->connection,$strQuery)) {
            HandleDBError("Error inserting data to the table\query:$query");
        }
        $data = [];
        while($row = mysqli_fetch_assoc($query)) {
            $data[] = $row;
        }
        return $data;
    }
}

?>

等等?如何添加“更新”“删除”和“查看”按钮?还是添加、更新、删除和查看功能?这是一段代码,太宽了。添加这个标签,在里面创建一个按钮或两个按钮回显“EditAdd”;我的意思是,在这个.php文件中,表的右侧有3个按钮。更新、删除和查看:如果没有表单元素,这些提交就做不了什么。我认为最好是用这样的参数建立链接-。唯一的问题是更新。我认为他想要解决的唯一问题是如何在数据的每一个右侧插入一个命令按钮,是的,我同意使用链接比使用表单更简单:ddddd先生,你是什么意思我很不确定你的问题,你想实现一个由右侧按钮组成的表格结构,还是一个由右侧按钮和每个按钮的功能组成的表格结构?第二个sir表格结构,由我单击更新时右侧按钮和功能组成,表中的信息将更新。当我单击“删除”时,信息将被删除,先生。当我单击view时,表的信息将显示在view.php页面上。谢谢你,先生先生,在删除或编辑表时,如何添加ajax加载程序:上帝保佑,谢谢你,先生你所说的ajax加载器是什么意思?编辑中不应该有ajax。为了删除,我编辑了我的答案只是为了样本。没关系,先生,我到家后再试试DSir还有一个问题,当我单击您制作的view.php按钮时,它当然会重定向到view.php页面。如何从单击“查看”按钮的表中查看表信息。如何将该表行中的信息获取到view.php页面?我想不出来,先生因为,先生。在view.php中,我想打印该表中的信息。我想创建收据表单。该表是客户的信息。当我单击view按钮时,它将重定向到view.php,以便我打印该信息:先生对不起先生,我问了很多。因为我在家里没有网络连接。当我到达时,我将尝试上面的代码。希望它能正常工作
if(isset($_POST['id'])
{
         $id = $_POST['id'];
         $query = "SELECT * FROM table where id=$id";
}
<form method="post" action="<?php echo esc_url($_SERVER['PHP_SELF']); ?>">
<input type='text' name='firstname' value='<?php echo $row['firstname'];  ?>
<input type='hidden' name='id' value='<?php echo $row['id']; ?>
 if(isset($_POST['firstname'] && isset($_POST['lastname'] ) // Here you can use your any required field  
    { 
             //Your update logic go here like:
             $id = $_POST['id'];
             $query = "UPDATE table SET firstname=$_POST['firstname'] where id=$id"; // Your whole update query.
    }
<?php
Class Db {
    protected $connection;

    public function __construct() {

        $this->connection = $connection;
    }

    function insert($table,array $data) {
        $fields = '';$values = '';

        foreach($data as $col => $value) {
            $fields.= $col.",";
        }

        foreach($data as $col => $value) {
            $values.= "'".replace_str($value)."',";
        }

        $fields = substr($fields,0,-1);
        $values = substr($values,0,-1);

        if(!$query = mysqli_query($this->connection,"insert into ".$table."(".$fields.") values(".$values.")")) {
            HandleDBError("Error inserting data to the table\query:$query");
        }

        return $query;
    }

    function update($table, array $data, $where) {
        $fields = '';$values = '';

        foreach($data as $col => $value) {
            $values.= $col."='".replace_str($value)."',";
        }

        $values = substr($values,0,-1);

        if(!$query = mysqli_query($this->connection,"update ".$table." set ".$values." where ".$where)) {
            HandleDBError("Error updating data to the table\query:$query");
        }

        return $query;
    }

    function delete($table, $where = '') {
        if ($where)
            return mysqli_query($this->connection,"delete from ".$table." where ".$where);

        return mysqli_query($this->connection,"delete from ".$table);
    }

    function get($strQuery) {
        if(!$query = mysqli_query($this->connection,$strQuery)) {
            HandleDBError("Error inserting data to the table\query:$query");
        }
        $data = [];
        while($row = mysqli_fetch_assoc($query)) {
            $data[] = $row;
        }
        return $data;
    }
}