2

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";
    ?>
11
  • except if i didn't well understand the problem here is that you have to use a form to send your data in the php file, not simply a link on a button. Commented Aug 14, 2017 at 8:54
  • can you also post the code from the Form you are passing it from Commented Aug 14, 2017 at 8:55
  • Post your HTML form code so we can help you. Commented Aug 14, 2017 at 8:55
  • 1
    Also your MySQL code is faulty it's wide open to a MySQL injection attack don't use variables directly inside the SQL statement use prepared statements with your mysqli. Commented Aug 14, 2017 at 8:57
  • Ya sure actually I am developing website in one.com which provides platform to publish the website. Commented Aug 14, 2017 at 14:37

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.