0

I created a registration form that takes 3 inputs from the user and then submits data to MySQL database but in the localhost MySQL, data is not saving. I am PHP Newbie, HELP PLEASE!

On using below registration.php(1) code I get no errors on localhost/register.html but register-form data is not saving in MySql database:

    <?php

    session_start();

    $conn = mysqli_connect('localhost','root','pass','gm-registration');

    $username = $_POST['username'];
    $email = $_POST['email'];
    $password = $_POST['password'];

    $s = "select * from users where username = '$username'";

    $result = mysqli_query($conn, $s);

    $num = mysqli_num_rows($result);

    if($num == 1){
    echo "This Username is Already Taken";
    }
    else{
    $reg = "insert into users(username, email id, password) values('$username', '$email', '$password')";
    mysqli_query($conn, $reg);
    echo "Registration Successful";
    }

    ?>

On using below registration.php(2) code I get one localhost/register.html error ->> Fatal error: Uncaught Error: Call to a member function bind_param() on bool in C:\xampp\htdocs\gamingmode\registration.php:22 Stack trace: #0 {main} thrown in C:\xampp\htdocs\gamingmode\registration.php on line 22.

       <?php

       session_start();

       $conn = mysqli_connect('localhost','root','7777','gm-registration');

       $username = $_POST['username'];
       $email = $_POST['email'];
       $password = $_POST['password'];

       $s = "select * from users where username = '$username'";

       $result = mysqli_query($conn, $s);

       $num = mysqli_num_rows($result);

       if($num==1){
        echo "This Username is Already Taken";
       }
       else{
        $stmt = $conn->prepare("INSERT INTO users(username, email id, password) VALUES(?,?,?)");
        $stmt->bind_param("sss", $username, $email, $password);   //line 22
        $stmt->execute();
        echo "Registration Successful";
        $stmt->close();
        $conn->close();
       }

       ?>
My `html` form code :
    <form id="register" class="input_group" action="registration.php" method="post">
    <input type="username" class="input_field" placeholder="Username" name="username">
    <input type="email" class="input_field" placeholder="Email Id" name="email">
    <input type="password" class="input_field" placeholder="Password" name="password">
    <input type="checkbox" class="checkbox" name="checkbox"> <span>I agree to the term & conditions</span>
    <button type="submit" class="submit_btn">Register</button>
    </form>

What should I do now to save forms data to MySql Database? registration.php(1) is not saving the form's data. & registration.php(2) is throwing one fatal error.

Ritvik
  • 23
  • 6
  • 1
    Of course no errors are showing. You don't check for any errors! But your code is vulnerable to SQL injection, that should be the first thing you fix. https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php – miken32 Mar 08 '20 at 04:58
  • Fixed It now: https://gist.github.com/Reevan799/1a3a984214a4879b0ffa527c70f3d84a but now it is showing error on submission: Fatal error: Uncaught Error: Call to a member function bind_param() on bool in C:\xampp\htdocs\gamingmode\registration.php:22 Stack trace: #0 {main} thrown in C:\xampp\htdocs\gamingmode\registration.php on line 22 – Ritvik Mar 08 '20 at 05:18
  • 1
    You are mixing PDO and MYSQLI. Learn more about it https://www.php.net/manual/en/mysqli.query.php – mail2bapi Mar 08 '20 at 05:28
  • so how can I fix this error I am PHP newbie, just one error I am unable to save register data :( can you please post the answer to the question? – Ritvik Mar 08 '20 at 06:07
  • 1
    First of all improve your code and go step by step. step 1: only submit a form to a php file and get form variables there.step 2: create mysql connection. step then try to submit enquiries into mysql. as you are new to php you need to learn step by step. – Owais Aslam Mar 08 '20 at 06:50
  • @OwaisAslam Sir, I did the same thing, 1st: created from - provided action="registration.php" with method="post" as you can see code I provided in my question. when I click on the submit button it also echo's "Registration Successful" but form data is not showing in MySQL database. my variables, database name and table names are also correct. – Ritvik Mar 08 '20 at 06:58
  • 2
    @Ritvik you are making mysql connection in Procedural style and running query in Object oriented style. check `mail2api's` comment. check the link he has given and try 1 example. – Owais Aslam Mar 08 '20 at 07:22
  • @mail2bapi there is no PDO in the question, only mysqli. – miken32 Mar 08 '20 at 16:51
  • @OwaisAslam there is no problem switching between procedural and OOP with the same mysqli object. Messy for sure, but no errors result from doing it. – miken32 Mar 08 '20 at 16:52
  • There was a problem with my table creation, later I created table using SQL query and it worked well. – Ritvik Mar 09 '20 at 16:14
  • $stmt = $conn->prepare("INSERT INTO users(username, email id, password) VALUES(?,?,?) >>> ; <<< "); You forgot to close. – RaRa Ritalin Feb 12 '21 at 01:10

1 Answers1

-2
$stmt->bind_param("sss", $username, $email, $password);   //line 22

Password is not a string, so, you need to declare an integer as password. This will prevent the fatal error in your code.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
madhan
  • 1