简体   繁体   English

这个PHP代码有什么问题? 从数据库检索数据

[英]What is wrong in this php code? Retrieving data from database

What is wrong in this code? 该代码有什么问题? Here I am trying to show the data fetched from a mySQL database when someone clicks on the "Show" button (Which is a submit button). 在这里,我试图显示当有人单击“显示”按钮(这是一个提交按钮)时从mySQL数据库获取的数据。

<?php
if(isset($_POST['submit'])){
    $mysqli = new mysqli("localhost", "rms", "sarangi", "rms");
    if($mysqli === false){
        die("Connection Error.");
    }
    else{
        echo'<table border="1"><tr><th> Name </th><th> Username </th></tr>';
        $sql = "SELECT * FROM users";
        if($result = $mysqli->query($sql)){
            if($result->num_rows > 0){
                while($data = $result->fetch_array()){
                    echo'<tr><td>' . $data['firstname'] . " " .     $data['lastname'] . '</td><td>' . $data['username'] . '</td></tr>';
                }
            }
        }
        echo'</table>';
    }
    $mysqli->close();
}
else{
?>
<form action="index.php" method="POST">
   <input type="submit" id="submit" value="Show" />
</form>
<?php
  }
?>

You have to name your input field 您必须命名输入字段

<input type="submit" id="submit" value="Show" />

should be 应该

<input name="submit" type="submit" id="submit" value="Show" />

change your code to this 将您的代码更改为此

<form action="" method="POST">
   <input type="submit" id="submit" name="submit" value="Show" />
</form>

I have changed form action to "" since its going to submit to the same page. 自将表单操作提交到同一页面以来,我已将其更改为“”。 or you can leave it as index.php if this page is index.php. 或者,如果此页面为index.php,则可以将其保留为index.php。 You need to put the name attribute of input submit as submit 您需要将输入的名称属性Submit提交为Submit

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM