Undefined variable in php sign up tutorial - php

I am following a tutorial on how to create a sign up page in php, but I keep getting an empty input error when I type results into the sign up section. I think that is because I am getting an error that my $result variable is undefined. I have re-watched the tutorial many times to find any typos but have ended up just exhausting myself.
This is my .signup.inc.php file
<?php
// no ending tag when only php code
if (isset($_POST["submit"])) {
//$ signifies a variable
$name = $_POST["name"];
$email = $_POST["email"];
$username = $_POST["uid"];
$pwd = $_POST["pwd"];
$pwdrepeat = $_POST["pwdrepeat"];
require_once 'dbh.inc.php';
require_once 'functions.inc.php';
if(emptyInputSignup($name, $email, $username, $pwd, $pwdrepeat) !== false) {
header("location: ../signup.php?error=emptyinput");
exit();
}
if(invalidUid($username) !== false) {
header("location: ../signup.php?error=invaliduid");
exit();
}
if(invalidEmail($email) !== false) {
header("location: ../signup.php?error=invalidemail");
exit();
}
if(pwdMatch($pwd, $pwdRepeat) !== false) {
header("location: ../signup.php?error=passwordsdontmatch");
exit();
}
if(Uidexists($conn, $username, $email) !== false) {
header("location: ../signup.php?error=usernametaken");
exit();
}
createUser($conn, $name, $email, $username, $pwd);
}
else {
header("location: ../signup.php");
exit();
}
This is the start of my functions.inc.php file where the undefined $result occurs.
function emptyInputSignup($name, $email, $username, $pwd, $pwdrepeat) {
$result;
if(empty($name) || empty($email) || empty($username) ||
empty($pwd) || empty($pwdrepeat)) {
$result = true;
}
else {
$result = false;
}
return $result;
}

can you just remove the first $result in the function and check again.
function emptyInputSignup($name, $email, $username, $pwd, $pwdrepeat) {
if(empty($name) || empty($email) || empty($username) ||
empty($pwd) || empty($pwdrepeat)) {
$result = true;
}
else {
$result = false;
}
return $result;
}
also you can do it shorthand
function emptyInputSignup($name, $email, $username, $pwd, $pwdrepeat) {
return (empty($name) || empty($email) || empty($username) ||
empty($pwd) || empty($pwdrepeat)) ?? false;
}

Related

How to declare an empty variable inside function in PHP

It sound a Stupid question but I don know what is the problem each time i try to declare a variable inside a function it gives me an error !
function emptyInputSignup($name, $email, $password, $confirm_password)
{
$result;
if (empty($name) || empty($email) || empty($password) || ($confirm_password)) {
$result = true;
} else {
$result = false;
}
return $result;
}
I've tried to declare it and i got an error.
You don't declare variables in PHP, you define them.
There's no need for $result; and it actually means "read the value of the variable $result and then do nothing with it" - which will give you an undefined variable error if $result isn't defined (which, in your case, it is).
What you have to do instead is initialize $result to some value, or leave it out completely:
$result = false;
For example.
Your code can be greatly simplified by just using assigment:
$result = empty($name) || empty($email) || empty($password) || ($confirm_password);
No need for the if. And you can actually ditch the $result variable entirely:
function emptyInputSignup($name, $email, $password, $confirm_password) {
return empty($name) || empty($email) || empty($password) || ($confirm_password);
}
You can initialize a variable like this:
$result = NULL;
You can try using any of the following:
{
$result = false;
if (empty($name) || empty($email) || empty($password) || ($confirm_password)) {
$result = true;
}
return $result;
}
// or
{
if (empty($name) || empty($email) || empty($password) || ($confirm_password)) {
return true;
}
return false;
}
// or
{
return (empty($name) || empty($email) || empty($password) || ($confirm_password));
}
All these constructs produce the same result.

Error connecting to mySQL: SQLSTATE[HY093]: Invalid parameter number

I've searched through multiple threads, but I don't see exactly where is the problem in my case.
It is showing the error on line 30: $findU->bindParam(':uid', $uid);. Not really sure if that's the real problem here.
Full code:
<?php
try {
$pdo = new \PDO('mysql:host=localhost;dbname=project;charset=utf8', 'root', '', [
PDO::ATTR_EMULATE_PREPARES => false,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
]);
if($_POST && isset($_POST['submit'])) {
$first = trim($_POST['first']);
$last = trim($_POST['last']);
$email = trim($_POST['email']);
$uid = trim($_POST['uid']);
$pwd = trim($_POST['pwd']);
//checks if there's an empty field
if(empty($first) || empty($last) || empty($email) || empty ($uid) || empty ($pwd)) {
header('Location: sign_up.php?signup=empty');
exit();
} else {
//checks inputs for invalid symbols through Regex
if(!preg_match("/^[a-zA-Z]/", $first) || !preg_match("/^[a-zA-Z]/", $last)) {
header('Location: sign_up.php?signup=invalid');
exit();
} else {
//checks if the email is in valid format
if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
header('Location: sign_up.php?signup=invalidemail');
exit();
} else {
//checks if the username is already in use
$findU = $pdo->prepare("SELECT * FROM `users` WHERE user_uid = ':uid'");
$findU->bindParam(':uid', $uid);
$result = $findU->execute();
if($result->fetchColumn() > 0) {
header("Location: sign_up.php?signup=usertaken");
exit();
} else {
//creating hash for password
$hashedPwd = password_hash($pwd, PASSWORD_DEFAULT);
//inserts new users into DB
$newUser = $pdo->prepare("INSERT INTO `users` (user_first, user_last, user_email,
user_uid, user_pwd) VALUES (':first', ':last', ':email', ':uid', ':hashedPwd')");
$newUser->execute(array(':first' => $first, ':last' => $last, ':email' => $email,
':uid' => $uid, ':hashedPwd' => $hashedPwd));
header("Location: sign_up.php?signup=success");
exit();
}
}
}
}
}
} catch(\PDOException $e) {
echo "Error connecting to mySQL: " . $e->getMessage();
echo "<code><pre>".print_r($e)."</pre></code>";
exit();
}
?>

