简体   繁体   English

如何使用PHP将数据保存在数据库中

[英]How to save data in my database with PHP

When I send data to my php file. 当我将数据发送到我的php文件时。 Data is not getting stored in the database. 数据未存储在数据库中。 My php code is below. 我的PHP代码如下。

I am using one.com as my host. 我使用one.com作为主机。 There I can use to php code using some components. 在那里,我可以使用一些组件来编写php代码。 So I gave an external link to the button to submit to php file. 所以我给按钮提供了一个外部链接以提交到php文件。 But I cannot submit data to php code using button name or class attribute. 但是我不能使用按钮名称或类属性将数据提交到php代码。 How can I store data in database using php file. 如何使用php文件将数据存储在数据库中。

Please help me in this issue.Thank you in advance. 请帮助我解决这个问题。谢谢。

HTML: HTML:

<form method="post" action="register.php">
   <div class="input-group">
     <lable>Username</lable>
     <input type="text" name="Username" required>
   </div>
   <div class="input-group">
      <lable>Email</lable>
      <input type="text" name="Email" required>
   </div>
   <div class="input-group">
      <lable>Password</lable>
      <input type="password" name="password_1" required></div>

   <div class="input-group">
      <lable>Confirm Password</lable>
      <input type="password" name="password_2" required>
   </div>
   <p>Already a User?</p>
  </form>

PHP: PHP:

    <?php
    $Username = "";
    $Email = "";
    $errors = array();

    // connect to the database

    $db = mysqli_connect('hostname', 'root', 'password', 'dbname');
    echo "database connected";

    // if the register button is clicked

    $username = $_POST['Username'];
    $email = $_POST['Email'];
    $password_1 = $_POST['password_1'];
    $password_2 = $_POST['password_2'];
    echo "data is taken";

    // if there are no errors, save user to database

    $sql = "INSERT INTO Users(Username, Email, password) VALUES('$username', 
 '$email', '$password_1')";
    mysqli_query($db, $sql);
    echo "data inserted successfully";
    ?>

Is your database correct? 您的数据库正确吗?

// connect to the database

$db = mysqli_connect('hostname', 'root', 'password', 'dbname');
echo "database connected";

And remember it always printed "database connected". 并记住它总是打印“数据库已连接”。

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

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