在网址中显示ID号&将数据库结果从ID号获取到文本字段中 [英] Display ID Number in URL & fetch database results from ID Number into textfields

查看:71
本文介绍了在网址中显示ID号&将数据库结果从ID号获取到文本字段中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个网站上工作,我想在URL上显示ID号以及获取其属性(例如用户名,名字,姓氏)

I'm working on a website where I want to display the ID Number on the url as well as fetch its attributes along with it(e.g username, firstname, lastname)

例如,如果我输入: localhost / search / index3.php?u = 3 ,它将在页面上将数据库值显示在文本字段中。

An example would be if I type: localhost/search/index3.php?u=3, it would display the database values with it on the page into the textfields.

这是我的.htaccess的代码

Here's the codefor my .htaccess

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ http://localhost/search/index3.php?u=$1 [NC]

这是我其余的代码

<?php

$host = "localhost";
$user = "root";
$password ="";
$database = "ntmadb";

$id = "";
$firstname = "";
$lastname = "";
$username = "";

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

// connect to mysql database
try{
    $connect = mysqli_connect($host, $user, $password, $database);
} catch (mysqli_sql_exception $ex) {
    echo 'Error';
}

// get values from the form
function getPosts()
{
    $posts = array();
    $posts[0] = $_POST['id'];
    $posts[1] = $_POST['firstname'];
    $posts[2] = $_POST['lastname'];
    $posts[3] = $_POST['username'];
    return $posts;
}

// Search

if(isset($_POST['search']))
{
    $data = getPosts();
    
    $search_Query = "SELECT * FROM members WHERE id = $data[0]";
    
    $search_Result = mysqli_query($connect, $search_Query);
    
    if($search_Result)
    {
        if(mysqli_num_rows($search_Result))
        {
            while($row = mysqli_fetch_array($search_Result))
            {
                $id = $row['id'];
                $firstname = $row['firstname'];
                $lastname = $row['lastname'];
                $username = $row['username'];
            }
        }else{
            echo 'No Data For This Id';
        }
    }else{
        echo 'Result Error';
    }
}


// Insert
if(isset($_POST['insert']))
{
    $data = getPosts();
    $insert_Query = "INSERT INTO `members`(`firstname`, `lastname`, `username`) VALUES ('$data[1]','$data[2]','$data[3]')";
    try{
        $insert_Result = mysqli_query($connect, $insert_Query);
        
        if($insert_Result)
        {
            if(mysqli_affected_rows($connect) > 0)
            {
                echo 'Data Inserted';
            }else{
                echo 'Data Not Inserted';
            }
        }
    } catch (Exception $ex) {
        echo 'Error Insert '.$ex->getMessage();
    }
}

// Delete
if(isset($_POST['delete']))
{
    $data = getPosts();
    $delete_Query = "DELETE FROM `members` WHERE `id` = $data[0]";
    try{
        $delete_Result = mysqli_query($connect, $delete_Query);
        
        if($delete_Result)
        {
            if(mysqli_affected_rows($connect) > 0)
            {
                echo 'Data Deleted';
            }else{
                echo 'Data Not Deleted';
            }
        }
    } catch (Exception $ex) {
        echo 'Error Delete '.$ex->getMessage();
    }
}

// Edit
if(isset($_POST['update']))
{
    $data = getPosts();
    $update_Query = "UPDATE `members` SET `firstname`='$data[1]',`lastname`='$data[2]',`username`='$data[3]' WHERE `id` = $data[0]";
    try{
        $update_Result = mysqli_query($connect, $update_Query);
        
        if($update_Result)
        {
            if(mysqli_affected_rows($connect) > 0)
            {
                echo 'Data Updated';
            }else{
                echo 'Data Not Updated';
            }
        }
    } catch (Exception $ex) {
        echo 'Error Update '.$ex->getMessage();
    }
}



?>


<!DOCTYPE Html>
<html>
    <head>
        <title>PHP INSERT UPDATE DELETE SEARCH</title>
    </head>
    <body>
        <form action="indexx.php?id=" method="post">
            <input type="number" name="id" placeholder="Id" value="<?php echo $id;?>"><br><br>
            <input type="text" name="firstname" placeholder="First Name" value="<?php echo $firstname;?>"><br><br>
            <input type="text" name="lastname" placeholder="Last Name" value="<?php echo $lastname;?>"><br><br>
            <input type="text" name="username" placeholder="username" value="<?php echo $username;?>"><br><br>
            <div>
                <!-- Input For Add Values To Database-->
                <input type="submit" name="insert" value="Add">
                
                <!-- Input For Edit Values -->
                <input type="submit" name="update" value="Update">
                
                <!-- Input For Clear Values -->
                <input type="submit" name="delete" value="Delete">
                
                <!-- Input For Find Values With The given ID -->
                <input type="submit" name="search" value="Find">
            </div>
        </form>
    </body>
</html>

我希望有人这可以帮助我进行设置。

I hope anyone here can help me set it up. Thanks in advance!

推荐答案

您正在引用

if(isset($_POST['search']))

您需要查看 $ _ GET ['u'] 以获得传入的ID。

You need to be looking at $_GET['u'] to get the id passed in.

手册中的相关页面: $ _ POST $ _ GET

Relevant pages in the manual: $_POST , $_GET

编辑:仅阅读代码,还有其他一些错误,例如以下几位来自用户发布表单的示例。 (即 getPost()位)。如果您尝试基于传入的ID从数据库中获取详细信息,则不需要。

just reading through the code, there are a few other bits wrong as the following bits are from an example where a user has posted a form. (ie. the getPost() bit). You don't need that if you're trying to get details from a database based on a passed in ID.

if (isset($_GET['u'])) {
    if ($stmt = $mysqli->prepare("SELECT * FROM members WHERE id = ?")) {

        /* bind parameters for markers */
         $stmt->bind_param("i", $_GET['u']);

         /* execute query */
         $stmt->execute();

         if ($stmt->affected_rows) {
             // Deleted
         } else {
             // Not found / deleted
         }
     }
 }   

这篇关于在网址中显示ID号&amp;将数据库结果从ID号获取到文本字段中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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