简体   繁体   中英

How to save data in my database with PHP

When I send data to my php file. Data is not getting stored in the database. My php code is below.

I am using one.com as my host. There I can use to php code using some components. So I gave an external link to the button to submit to php file. But I cannot submit data to php code using button name or class attribute. How can I store data in database using php file.

Please help me in this issue.Thank you in advance.

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
    $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".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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