-1

I am having trouble connecting a database i have created in phpMyAdmin to my web page using PHP. I am pretty sure my code is correct in both the html and php files and my DB and table names are correct, but when i try to use the registration form, it does nothing. I am using jQuery mobile, so have tried it with and without the data-ajax="false" attribute to no avail. What am i missing here? Thanks for any answers :) I have included the relevant html and php code below, it is the register form in the register page I am trying to connect to the DB.

<!DOCTYPE html>
<html lang="en">

<head>
  <meta name="viewport" content="width=device-width, initial-scale=1; minimum-scale=1;">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
  <link href="/projects/ibill_v3/css/mainstyles.css" rel="StyleSheet"/>
  <link href="/projects/ibill_v3/css/register.css" rel="StyleSheet"/>
  <link href="/projects/ibill_v3/css/login.css" rel="StyleSheet"/>
  <link href="/projects/ibill_v3/css/home.css" rel="StyleSheet"/>
  <link href="/projects/ibill_v3/css/record_session.css" rel="StyleSheet"/>
  <link href="/projects/ibill_v3/css/viewsessions.css" rel="StyleSheet"/>
  <link href="/projects/ibill_v3/css/email.css" rel="StyleSheet"/>
  <script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
  <script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
  <script src="script.js"></script>
  <script type='text/javascript' src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js"></script>
  <script type='text/javascript'>
            $(document).on('pageinit', function(){
                $('#registerForm').validate({ // initialize the plugin
                    // rules & options
                });
            });  
  </script>
  <script type='text/javascript'>
            $(document).on('pageinit', function(){
                $('.loginform').validate({ // initialize the plugin
                    // rules & options
                });
            });  
  </script>

</head>

<body>
 <!--********************************REGISTER PAGE**********************************************-->
<!--****************************************************************************************-->

<!--********************************HEADER**********************************************-->
<div data-role="page" id="register">
    <div data-role="header" data-id="foo1" data-position="fixed">
    <div class='cssmenu'>
      <ul>
         <li class='active'><a href='#home'>Home</a></li>
         <li><a href='#sessionrecord'>Record a Session</a></li>
         <li><a href='#viewsessions'>View Sessions</a></li>
         <li><a href='#email'>E-mail an Invoice</a></li>
      </ul>
    </div>
  </div>
<!--********************************HEADER**********************************************-->

<!--********************************MAIN**********************************************-->
  <div data-role="main" class="ui-content">
    
    <img class="mainlogo" src="/projects/ibill_v3/img/ibill logo.png" alt="iBill Logo" width="250" height="190">
        <section class="registerform">
          <h1>The iBill Invoicing App for Activity Instructors</h1>
          <p>Register below</p>
              <form data-ajax="false" method="POST" action="register.php" >
                <ul>
                  <li>
                    <label for="username">Username</label>
                    <input type="text" name="username" id="username" class="required" minlength="5" placeholder="enter username (min-5 characters)">
                  </li>

                  <li>
                    <label for="firstname">Firstname</label>
                    <input type="text" name="firstname" id="firstname" class="required" minlength="5" placeholder="enter firstname">
                  </li>

                  <li>
                    <label for="username">Surname</label>
                    <input type="text" name="surname" id="surname" class="required" minlength="5" placeholder="enter surname">
                  </li>

                  <li> 
                    <label for="email">Email</label>
                    <input type="email" name="email" id="email" class="required email" placeholder="yourname@email.com">
                  </li>

                  <li> 
                    <label for="password">Password</label>
                    <input type="password" name="password" id="password" class="required" minlength="6" placeholder="enter password">
                  </li>

                  <li> 
                    <label for="confirmpassword">Confirm Password</label>
                    <input type="password" name="confirmpassword" id="confirmpassword" class="required" minlength="6" placeholder="re-enter password">
                  </li>
                <div id="registerbutton">
                      <input type="submit" value="Register">
                </div>
                </ul>
              </form>
        </section>

  </div>
<!--********************************MAIN**********************************************-->

<!--********************************FOOTER**********************************************-->
  <div data-role="footer">
    <footer class="footer">
        <p>awilliams&copy;</p>
    </footer>
  </div>
</div>
<!--********************************REGISTER PAGE**********************************************-->
<!--****************************************************************************************-->
 


<!--********************************LOGIN PAGE**********************************************-->
<!--****************************************************************************************-->

<!--********************************HEADER**********************************************-->
<div data-role="page" id="login">
    <div data-role="header" data-id="foo1" data-position="fixed">
    <div class='cssmenu'>
      <ul>
         <li class='active'><a href='#home'>Home</a></li>
         <li><a href='#sessionrecord'>Record a Session</a></li>
         <li><a href='#viewsessions'>View Sessions</a></li>
         <li><a href='#email'>E-mail an Invoice</a></li>
      </ul>
    </div>
  </div>
<!--********************************HEADER**********************************************-->

