Reputation: 542
I have declared a function called test_input in my php script,which working absolutely fine in another scripts.
It shows error
Fatal error: Uncaught Error: Call to undefined function test_input() in C:\xampp\htdocs\submitdata\cwisubmit.php:25 Stack trace: #0 {main} thrown in C:\xampp\htdocs\submitdata\cwisubmit.php on line 25
My php script is:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Database Connected SUCCESSFULLY";
if($_SERVER["REQUEST_METHOD"]=="POST") {
// Variables initialization
$fname=$lname=$dob=$phone=$email=$postcode=$staddress=$city=$lan=$lcwi=$ptype=$mhtype="";
// Retrieving the data from the form
$fname=test_input(mysqli_real_escape_string($conn,$_POST['fname']));
$lname=test_input(mysqli_real_escape_string($conn,$_POST['lname']));
$dob=test_input(mysqli_real_escape_string($conn,$_POST['dob']));
$phone=test_input(mysqli_real_escape_string($conn,$_POST['phone']));
$email=test_input(mysqli_real_escape_string($conn,$_POST['email']));
$postcode=test_input(mysqli_real_escape_string($conn,$_POST['postcode']));
$staddress=test_input(mysqli_real_escape_string($conn,$_POST['staddress']));
$city=test_input(mysqli_real_escape_string($conn,$_POST['city']));
$lan=test_input(mysqli_real_escape_string($conn,$_POST['lan']));
$lcwi=test_input(mysqli_real_escape_string($conn,$_POST['lcwi']));
$ptype=test_input(mysqli_real_escape_string($conn,$_POST['ptype']));
$mhtype=test_input(mysqli_real_escape_string($conn,$_POST['mhtype']));
function test_input($data) {
$data=trim($data);
$data=stripslashes($data);
return $data;
}
//sql insert
$sql="INSERT INTO cwi(fname, lname, dob, phone, email, postcode, staddress, city, lan, lcwi, ptype, mhtype) VALUES('$fname','$lname','$dob','$phone','$email','$postcode','$staddress','$city','$lan','$lcwi','$ptype','$mhtype')";
if($conn->query($sql)===TRUE){
echo "<center>RECORDS UPDATED SUCCESSFULLY</center>";
}else{
echo "Error: ".$sql."<br>".$conn->error;
}
}
$conn->close();
?>
Please check this code whether there is error in my code or why its returning this error
Upvotes: 2
Views: 751
Reputation: 987
Changes are made to the code try this. It should work fine.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}
echo "Database Connected SUCCESSFULLY";
if($_SERVER["REQUEST_METHOD"]=="POST"){
//Variables initaition
$fname=$lname=$dob=$phone=$email=$postcode=$staddress=$city=$lan=$lcwi=$ptype=$mhtype="";
//Retrieving the data from the form
$fname=test_input(mysqli_real_escape_string($conn,$_POST['fname']));
$lname=test_input(mysqli_real_escape_string($conn,$_POST['lname']));
$dob=test_input(mysqli_real_escape_string($conn,$_POST['dob']));
$phone=test_input(mysqli_real_escape_string($conn,$_POST['phone']));
$email=test_input(mysqli_real_escape_string($conn,$_POST['email']));
$postcode=test_input(mysqli_real_escape_string($conn,$_POST['postcode']));
$staddress=test_input(mysqli_real_escape_string($conn,$_POST['staddress']));
$city=test_input(mysqli_real_escape_string($conn,$_POST['city']));
$lan=test_input(mysqli_real_escape_string($conn,$_POST['lan']));
$lcwi=test_input(mysqli_real_escape_string($conn,$_POST['lcwi']));
$ptype=test_input(mysqli_real_escape_string($conn,$_POST['ptype']));
$mhtype=test_input(mysqli_real_escape_string($conn,$_POST['mhtype']));
//sql insert
$sql="INSERT INTO cwi(fname, lname, dob, phone, email, postcode, staddress, city, lan, lcwi, ptype, mhtype) VALUES('$fname','$lname','$dob','$phone','$email','$postcode','$staddress','$city','$lan','$lcwi','$ptype','$mhtype')";
if($conn->query($sql)===TRUE){
echo "<center>RECORDS UPDATED SUCCESSFULLY</center>";
}else{
echo "Error: ".$sql."<br>".$conn->error;
}
}
$conn->close();
function test_input($data)
{
$data=trim($data);
$data=stripslashes($data);
return $data;
}
?>
Upvotes: 0
Reputation: 1382
PHP functions can be called before defining it because PHP parses the file first and then executes it. But here is the twist.
Functions that are enclosed in a conditional statement(if else) will not be parsed. It will be only available after PHP interprets
if
So, You have to move your function outside of IF
block like
if(condition){
//your code
}
//Your function
function test_input($data){
$data=trim($data);
$data=stripslashes($data);
return $data;
}
Upvotes: 2
Reputation: 51
define your test_input() function outside of
if($_SERVER["REQUEST_METHOD"]=="POST"){
and your code
if($_SERVER["REQUEST_METHOD"]=="POST"){
seems miss "}"
Upvotes: 0