0

I have a form that passes input data to a processing page. This processing form then checks whether the email and user name already exists in the database. If they do, an error is reported, the function I am having difficulty with is, if the error reports nothing then go and execute the sql insert query otherwise echo the error. I can get most of it to work except the insert data to database. Can anyone help me see the error in my code please ?

Processing page :

<?php

session_start();
list($username,$email,$clubname, $hash) = $_SESSION['data'];
unset($_SESSION['data']);

include_once 'db_connect.php';

$usernameErr = $emailErr = "";

$password = $hash;
$databaseErr = 'cannot connect to database';


$query1 = mysqli_query($mysqli, "SELECT * FROM members WHERE email='".$email."'");

if(mysqli_num_rows($query1) > 0){
//  echo 'email already exists';

    $usernameErr = "username already exists";

}else{
    // do something
    if (!mysqli_query($mysqli,$query1))
    {
        die('Error: ' . mysqli_error($mysqli));
    }
}

$query2 = mysqli_query($mysqli, "SELECT * FROM members WHERE username='".$username."'");

if(mysqli_num_rows($query2) > 0){
//  echo 'username already exists';

    $emailErr = "email already exists";

}else{
    // do something
    if (!mysqli_query($mysqli,$query2))
    {
        die('Error: ' . mysqli_error($mysqli));
    }
}

if ($usernameErr == "" && $emailErr == "" ) {

    $sql = mysqli_query($mysqli, "INSERT INTO members (username, email, password, clubname) VALUES ('$username', '$email', '$password', '$clubname')");

    if (!mysqli_query($mysqli,$sql)) {
        die('Error: ' . mysqli_error($mysqli));
    }
    echo "1 record added";
} 
else {
    echo $usernameErr.'<br/><br/>';
    echo $emailErr.'<br/><br/>';
}
5
  • Get rid of the $mysqli, in if (!mysqli_query($mysqli,$sql)) { you're already passing DB connection to your query $sql = mysqli_query($mysqli, "INSERT... Commented May 26, 2014 at 13:23
  • ok I have done that but still getting error on the insert to database Commented May 26, 2014 at 13:31
  • Add error reporting to the top of your file(s) error_reporting(E_ALL); ini_set('display_errors', 1); and tell me what the error is. Commented May 26, 2014 at 13:33
  • this the error that comes up :- Warning: mysqli_query() expects parameter 2 to be string, object given in /home/sites/thamesvalleycoalition.co.uk/public_html/testsite/includes/register.inc.php on line 24 Error: Commented May 26, 2014 at 13:35
  • Then try changing $query1 = mysqli_query($mysqli, "SELECT * FROM members WHERE email='".$email."'"); to $query1 = "SELECT * FROM members WHERE email='".$email."'"; while keeping if (!mysqli_query($mysqli,$query1)) Commented May 26, 2014 at 13:44

1 Answer 1

1

I came up with this, a simple skeleton, just edit to suit you. Don't forget to hash the password parameter.

function emailEmailExists($emall) {
   if(mysqli_num_rows($email) >= 1) {
     return 1;
   } else {
     return 0;
   }
 }

function usernameExists($username) {
  if(mysqli_num_rows($username) >= 1) {
    return 1;
  } else {
    return 0;
  }
}


function insertUser($username, $password, $email, $otherField, $otherField, ) {
  if(checkEmailExists($email) == 1) {
    echo 'Email in use!';
  } else if(usernameExists($username) == 1){
    echo 'Username in use!';
  } else {
    $insertUser = mysqli_query($yourQuery);

    if($insertUser) {
      echo 'User created!';
    } else {
      // Give error.
    }
  }
} 

Edit

How do you create you DB connection, remove your credentials and please show.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.