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 outlook无法配置成功
  • ¥15 Pwm双极模式H桥驱动控制电机
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换