2

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

marc_s
760k186 gold badges1.4k silver badges1.5k bronze badges
asked Apr 28, 2018 at 4:45
0

3 Answers 3

2

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;
 }
answered Apr 28, 2018 at 4:55
Sign up to request clarification or add additional context in comments.

Comments

0

define your test_input() function outside of

if($_SERVER["REQUEST_METHOD"]=="POST"){

and your code

 if($_SERVER["REQUEST_METHOD"]=="POST"){ 

seems miss "}"

answered Apr 28, 2018 at 4:53

Comments

0

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;
 }
?>
marc_s
760k186 gold badges1.4k silver badges1.5k bronze badges
answered Apr 28, 2018 at 4:51

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.