Undefined variable problem with PHP function
This is because you're using the $pera
variable (which exists only in the global scope) inside a function.
See the PHP manual page on variable scope for more information.
You could fix this by adding global $pera;
within your function, although this isn't a particularly elegant approach, as global variables are shunned for reasons too detailed to go into here. As such, it would be better to accept $pera
as an argument to your function as follows:
function provera($prom, $pera){
if (preg_match("/[0-9\,\.\?\>\.<\"\'\:\;\[\]\}\{\/\!\\\@\#\$\%\^\&\*\(\)\-\_\=\+\`[:space:]]/",$prom)){
echo "Nepravilan unos imena ili prezimina!";
echo $pera;
}
}
Undefined Variable Error While Calling a Function
Add global $con;
to your function. It is a global variable and is not automatically accessible by your function.
Why my variable is undefined inside the function
Have a read about variable scope in the manual
$file and $newfile are global, therfore $newfile cannot be accessed locally in your function. Either move it into the function, pass it in as a parameter or as a last resort declare it as global in the function
How to fix undefined variable in PHP code?
The problem is your method requires a parameter to be given whenever called,
that parameter is $nohp
in your Home controller method ubahdata
.So if you want to solve this problem you either have to pass that parameter when you call that method or remove it.I don't understand the purpose of this variable and looking at your HTML code you passing that variable via POST method so you don't need to pass that as parameter, you can just remove it. This just my understanding.
public function ubahdata()
{
$user['nohp'] = $this->input->post('nohp');
$user['nama'] = $this->input->post('nama');
$user['alamat'] = $this->input->post('alamat');
$query = $this->mahasiswa_model->ubah_model_data($user, $user['nohp']);
}
public function ubah($nohp)
{
$data['mahasiswa'] = $this->mahasiswa_model->ubah_model($nohp);
$this->load->view('home/tampil', $data);
}
Undefined variable : function parameter (PHP)
I would do something like this (note you never want to echo out individual messages for email and password to stop hackers gaining information about which is correct:
session_start();
require_once('../model/pdo.inc.php');
//username and password will contain the posted resulte or FALSE
$username = usernameValidation();
$password = passwordValidation();
if (!$username OR !$password) {
echo 'Invalid username or password!';
die;
}
// PDO Query (SELECT ...)
// function for checking the username validation (not empty & Regex)
function usernameValidation() { // Username as parameter
if (!empty($_POST['username'])) {
$username = strtolower(htmlspecialchars($_POST['username'])); // username will be tranform to lowercase before regex matching
if (preg_match('#^[a-z0-9\.]{5,20}$#', $username)) { // 5 <= username lenght <= 20 in lowercase character to be valid
return $username; // return true when the username is valid
}
}
return FALSE;
}
// function for checking the password validation (not empty & Regex)
function passwordValidation() { // Password as parameter
if (!empty($_POST['password'])) {
$password = htmlspecialchars($_POST['password']); // Protect the password
if (preg_match('#^[a-zA-Z0-9\.-_@$()]{6,10}$#', $password)) { // 6 <= password length <= 10 character to be valid
return $password; // return true when password is valid
}
}
return FALSE;
}
Related Topics
The Post Method Is Not Supported for This Route. Supported Methods: Get, Head. Laravel
Is Include()/Require() with "Side Effects" a Bad Practice
How to Access the PHP.Ini File in Godaddy Shared Hosting Linux
How to Increase Memory Limit for PHP Over 2Gb
Fetching Single Row, Single Column with Pdo
Comma Separated Values in MySQL "In" Clause
Woocommerce - Hide Other Shipping Methods When Free Shipping Is Available
Fix Access Denied for User 'Root'@'Localhost' for PHPmyadmin
Ffmpeg Progress Bar - Encoding Percentage in PHP
Trying to Access Array Offset on Value of Type Bool in PHP 7.4
Remove .PHP Extension (Explicitly Written) for Friendly Url
How to Alias a Table in Laravel Eloquent Queries (Or Using Query Builder)
Why Use $_Server['Php_Self'] Instead of ""
Can You Add an If Statement in Order By
Single Quotes or Double Quotes for Variable Concatenation
Form Input Field Names Containing Square Brackets Like Field[Index]
Calculate the Time When the Sun Is X Degrees Below/Above the Horizon