duanpasi6287 2019-02-02 18:26 采纳率: 100%
浏览 69
已采纳

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

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条回答 默认 最新

  • dongqixian8474 2019-02-02 19:31
    关注
       $journalnummer =$_GET["journalnummer"];
    

    Your looking for journalnummer but your input has a name of journal

    <input type="text" name="journal" class="form-control" required>

    $_POST['journal'];
    

    This is what should contain the number .

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?