在URL&中显示ID号将ID号的数据库结果提取到文本字段中

时间:2017-01-23 04:32:23

标签: php mysql .htaccess

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

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

这是我的.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>
&#13;
&#13;
&#13;

我希望这里有人可以帮我设置它。提前谢谢!

1 个答案:

答案 0 :(得分:0)

您正在引用

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

用于获取“已发布”参数(通常来自表单)。

您需要查看$_GET['u']才能传入ID。

手册中的相关页面:$_POST$_GET

编辑:只是通读代码,还有一些其他位错误,因为以下位来自用户发布表单的示例。 (即getPost()位)。如果您尝试根据传入的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
         }
     }
 }