Jump to content

Leaderboard


Popular Content

Showing most liked content on 10/22/2017 in all areas

  1. 1 point
    The reason why your code isn't working is because $con is not defined in those functions (and $pass and $conf_pass are not defined in newUser either). You can use global variables inside the function, but it's better to avoid globals and pass in any variables you want the functions to use. So you should pass $con to the functions. You should add this to the top of your code: ini_set('display_errors', 1); error_reporting(E_ALL); You should switch your database code to use prepared statements instead of trying to manually escape everything. Prepared statements offer better security. You can use prepared statements with mysqli, but it's better to use PDO. The PDO code is shorter and easier to understand, and PDO works with databases other than only MySQL. e.g.: $stmt = $pdo->prepare("SELECT * FROM users WHERE username = ?"); if ($stmt->execute(array($username))) { if ($row = $stmt->fetch()) { // it found a user } } http://php.net/manual/en/book.pdo.php For the code I posted above, I was saying that each function should only do one thing. Your newUser function should only validate and add the user if necessary, it should not also output anything or redirect. Your main code should do that based on what the function returned. If you want to return multiple values you can use an array like I did above to return a "success" value for whether the user was added, and a message to show them. So your main code would call that function and then decide what to do. Those header redirects wouldn't work though, because you sent output to the browser first. $result = newUser(...); if ($result['success']) { // maybe redirect to a thank-you page or something } else { // show the error in $result['message'] }
  2. 1 point
    The forum completely wiped out my reply when I tried to edit my post, because the forum software is awesome. I'll try that again.
  3. 1 point
    function newUser ($username, $password){ $return = ['success' => false, 'message' => '']; if (!isExist($username)) { if (passChk($pass, $conf_pass)){ // insert into the database here $return['message'] = 'Username Created successfully'; $return['success'] = true; } else { $return['message'] = "Password and confirmed password do not match"; } } else { $return['message'] = 'Username already exists'; } return $return; }
×