<!--********************************MAIN**********************************************-->
  <div data-role="main" class="ui-content">
    
    <img class="mainlogo" src="/projects/ibill_v3/img/ibill logo.png" alt="iBill Logo" width="250" height="190">
        
        <section class="loginform">
              <form action="form.php" method="post"> 
                  <ul>
                    <li><label for="usermail">Email</label>
                      <input type="email" name="usermail" placeholder="yourname@email.com" class="required"></li>
                    <li><label for="password">Password</label>
                      <input type="password" name="password" placeholder="enter password"  minlength="6"></li>
                    <li>
                    <div id="loginbutton">
                      <input type="submit" value="Login"></li>
                    </div>
                    <div id="registerbutton">
                      <input type="submit" value="Register"></li>
                    </div>
                  </ul>
              </form>
        </section>

  </div>
<!--********************************MAIN**********************************************-->

<!--********************************FOOTER**********************************************-->
  <div data-role="footer">
    <footer class="footer">
        <p>awilliams&copy;</p>
    </footer>
  </div>
</div>
<!--********************************LOGIN PAGE**********************************************-->
<!--****************************************************************************************-->

<!--********************************HOME PAGE**********************************************-->
<!--***************************************************************************************-->

<!--********************************HEADER**********************************************-->
<div data-role="page" id="home">
    <div data-role="header" data-id="foo1" data-position="fixed">
    <div class='cssmenu'>
     <ul>
        <li class='active'><a href='#home'>Home</a></li>
           <li><a href='#sessionrecord'>Record a Session</a></li>
           <li><a href='#viewsessions'>View Sessions</a></li>
           <li><a href='#email'>E-mail an Invoice</a></li>
     </ul>
    </div>
    </div><!-- /header -->
<!--********************************HEADER**********************************************-->

<!--********************************MAIN**********************************************-->
  <div data-role="main" class="ui-content">
    
    <img class="mainlogo" src="/projects/ibill_v3/img/ibill logo.png" alt="iBill Logo" width="250" height="190">
        
        <section class="maincontent">
                <h1>The iBill Invoicing App for Activity Instructors</h1>
                  <p>iBill is a cross-platform app that allows you track and manage activity sessions on the go.</p>
                  <br> 
                  <p>Record an activity session to get started!</p>
                <div style="text-align:center">
                  <a href="#sessionrecord" class="ui-btn ui-btn-inline ui-mini ui-corner-all ui-shadow">Record a Session</a>
                </div>
        </section>
  </div>
<!--********************************MAIN**********************************************-->

<!--********************************FOOTER**********************************************-->
  <div data-role="footer">
    <footer class="footer">
        <p>awilliams&copy;</p>
    </footer>
  </div>
</div>
<!--********************************FOOTER**********************************************-->

<!--********************************HOME PAGE**********************************************-->
<!--***************************************************************************************-->

<!--********************************RECORD SESSION PAGE************************************-->
<!--***************************************************************************************-->

<!--********************************HEADER**********************************************-->
<div data-role="page" id="sessionrecord">
    <div data-role="header" data-id="foo1" data-position="fixed">
    <div class='cssmenu'>
      <ul>
        <li class='active'><a href='#home'>Home</a></li>
        <li><a href='#sessionrecord'>Record a Session</a></li>
        <li><a href='#viewsessions'>View Sessions</a></li>
        <li><a href='#email'>E-mail an Invoice</a></li>
      </ul>
    </div>
  </div><!-- /header -->
<!--********************************HEADER**********************************************-->

<!--********************************MAIN**********************************************-->
  <div data-role="main" class="ui-content">
    
    <img class="mainlogo" src="/projects/ibill_v3/img/ibill logo.png" alt="iBill Logo" width="250" height="190">
        
        <section class="maincontent">
          <h1>Record a session using the form below</h1>
            <form method="post" action="" id="sessionRecord">
              <fieldset>
                  <select name="typeofactivity" id="typeofactivity" data-native-menu="false">
                    <option>Type of Session</option>
                    <option value="surf">Surf</option>
                    <option value="coast">Coasteer</option>
                    <option value="bodyboard">Bodyboard</option>
                    <option value="climbing">Cornish Challenge</option>
                  </select>
              </fieldset>
              <fieldset>
                  <select name="employer" id="employer" data-native-menu="false">
                    <option>Employer</option>
                    <option value="nac">Newquay Activity Centre</option>
                    <option value="coastline">Coastline Coasteer</option>
                  </select>
              </fieldset>
              <form method="post" action="PHP HERE!">
                  <label for="datetime">Date and Time of Session</label>
                  <input type="datetime-local" data-clear-btn="false" name="datetime" id="datetime" value="">
              </form>
              <form method="post" action="PHP HERE!">
                  <label for="amount">Amount (GBP)</label>
                  <input type="number" data-clear-btn="true" name="amount" id="amount" value="">
              </form>
                <div id="submitbutton">
                  <input type="submit" value="Submit">
                </div>
            </form>

        </section>
  </div>
