Hi, I am a newbie in developing a website. i have tried to connect using mysqli conncetion
    Here is my simple code.

    // db.php code
    _______________________

    <?php

    //$con = mysqli_connect("localhost","root","","bakery");
    $db = mysqli_connect('localhost', 'root', '', 'bakery');

    // Check connection
    if (mysqli_connect_errno())
      {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
      }

    ?>

-------------------------------------------------------------------------------
**//Sign up form in index.php**

<?php
                require('db.php');
                // If form submitted, insert values into the database.
                if (isset($_REQUEST['firstname'])){
                    $firstname = stripslashes($_REQUEST['firstname']); // removes backslashes
                    $firstname = mysqli_real_escape_string($con,$firstname); //escapes special characters in a string
                    $lastname = stripslashes($_REQUEST['lastname']); // removes backslashes
                    $lastname = mysqli_real_escape_string($con,$lastname); //escapes special characters in a string
                    $username = stripslashes($_REQUEST['username']); // removes backslashes
                    $username = mysqli_real_escape_string($con,$username); //escapes special characters in a string
                    $phonenumber = stripslashes($_REQUEST['phonenumber']); // removes backslashes
                    $phonenumber = mysqli_real_escape_string($con,$phonenumber); //escapes special characters in a string
                    $email = stripslashes($_REQUEST['email']);
                    $email = mysqli_real_escape_string($con,$email);
                    $password = stripslashes($_REQUEST['password']);
                    $password = mysqli_real_escape_string($con,$password);
                    $trn_date = date("Y-m-d H:i:s");

                            $query = "INSERT into `login` (firstname, lastname, username, phonenumber, email, password, trn_date) VALUES ('$firstname', '$lastname', '$username', '$phonenumber', '$email', '".md5($password)."', '$trn_date')";
                    $result = mysqli_query($con,$query);
                    if($result){
                            //echo "<div class='form'><h3>You are registered successfully.</h3><br/>Click here to <a href='login.php'>Login</a></div>";
                        header( 'Location: content.php' );
                    }
                }else{}
                ?>

   ----------------------------------------------------------
          **  // sign in form in index.php**

             <?php
                require('db.php');
                session_start();

                if($_POST['username'] == $username && $_POST['password'] == $password)
                    header( 'Location: content.php' );
                ?> 

Dani AI

Generated

A short, practical diagnosis and fixes based on the posted snippets and the follow-up from .

Most likely reasons the form “does nothing” here: the browser never sends a POST (HTML form missing method or submit button not of type submit, or JS/CSS blocking the button), or PHP encounters an error but error display is off so nothing is visible. In the code shown there are two clear server-side bugs to fix first: db.php creates a connection under one variable name while index.php expects another, and the sign-in code compares POST values to variables that were never set (so it will never succeed even if the form posts). Enabling error reporting will reveal fatal/notice messages immediately.

Checklist to run now

  • Turn on PHP error reporting and check the server error log.
  • Confirm the HTML form has method="post", matching name attributes, and a submit button with type="submit".
  • Make db.php expose the same connection variable that index.php uses (or change callers to use one consistent variable).
  • Replace comparing raw POST values to unset variables with a DB lookup + password verification (use prepared statements).
  • Ensure session_start() runs before any output and that redirects are followed by exit; check browser DevTools → Network to confirm a POST is actually sent.

Example (debug + safe PDO connection)

<?php
// debug only: enable during troubleshooting
error_reporting(E_ALL);
ini_set('display_errors','1');

// PDO connection (store $pdo and use it everywhere)
$host='127.0.0.1';
$dbname='bakery';
$user='root';
$pass='';
$dsn="mysql:host=$host;dbname=$dbname;charset=utf8mb4";
$pdo = new PDO($dsn,$user,$pass, [
  PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
]);

Example (login check using prepared statements and modern hashing)

// fetch posted values (after validating/trim)
$stmt = $pdo->prepare('SELECT id,password FROM login WHERE username = ? LIMIT 1');
$stmt->execute([$postedUsername]);
$user = $stmt->fetch();
if ($user && password_verify($postedPassword, $user['password'])) {
    session_start();
    $_SESSION['user_id'] = $user['id'];
    // perform redirect to protected page
}

Notes: migrate away from weak hashes (MD5) to password_hash/password_verify; if errors remain invisible, check PHP settings and the webserver error log. For : enabling errors and aligning the connection variable name will very likely show the immediate cause.

Recommended Answers

All 2 Replies

Help us to help you. What error are you getting or what behavior are you seenig that you don't expect?

the button to submit the form seems not working. when i click the button to submit the form, it shows no response.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.