如何修复与数据库连接的错误

时间:2019-04-26 07:53:52

标签: php html mysql forms

我有一个html表单和一个到数据库的连接,当我点击“提交”按钮时,信息将进入我的数据库。但是,如果我在不按任何按钮的情况下刷新页面,信息就会一次又一次地消失。请帮助我解决此问题。 我也希望通过UI对PHP表单CRUD的帮助。您能帮我提供代码吗,假设有一些按钮,这些按钮显示我的数据库表,可以在其中从UI进行更改。

    <!DOCTYPE html>
    <html lang="en" dir="ltr">
      <head>
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
        <meta charset="utf-8">
        <title></title>
      </head>
      <body>
        <form  action="form.php" method="post">
          <table>
            <tr>
              <td>Name:</td>
              <td>
               <input type="text" name="name" placeholder="name" required> <br>
              </td>
            </tr>
            <tr>
              <td>
             Surname:
              </td>
              <td>
              <input type="text" name="surname" placeholder="surname" required>
              </td>
            </tr>
          </table>
          <input type="submit" name="submit">
        </form>
    </form>
      </body>
    </html>

form.php

    <?php
    //Create connection
    $servername='localhost';
    $username = 'root';
    $password  = '';
    $dbname='testdb';
    //create connection
    $conn = new mysqli($servername,$username,$password,$dbname);
    if($conn->connect_error){
      echo $conn->connect_error;
    }else {
      echo "connected successfully<br/>";
    }
    //post method
    if(isset($_POST['submit'])){
      $name = $_POST['name'];
      $surname = $_POST['surname'];
    }
    #insert data
     $result = "INSERT INTO users(name,surname) VALUES('$name','$surname')";
     $show = "SELECT * FROM users";
     if($conn->query($result)){
       echo "data inserted successfully";
     }else {
       $conn->connect_error;
     }
    $conn->close();
    ?>

1 个答案:

答案 0 :(得分:0)

针对您的问题,有几种解决方案。

  1. PHP方法:在数据库中成功存储后进行重定向。您可以使用PHP标头函数将带有成功消息的页面重定向到另一个页面。

  2. JavaScript方法:这种方法比较麻烦,因为它要求使用JavaScript的异步请求来提交数据。脚本成功响应后,您可以使用JavaScript隐藏表单并为用户显示成功消息。

针对您的CRUD系统的帮助

由于您还没有提出自己的方法,因此不能期望您会在这里获得确切的帮助。有很多示例,说明如何实现CRUD系统。

这是CRUD的基本方法。

class Person
{
    public function create($name, $surname) : int
    {
        // insert data into database and return the auto_increament value
        return $id;
    }

    public function read(int $id) : array
    {
        // read out the data by the given id and return the data
        return $data;
    }

    public function update($id, $data) : bool
    {
        // update the database entry with the given id and the given data and return the response of the update statement (true or false)
        return $result;
    }

    public function delete($id) : bool
    {
        // delete the database entry with the given id and return the response
        return $result;
    }
}

这就是您应该开始的方式。然后,您必须处理视图中所需的数据。要读出特定的人,您需要一个ID。要更新特定人员,您需要一个ID。与删除特定人相同。考虑如何获取ID。您可以使用表单中的隐藏输入元素或表单URL中的GET参数来解决此问题。