duanpasi6287
duanpasi6287
2019-02-02 18:26

使用php在html中显示数据库搜索结果?

  • php
  • html
  • mysql

I'm attempting to create a page, where I can search for case files from my database, however, I can't get my code to work.

I already have pages showing all content of the database and where I can add new content.

I've read guides, tutorials and questions on stackoverflow, but can't find an answer on how to proceed.

I have the following php code in searchcasescript.php

<?php

$servername = "localhost";
$username = "test";
$password = "";
$dbname = "test";

$conn = mysqli_connect($servername, $username, $password, $dbname) or 
die("connection failed: " . mysqli_connect_error());
mysqli_select_db($conn, $dbname) or die("something went wrong");

if(isset($_GET["form_submit"]))
{
    $journalnummer =$_GET["journalnummer"];

    $stmt = $conn->prepare("select * from testcases where journalnummer=? ");

        $stmt->bind_param("s",$journalnummer);

        $stmt->execute();
    $val = $stmt->get_result();
    $row_count= $val->num_rows;

    if($row_count >0)
    {
        $result =$val->fetch_assoc();

        print_r($result);
    }
    else{
        echo "wrong number";
    }

    $stmt->close();
    $conn->close();
}

?>

and my html:

<div class="container-fluid">
<div class="form-group">
    <form action="searchcasescript.php" method="post">
    <label for="journalnummer">Journalnummer:</label>
    <input type="text" name="journal" class="form-control" required> 
<div class="form-group"> <br>
    <input type="submit" name="form_submit" class="btn btn-basic" value="Søg">
</form>

I've had different results depending on my changes to the php, but currently I'm getting nothing. Pressing search opens a new, blank page.

What I would like to end up with was being able to enter full or part of a case number and the search results being displayed on the same or a new page.

Can anyone point me in the right direction to read more about this and/or where to proceed with my code?

And apologies, if I've somehow missed the answer in one of the questions here.

UPDATED WITH "LIKE":

$stmt = $conn->prepare("SELECT * FROM straffesager WHERE journalnummer LIKE ?");

        $stmt->bind_param("s", '%' . $journal . '%');
  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答