0

Hi I'm very very new to php and playing with codes to learn. I have a very basic login form that has username and password input and two input buttons one to create entries in the database and other to read from database.

JsFiddle: https://jsfiddle.net/e93kcpto/

My sign up and read functions are located in functions.php file

function signUpFunct(){

if(isset($_POST['username']) and isset($_POST['password'])){
    $username = $_POST['username'];
    $password = $_POST['password'];

    $connection = mysqli_connect('localhost','root','','loginPage');

    if($connection){
       $query = "INSERT INTO users(username,password) VALUES ('$username','$password')";
       if($query){
            mysqli_query($connection,$query);   
       }
       else{
           die("Sign Up Failed");
       }
   }
   else{
       die("Failed to Connect Database");
   }
} 

}

function readAll(){
    $connection = mysqli_connect('localhost','root','','loginPage');

    if($connection){
       $query = "SELECT * FROM users";
       if($query){
           $result = mysqli_query($connection,$query); 
           while($row = mysqli_fetch_assoc($result)){

               ?>
               <pre>
                   <?php print_r($row);?>
               </pre>
               <?php
           }
       }
       else{
           die("Sign Up Failed");
       }
   }
   else{
       die("Failed to Connect Database");
   }

} and this is how I call these two functions in the functions.php file(I'm not sure if this is a good approach so if there is a better way please let me know)

if(isset($_POST['signUp'])){
    signUpFunct();
}
else if(isset($_POST['display'])){
    readAll();
}

both functions work fine but the problem is when I click on the display button in the browser I am at http://localhost.../functions.php

what I would like to do is to display the data on the form page when I click on the Display button. How can I do this?

Asım Gündüz
  • 1,257
  • 3
  • 17
  • 42
  • Your code is not safe! Please read this too to prevent SQL Injections: http://stackoverflow.com/a/60496/5229859 – Tom V. Oct 21 '15 at 12:39
  • I know, these are the baby steps, I still need to learn more things before going there, thanks =) – Asım Gündüz Oct 21 '15 at 12:42
  • But anyway, Your form is on index.php or something? Use php include to include the functions.php file. Then call the functions you made in functions.php in your index.php. (Change to action parameter on form to index.php). – Tom V. Oct 21 '15 at 12:43

0 Answers0