4 Replies - 209 Views - Last Post: 30 December 2017 - 07:20 AM Rate Topic: -----

#1 Bourbia  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 29-December 17

Error when connecting login system to database

Posted 29 December 2017 - 11:44 PM

Hi!

I have recently got into coding and I am having some difficulty with a php script. I followed a youtube guide on how to create and connect a login system to a database. I use a localhost as a database but something goes wrong when i try to signup as a new user on the website i created. When I hit the signup button the site refreshes but no information get sent to my database. And yes I have a seprate tab where wrote information about the database.
The guide I used: https://www.youtube....h?v=xb8aad4MRx8
I think it is something wrong with this part of the script:

 

<?php 
if (isset($_POST['sumbit'])) {
    
   include_once 'dbh.inc.php';

   $first = mysqli_real_escape_string($conn, $_POST['first']);
   $last = mysqli_real_escape_string($conn, $_POST['last']);
   $email = mysqli_real_escape_string($conn, $_POST['email']);
   $uid = mysqli_real_escape_string($conn, $_POST['uid']);
   $pwd = mysqli_real_escape_string($conn, $_POST['pwd']);

   //Error handlers
   //Check for empty fields
   if (empty($first) || empty($last) || empty($email) || empty($uid) || empty($pwd)) {
    header("Location: ../signup.php?signup=empty");
	exit();
   } else {
       //Check if input characters are valid
   	if (!preg_match("/^[a-zA-Z]*$/", $first) || !preg_match("/^[a-zA-Z]*$/", $last)) {
   	
    header("Location: ../signup.php?signup=invalid");
	exit();
} else {
      //Check if email is vaid
   	if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
   		    header("Location: ../signup.php?signup=email");
	        exit();
   	} else {
        $sql = "SELECT * FROM users WHERE user_uid='$uid'";
        $result = mysqli_query($conn, $sql);
        $resultCheck = mysqli_num_rows($result);
  

        if ($resultCheck > 0) {
        	header("Location: ../signup.php?signup=usertaken");
	        exit();
        } else {
            //Hashing the password
            $hashedPwd = password_hash($pwd, 'PASSWORD_ DEFAULT'); 
            //insert the user into the database
            $sql = "INSERT INTO users (user_first, user_last, user_email, user_uid, user_pwd) VALUES ('$first', '$last', '$email', '$uid', '$hashedPwd');";
            mysqli_query($conn, $sql);
            header("Location: ../signup.php?signup=success");
	        exit();
	     }
   }
}
	 
}

} else {
	header("Location: ../signup.php");
	exit();

}

]




Is This A Good Question/Topic? 0
  • +

Replies To: Error when connecting login system to database

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 4140
  • View blog
  • Posts: 13,094
  • Joined: 08-June 10

Re: Error when connecting login system to database

Posted 30 December 2017 - 01:55 AM

what’s the URL you land on?

Note: you never check if any query succeeds ...
Was This Post Helpful? 0
  • +
  • -

#3 Bourbia  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 29-December 17

Re: Error when connecting login system to database

Posted 30 December 2017 - 05:04 AM

The URL doesn't change at all when pressing the sign up button.
The URL is: http://localhost/Log...ion)/signup.php

But the login button seems to work. (Havn't connected it with the database yet. But the URL changes when pressing the login button
From: http://localhost/Log...tion)/index.php
To: http://localhost/Log...d=&pwd=&submit=

If you need the rest of the code to help me, let me know.
Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3721
  • View blog
  • Posts: 13,512
  • Joined: 08-August 08

Re: Error when connecting login system to database

Posted 30 December 2017 - 07:14 AM

Deprecated MySQL functions are simpler to use (not counting all the sanitizing you need to add) than PDO or MySQLi prepared statements, but far less secure. That's why they've been deprecated. If you're just learning PHP you should start with
http://www.dreaminco...duction-to-pdo/
or
http://php.net/manua...-statements.php
When dealing with old code that uses deprecated MySQL functions you should replace them if possible. If it's not possible, your experience with PDO or MySQLi will benefit you when dealing with MySQL functions.

Using Mysqli or PDO without using prepared statements is just as bad as using the deprecated and removed mysql extension. Don't do it.
Was This Post Helpful? 1
  • +
  • -

#5 Bourbia  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 29-December 17

Re: Error when connecting login system to database

Posted 30 December 2017 - 07:20 AM

Alright, thank you alot!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1