0


I am trying to create a register/login html/php script. I created a database and I believe my html/php code is correct. I was wondering if I am missing something small. Here is my code so far.

Here is the database

<?php
         error_reporting(E_ALL);
         ini_set('display_errors', 'on');
if(isset($_POST["submit"]))
        {
        $User = "**"; 
        $Password = "**";
        $Database = "member";
        $Host = "localhost"; 
        $con = mysqli_connect($Host, $User, $Password);
        if (mysqli_connect_errno())
        {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
        }

        mysqli_select_db($con,$Database);
        $myusername = $_POST["username"];
        $mypassword = $_POST["password"];
        $sql = "INSERT INTO member (username, password) VALUES ('$myusername','$mypassword')";
        mysqli_query($con,$sql);
        mysqli_commit($con);
        mysqli_close($con);

        echo "Thank You! Information entered.";
        }
else
        {
        ?>
        <form method="post" action="proInput.php">
        Username:<input type="Text" name="username"><br>
        Password:<input type="Text" name="password"><br>
        <input type="Submit" name="submit" value="Register"></form>
        <?
        }
?>

Every time I type SELECT * FROM member; in MySQL my database is empty.

6
  • this may be obvious but is there records in the db? Commented Nov 11, 2015 at 18:10
  • Nope my db is empty. I am new to creating databases too. Did I create my db incorrectly? Commented Nov 11, 2015 at 18:13
  • Try if(isset($_POST["submit"])) Commented Nov 11, 2015 at 18:28
  • It's still showing Empty set (0.00 sec) Commented Nov 11, 2015 at 18:35
  • what's the name of the php file? Commented Nov 12, 2015 at 3:26

3 Answers 3

2
mysqli_commit($con);
mysqli_query($con,$sql);
mysqli_close($con);

You're committing before you insert, and then closing an uncommitted transaction. Try swapping the first two lines in the above excerpt.

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

4 Comments

It still didn't populate my datebase. =(
Looks like you're still leaving out the commit.That's the step that tells the dbms to save the insert.
Just fixed it. I left it out for some reason. It's still not populating with the commit code. Sorry about that.
mysqli_query() and mysqli_commit() both return success or failure. If one of those is actually failing a call to mysqli_error() should prove informative.
1

As you answered already in your comment,

the name of the file isn't main_login.php but proinput.php

Your form is posting it's data to main_login.php and I'm assuming you don't have the insert query on that page, your code isn't run at all.

Options:

  1. Try changing the name of this php file to main_login.php and then instead of the echo redirect the user to the login page

  2. Move this part of the insert to your main_login.php

    if(isset($_POST["submit"]))
        {
        $User = ""; 
        $Password = "";
        $Database = "member";
        $Host = "localhost"; 
        $con = mysqli_connect($Host, $User, $Password);
        if (mysqli_connect_errno())
        {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
        }
        mysqli_select_db($con,$Database);
        $myusername = $_POST["username"];
        $mypassword = $_POST["password"];
        $sql = "INSERT INTO member (username, password) VALUES ('$myusername','$mypassword')";
        mysqli_query($con,$sql);
        mysqli_commit($con);
        mysqli_close($con);
    
    
    
    
        echo "Thank You! Information entered.";
        }
        else
        {
        ?>
    
  3. for debugging add the following to the very top of your php file right after the opening of php

     error_reporting(E_ALL);
     ini_set('display_errors', 'on');
    

This worked for me

  <?php

  error_reporting(E_ALL);
  ini_set('display_errors', 'on');

  if(isset($_POST["submit"]))
    {
    $User = "db_user"; 
    $Password = "db_password";
    $Database = "db_name";
    $Host = "db_host"; 
    $con = mysqli_connect($Host, $User, $Password);
    if (mysqli_connect_errno())
    {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    mysqli_select_db($con,$Database);
    $myusername = $_POST["username"];
    $mypassword = $_POST["password"];
    $sql = "INSERT INTO member (username, password) VALUES ('$myusername','$mypassword')";
    mysqli_query($con,$sql);
    mysqli_commit($con);
    mysqli_close($con);

    echo "Thank You! Information entered.";
    }
    else
    {
    ?>
    <form method="post" action="proinput.php">
    Username:<input type="Text" name="username"><br>
    Password:<input type="Text" name="password"><br>
    <input type="Submit" name="submit" value="Register"></form>
    <?php
    }
    ?>

7 Comments

Just updated my proInput.php. file. My database still isn't populating. I must be overlooking something simple.
please check the debugging option and reply with the error
is your db called member and your table also called member?
Yep I double checked. I really have no idea why this isn't populating my datebase. Would the error show up on the screen?
if you add the error reporting as I suggested you should se the error on screen
|
1

please add 'or die (mysqli_error())' at the end of your query and get the sql error report. That should tell you what is exactly wrong with sql . Php error reporting will only give php errors.

1 Comment

I assigned $Database incorrectly. I was able to connect to MySQL correctly but I thought $Database was suppose to be the name of my table. I changed my $Database and everything works now. Thank you everyone for all the help.

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.