0

I have created a database in phpmyadmin. I am trying to create a register and login, but I am creating a config.php file first which you can see below. I have used placeholders instead of my username and password. However once the web page is loaded it is just saying could not connect.

<?php
 $conn = mysqli_connect("localhost", "username", "password"); 
 if($conn){
    die("could not connect" . mysqli_error($conn));
  }

    mysqli_close($conn);
 ?>

I am not sure if I am connected to my database, as I am new to php. I have created a register form, but anytime I type register.php onto the end of the url it just displays the could not connect. It does not show any of my form and there are no errors in the console. Below is the php I have so far for the register.php. I am just trying to test it at the minute, but the html part isn't showing at all so I cannot input a name to see if it is working.

<?php 

require_once('config.php');

$username = $_POST ['username'];
?> 

PHP is before the head tag in html for the register.php. Does anyone have any suggestions as to why my register form is not appearing?

register.php

<?php 

require_once('config.php');

$username = $_POST ['username'];
?> 

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
        <meta name="description" content="">
        <meta name="author" content="">

        <title>Trip Guider</title>

        <!-- Bootstrap core CSS -->
        <link href="vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">

        <!-- Custom fonts for this template -->
        <link href="vendor/font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css">
        <link href='https://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800' rel='stylesheet' type='text/css'>
        <link href='https://fonts.googleapis.com/css?family=Merriweather:400,300,300italic,400italic,700,700italic,900,900italic' rel='stylesheet' type='text/css'>

        <!-- Plugin CSS -->
        <link href="vendor/magnific-popup/magnific-popup.css" rel="stylesheet">

        <!-- Custom styles for this template -->
        <link href="css/creative.min.css" rel="stylesheet">
        <link href="css/style.css" rel="stylesheet">

    </head>

    <body id="page-top" >

        <!-- Navigation -->
        <nav class="navbar navbar-expand-lg navbar-light fixed-top" id="mainNav">
            <div class="container">
                <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">
                    <span class="navbar-toggler-icon"></span>
                </button>
                <div class="collapse navbar-collapse" id="navbarResponsive">
                    <ul class="navbar-nav ml-auto">
                        <div class="dropdown" id="myDropdown">
                            <a class="btn dropdown-toggle" data-toggle="dropdown" href="#">
                                Menu

                                <span class="caret"></span>
                            </a>
                            <ul class="dropdown-menu">
                                <li><a href="#">Home</a></li>
                                <li><a href="gallery.html">View Holiday Gallery</a></li>
                                <li><a href="itinerary.html">View Itinerary</a></li>
                                <li><a href="settings.html">Settings</a></li>
                            </ul>
                        </div>

                    </ul>
                </div>
            </div>
        </nav>
        <div class="container">
            <div class="card card-container">
                <a href="index.html">
                    <img id="profile-img" class="profile-img-card" src="//ssl.gstatic.com/accounts/ui/avatar_2x.png" alt="logo" />
                </a>
                <p id="profile-name" class="profile-name-card"></p>
                <form class="form-signin" action="" method="post">

                    <input type="text" id="inputEmail" class="form-control" placeholder="Username" required autofocus>

                    <span id="reauth-email" class="reauth-email"></span>
                    <input type="email" id="inputEmail" class="form-control" placeholder="Email address" required autofocus>
                    <input type="password" id="inputPassword" class="form-control" placeholder="Password" required>

                    <button class="btn btn-lg btn-primary btn-block btn-signin" type="submit">Register</button>
                    <button class="btn btn-lg btn-primary btn-block btn-signin" type="submit">Sign in</button>
                </form><!-- /form -->
            </div><!-- /card-container -->
        </div><!-- /container -->
        <script src="js/creative.min.js"></script>
        <script src="js/jquery.js"></script>
    </body> 

</html>
Lawrence Cherone
  • 41,907
  • 7
  • 51
  • 92
M_12
  • 3
  • 5
  • Its connecting, your if statement is wrong `if (!$conn) {` – Lawrence Cherone Dec 11 '17 at 21:21
  • @LawrenceCherone Ok thanks, I changed the if statement in the config.php file. Do you have any idea as to why the html form isn't appearing? – M_12 Dec 11 '17 at 21:24
  • Show us whole script. – ino Dec 11 '17 at 21:32
  • @ino I added a paste bin to my question. Both php files are in it, I just put comments in between them – M_12 Dec 11 '17 at 21:36
  • If the 'config.php' file is not reached by the `require_once` then the script is terminated. – ino Dec 11 '17 at 21:46
  • you had a broken `
    ` in the code, i removed that as it put formatting out of whack.
    – Lawrence Cherone Dec 11 '17 at 21:48
  • @LawrenceCherone I added your code for the form, but it still didn't change anything – M_12 Dec 11 '17 at 21:52
  • instead of using `mysqli_error()` use `mysqli_connect_error()`, what's it say? – Lawrence Cherone Dec 11 '17 at 21:53
  • @LawrenceCherone Still looks the same in the browser with 'could not connect' still displaying but nothing else – M_12 Dec 11 '17 at 21:55
  • A lot of problems can be detected and resolved by [enabling exceptions in `mysqli`](https://stackoverflow.com/questions/14578243/turning-query-errors-to-exceptions-in-mysqli) so mistakes aren't easily ignored. – tadman Dec 11 '17 at 22:00
  • ^ lol .. @M_12 please use the following code and let me know what it says - https://3v4l.org/n90N6 – Lawrence Cherone Dec 11 '17 at 22:03
  • @LawrenceCherone I copied and pasted your code, but forgot to change the placeholders with my data. When I ran this it still said could not connect and didnt have any errors saying my username, password etc was wrong. I cleared my cache and I am definitely working off the right file. And again when I typed in my correct details the webpage didn't change it continues to say "could not connect" – M_12 Dec 11 '17 at 22:11
  • @LawrenceCherone got it sorted using your code, it is just throwing back an error now saying that the $username is undefined, but that is a whole other problem. Cheers for everyones help :) – M_12 Dec 11 '17 at 22:38

4 Answers4

1

Start with turning on the PHP error reporting first. Put just after <?php following stements:

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

How do I get PHP errors to display?

ino
  • 1,698
  • 1
  • 12
  • 21
0

The problem is from the if statement in your connection script; Your ' if ' condition returns true, instead of false, so use this instead ;

if (!$conn) {

}

0

Your statement here is bad:

 if($conn){
    die("could not connect" . mysqli_error($conn));
 }

You have to check is there no connection (!$conn), so the right method is:

if(!$conn){

    die("could not connect" . mysqli_error($conn));
 }

 else{

    //some code here

 }
Sorian
  • 46
  • 5
0

Try this: In config.php u havent included the name of database

<?php
 $conn = mysqli_connect("localhost", "username", "password","dbname"); 
 if(!$conn){
    die("could not connect" . mysqli_error($conn));
  }

    mysqli_close($conn);
 ?>

change register.php as:

<?php 

require('config.php');

$username = $_POST ['username'];
?> 
Sandra
  • 388
  • 4
  • 13