<!--********************************MAIN**********************************************-->

<!--********************************FOOTER**********************************************-->
  <div data-role="footer">
    <footer class="footer">
        <p>awilliams&copy;</p>
    </footer>
  </div>
</div>
<!--********************************FOOTER**********************************************-->

<!--********************************RECORD SESSION PAGE************************************-->
<!--***************************************************************************************-->

<!--********************************VIEW SESSIONS PAGE************************************-->
<!--***************************************************************************************-->

<!--********************************HEADER**********************************************-->
<div data-role="page" id="viewsessions">
    <div data-role="header" data-id="foo1" data-position="fixed">
    <div class='cssmenu'>
      <ul>
         <li class='active'><a href='#home'>Home</a></li>
         <li><a href='#sessionrecord'>Record a Session</a></li>
         <li><a href='#viewsessions'>View Sessions</a></li>
         <li><a href='#email'>E-mail an Invoice</a></li>
      </ul>
    </div>
  </div><!-- /header -->
<!--********************************HEADER**********************************************-->

<!--********************************MAIN**********************************************-->
  <div data-role="main" class="ui-content">
    
    <img class="mainlogo" src="/projects/ibill_v3/img/ibill logo.png" alt="iBill Logo" width="250" height="190">
        
        <section class="maincontent">
                <!--MAIN CONTENT GOES HERE-->
        </section>
  </div>
<!--********************************MAIN**********************************************-->

<!--********************************FOOTER**********************************************-->
  <div data-role="footer">
    <footer class="footer">
        <p>awilliams&copy;</p>
    </footer>
  </div>
</div>
<!--********************************FOOTER**********************************************-->

<!--********************************VIEW SESSIONS PAGE************************************-->
<!--***************************************************************************************-->


<!--********************************EMAIL AN INVOICE PAGE************************************-->
<!--***************************************************************************************-->

<!--********************************HEADER**********************************************-->
<div data-role="page" id="email">
    <div data-role="header" data-id="foo1" data-position="fixed">
    <div class='cssmenu'>
      <ul>
         <li class='active'><a href='#home'>Home</a></li>
         <li><a href='#sessionrecord'>Record a Session</a></li>
         <li><a href='#viewsessions'>View Sessions</a></li>
         <li><a href='#email'>E-mail an Invoice</a></li>
      </ul>
    </div>
  </div><!-- /header -->
<!--********************************HEADER**********************************************-->

<!--********************************MAIN**********************************************-->
  <div data-role="main" class="ui-content">
    
    <img class="mainlogo" src="/projects/ibill_v3/img/ibill logo.png" alt="iBill Logo" width="250" height="190">
        
        <section class="maincontent">
                <!--MAIN CONTENT GOES HERE-->
        </section>
  </div>
<!--********************************MAIN**********************************************-->

<!--********************************FOOTER**********************************************-->
  <div data-role="footer">
    <footer class="footer">
        <p>awilliams&copy;</p>
    </footer>
  </div>
</div>
<!--********************************FOOTER**********************************************-->

<!--********************************EMAIL AN INVOICE PAGE************************************-->
<!--***************************************************************************************-->

</body>
<?php

$servername = "localhost";
$username = "root";
$password = "cornwall";

$con=mysqli_connect('localhost','root','cornwall','ibill');
// This code creates a connec'tion to the MySQL database in PHPMyAdmin named 'ibill':

if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// The connection is then checked, if it fails, an echo is sent back to the page stating a connection error.

$username =  $_POST['username'];
$firstname =  $_POST['firstname'];
$surname =  $_POST['surname'];
$emailaddress =  $_POST['emailaddress'];
$password =  $_POST['password'];
$confirmpassword =  $_POST['confirmpassword'];
//These are the different PHP variables that store my posted data.

$register="INSERT INTO user_details (username, firstname, surname, emailaddress, password,) VALUES ('$username', '$firstname', '$surname', '$emailaddress', '$password')";
echo $register;
$result=mysqli_query($con, $register);
//This is the query that will be sent to the MySQL server.

header('Location: http://localhost/projects/ibill_v3/html/main.html#login');
exit();
//This directs the user to the relevant page after successful registration.

?>
Isaac Bennetch
  • 11,830
  • 2
  • 32
  • 43
asharoo85
  • 49
  • 10

1 Answers1

0

First of all, read this article about SQL injection: How can I prevent SQL injection in PHP?.

There is an error in you SQL query. In your PHP file, replace this line:

$result=mysqli_query($con, $register);

with this:

if(mysqli_query($con,$register) === FALSE){
    echo mysqli_error($con);
}

To display errors in PHP code, add these lines:

/** Error reporting */
error_reporting(E_ALL);
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);

Ref: How do I get PHP errors to display?

Community
  • 1
  • 1
Gjermund Dahl
  • 1,410
  • 17
  • 25