0

I am trying to put data from the form to the MySQL database of the wamp server using PHP. I did all and "failed" is displayed on the screen after pressing the submit button.

Here is the PHP code:

<?php

    $con = mysqli_connect('localhost', 'root', '', 'bse3a');

    if (isset($_POST['submit']))
    {
        $fName = $_POST['first_name'];
        $lName = $_POST['last_name'];
        $email = $_POST['email'];
        $password= $_POST['password'];
        $rPassword= $_POST['re_password'];
        $phone = $_POST['phone_number'];
        $dob = $_POST['birth_date'];
        $gender = $_POST['gender'];

        $query = "insert into  students(fName,lName,email,password,rPassword,phone,dob,gender) values ('$fName', '$lName', '$email', '$password', '$rPassword', '$phone', '$dob', '$gender')";

        if(mysqli_query($con,$query)){
            echo "Register successfully";
        }
        else {
            echo "failed";
        }

    }

?>
5
  • 1
    You're only connecting to the database but you're not making any queries at all. $query is just an ordinary PHP-string so when you do if ($query), it will always evaluate as true (since the string isn't empty). To actually do the query, you need to use mysqli_query() I would recommend you going through some PHP + MySQL tutorials. I would also suggest using PDO instead of MySQLi. It has a bit more verbose API which makes it easier to use. Commented Nov 16, 2019 at 13:01
  • Also, you shouldn't use completely unescaped user data directly in your queries either, or you will be wide open for SQL Injection attacks. You should read up on using Prepared Statements with placeholders. You can use that with both MySQLi and PDO. Commented Nov 16, 2019 at 13:06
  • Yes.. I use mysqli_connect() which works. But now it display "failed" Commented Nov 16, 2019 at 13:12
  • Possible duplicate of How to show SQL error in PHP custom mysqli function? Commented Nov 17, 2019 at 14:02
  • Never ever ever never store passwords in plain text! You should always hash the passwords using password_hash() and only store the hashes. Then you can use password_verify() to verify a password against a hash. There's also no reason for having the column rPassword in your database. The "repeat password" field is basically just for validating the password (to make sure the user didn't made a typo in the first password input) Commented Nov 17, 2019 at 14:03

1 Answer 1

1

While there's not enough information to debug your case, you should display that last error to understand why it doesn't work.

//in fail case
echo("Error description: " . mysqli_error($con));

Then update your question.

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.