我已经在我的php脚本中声明了一个名为test_input的函数,它在另一个脚本中运行得非常好。
显示错误
致命错误:未捕获错误:在C:\ xampp \ htdocs \ submitdata \ cwisubmit.php中调用未定义函数test_input():25堆栈跟踪:#0 {main}在C:\ xampp \ htdocs \ submitdata \中引发第25行的cwisubmit.php
我的PHP脚本是:
<?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();
?>
请检查此代码我的代码是否有错误或为何返回此错误
答案 0 :(得分:2)
可以在定义PHP函数之前调用它,因为PHP首先解析文件然后执行它。但这是扭曲。
条件语句(if else)中包含的函数将 不被解析。它仅在PHP解释
后才可用if
所以,你必须将你的功能移到IF
块之外,如
if(condition){
//your code
}
//Your function
function test_input($data){
$data=trim($data);
$data=stripslashes($data);
return $data;
}
答案 1 :(得分:0)
对代码进行了更改尝试此操作。它应该工作正常。
<?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;
}
?>
答案 2 :(得分:0)
在
之外定义你的test_input()函数if($_SERVER["REQUEST_METHOD"]=="POST"){
和你的代码
if($_SERVER["REQUEST_METHOD"]=="POST"){
似乎错过了“}”