doutanggun9816
2017-01-23 04:32 阅读 101
已采纳

在URL中显示ID号,并将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)

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.

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]

Here's the rest of my code

<?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!

</div>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    dongshang3309 dongshang3309 2017-01-23 04:55

    You are referencing

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

    which is for getting "posted" parameters (usually from forms).

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

    Relevant pages in the manual: $_POST , $_GET


    Edit: 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
             }
         }
     }   
    
    点赞 评论 复制链接分享

相关推荐