提交到另一个页面后从数据库获取ID的数据

时间:2018-07-06 08:43:21

标签: php mysql

我正在尝试通过id在showdetails.php页面上的另一页面上获取数据。但是我无法提供帮助。

Showdetails.php
该页面显示具有ID和按钮的用户的详细信息,允许用户在另一页面上编辑详细信息

<!DOCTYPE html>
<html>
<head>
    <title>Edit User Details</title>
</head>
<body>

<?php 
    require('database.php');

    $select = "SELECT firstname, lastname, age, phone_no, age, username, password FROM signup";
    $selectdata = $conn->query($select);

    if ($selectdata->num_rows > 0){
            $row = mysqli_fetch_array($selectdata); 

            $first = $row['firstname'];
            $last = $row['lastname']; 
            $age = $row['age'];
            $phone_no = $row['phone_no'];
            $username = $row['username'];
            $password = $row['password'];
    }
?>

<?php
    if (isset($_POST['update'])) {
        # code...       
        $first = mysqli_real_escape_string($conn, $_POST['first']);
        $last = mysqli_real_escape_string($conn, $_POST['last']);
        $age = mysqli_real_escape_string($conn, $_POST['age']);
        $phone = mysqli_real_escape_string($conn, $_POST['phone_no']);
        $username = mysqli_real_escape_string($conn, $_POST['user']);
        $password = mysqli_real_escape_string($conn, $_POST['pass']);

        $update = "UPDATE signup SET firstname= '$first', lastname= '$last', age= '$age', phone_no = '$phone', username = '$username', password = '$password' WHERE id= '$id'";
        $updatedata = $conn->query($update);

        if ($updatedata) {
            # code...
            echo $status = "Details Updated";
        }
        else {
            # code...
            echo $status = "Not Updated";
        }
    }

    if (isset($_POST['delete'])) {
        # code...

        $delete = "DELETE FROM signup WHERE firstname = $first";
        $deletedata = $conn->query($delete);

        if ($deletedata) {
            # code...
            echo $status = "Details Deleted";
        }
        else {
            # code...
            echo $status = "Not Deleted";
        }
    }
?>  

    <h1>Edit Details</h1>
    <form method="post" action= "<?php htmlspecialchars($_SERVER['PHP_SELF']) ?>">
    <p>FirstName: <input type="text" name="first" value="<?php echo $first; ?>"></p>
    <p>LastName: <input type="text" name="last" value="<?php echo $last; ?>"></p>
    <p>Phone no: <input type="number" name="phone_no" value="<?php echo $phone_no; ?>"></p>
    <p>Age: <input type="number" name="age" value="<?php echo $age; ?>"></p>
    <p>User: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="text" name="user" value="<?php echo $username; ?>"></p>
    <p>Password: <input type="password" name="pass" value="<?php echo $password; ?>"></p>
    <p><input type="submit" name="update" value="Update">
    <input type="submit" name="delete" value="Delete"></p>
    </form>
    <p><?php echo $status; ?></p>


</body>
</html>

editdetails.php
在此页面上,用户将可以编辑详细信息,而我想按ID

#!/user/bin/env python3
from peewee import *

import sys
import datetime
from collections import OrderedDict

db = SqliteDatabase('diary.db')


class Entry(Model):
    content = TextField()
    timestamp = DateTimeField(default=datetime.datetime.now)  # no ()

    class Meta:
        database = db

def intitalize():
    '''create database and table if not exists'''
    db.connect()
    db.create_tables([Entry], safe=True)

def menu_loop():
    '''show menu'''
    choice = None

    while choice != 'q':
        print("enter 'q' to quit")
        for key, value in menu.items():
            print('{}) {}'.format(key, value.__doc__))
        choice = input('action: ').lower().strip()

        if choice in menu:
            menu[choice]()

def add_entry():
    '''add an entry'''
    print("Enter your entry. press ctrl+d when finished")
    data = sys.stdin.read().strip()

    if data:
        if input('Save Entry?[Yn]').lower()!='n':
            Entry.create(content=data)
            print('saved successfully')

def view_entry():
    '''view entries'''

def delete_entry():
    '''delete an entry'''


menu = OrderedDict([
    ('a', add_entry),
    ('v', view_entry),
])


if __name__ == '__main__':
    intitalize()
    menu_loop()   

谢谢。

1 个答案:

答案 0 :(得分:1)

在HTML表单中添加隐藏的输入,如下所示:

<input type="hidden" name="id" value="<?php echo $id; ?>">

然后在editdetails.php文件中,您可以使用$_GET["id"]进行访问。


更新:

将隐藏的输入添加到 Showdetails.php 中的表单中。 然后在 editdetails.php 中添加页面$id = (int)$_GET["id"];

的顶部

然后在 editdetails.php 中的WHERE语句中添加到您的SELECT查询中,以选择正确的用户:

$select = "SELECT ... FROM signup WHERE id = $id";

对于更新查询,因为您已经在使用WHERE id = $id,所以可以继续进行。 (但在您的$id变量未定义之前)