header and redirect in php

my problem is in my add-post page when i submit redirect isnt done and header()seems doesnt work
public function insert($query){
$insertRecord = $this->link->query($query) or die($this->link->error.__LINE__);
if($insertRecord){
header("Location:admin/index.php?msg=".urlencode('Record Added'));
exit();
} else {
die("Error : (".$this->link->errno.')'.$this->link->error);
}
}
if($title == "" || $body == "" || $category == "" || $author == ""){
$error = "please fill all required fields";
} else {
$query = "insert into posts(category,title,body,author,tags) values ('$category','$title','$body','$author','$tags')";
$insertRecord = $db->insert($query);
}
header('Location: admin/index.php?msg='.urlencode('Record Added'));

PHP - if conditional statement skipped unless an else is specified

I created a very simple login page where I validate the username and password on PHP after submitting the form.
My first step in the validation is to check that the username and password are not empty, if empty I send the user back to the login page:
$e = $_POST['email'];
$p = $_POST['password'];
if($e == '' || $p == '' || is_null($e) || is_null($p)){
header('Location: login.php?e=missing');
}
it turns out that the if statement that checks if the username and password are empty only works if I add an else statement:
$e = $_POST['email'];
$p = $_POST['password'];
if($e == '' || $p == '' || is_null($e) || is_null($p)){
header('Location: login.php?e=missing');
}else{
//more validation
}
To be honest, my code works, I added the else and problem solved, but WHY???
Thanks.
$e = $_POST['email'];
$p = $_POST['password'];
if(trim($e) == '' || trim($p) == ''){
header('Location: login.php?e=missing');
exit(); // add this else it wont leave the page!
}else{
//more validation
}
if($e == '' || $p == ''){
header('Location: login.php?e=missing');
}
You need to add an exit() after the header line; otherwise, processing continues as normal and all the rest of the code is run.
you need to use empty to check if the variables are set, as well as empty.
$e = $_POST['email'];
$p = $_POST['password'];
if( empty($e) || empty($p) ) {
header('Location: login.php?e=missing');
exit;
}
else {
//more validation
}
$e = $_POST['email'];
$p = $_POST['password'];
if(!empty($e) && !empty($p))
{
header('Location: login.php?e=missing');
exit();
}
else
{
//more validation
}
if($e == '' || $p == ''){
header('Location: login.php?e=missing');
}
rathen then that use :
if(!isset($e) || !isset($p)){
header('Location: login.php?e=missing');
exit();
}
It is happens because of white space or you can use trim() function to remove white space and use exit(); after header its mandatory*

FILTER_VALIDATE_EMAIL not working

I certainly must be missing something here. For some reason filter_var is not working. I'm trying to validate an email from $_POST, and it returns false even with valid emails. But, when I hardcode an email, it works fine. What's wrong?
Here's my php code:
function redirect() { //redirecting to home page function. Used in one of the lectures.
$host = $_SERVER["HTTP_HOST"];
$path = rtrim(dirname($_SERVER["PHP_SELF"]), "/\\");
header("Location: http://$host$path/index.php");
exit;
}
try
{
$dbh = new PDO($db, $dbuser, $dbpassword);
}
catch (PDOException $e)
{
echo "Connection failure: " . $e->getmessage();
}
if (!isset($_POST['email']) || !isset($_POST['password1']) || !isset($_POST['password2'])) {
redirect();
}
$password1 = htmlspecialchars($_POST['password1']);
$email = htmlspecialchars($_POST['email']);
$password2 = htmlspecialchars($_POST['password2']);
//preg_match('/.+#.+\./', $email) == FALSE
if ($email = "") {
print "email not there";
} elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
print "not real email";
} elseif (strlen($password1) < 6) {
print("password too small");
} elseif (!(preg_match('/[A-Za-z].*[0-9]|[0-9].*[A-Za-z]/', $password1))) {
print "numbers and letters plz";
} elseif ($password1 != $password2) {
print "passwords not same";
//redirect();
}
Change the first email check:
if ($email == "") {
print "email not there";
}
It is getting the value " instead of checking for it.

Categories