我有3个php文件:第一个带有表单及其验证,第二个将这些值存储在db中,第三个包含域等等。我已经尝试了将所有变量值从第一个传递到第二个php ex:$_POST
,$_Get
和isset()
显示在Save.php
。
但是值是“”或null
(空),因为我在Save.php
中回显它们并且没有任何内容出现。只需为我提供每个变量的消息,如下所示 - Notice: Undefined index: lname in C:\xampp\blablabla\Save.php on line 5
。
连接正常,查询正常,验证正常,表单正常工作。唯一的问题是我打电话
if($valid)
{
header('Location: Save.php');
exit();
}
main.php
<?php
$nombreErr = "";
$nombre = "";
$apellidoErr = "";
$apellido = "";
$motivosErr = "";
$motivos = "";
$idEstudianteErr = "";
$idEstudiante = "";
$segundoAppellidoErr = "";
$segundoAppellido = "";
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
$valid = true;
if (empty($_POST["fname"])) //Nombre Requerido y Solo letras y espacios en blanco
{
$nombreErr = "Nombre requerido";
$valid = false;
}
else
{
$nombre = test_input($_POST["fname"]);
if (!preg_match("/^[a-zA-Z ]*$/",$nombre))
{
$nombreErr = "Solo letras y espacios en blanco";
$valid = false;
}
}
if (empty($_POST["lname"])) //Apellido Requerido y Solo letras y espacios en blanco
{
$apellidoErr = "Apellido Requerido";
$valid = false;
}
else
{
$apellido = test_input($_POST["lname"]);
if (!preg_match("/^[a-zA-Z ]*$/",$apellido))
{
$nombreErr = "Solo letras y espacios en blanco";
$valid = false;
}
}
if (empty($_POST["Slname"])) //Segundo apellido solo letras y espacios en blanco
{
$segundoAppellido = "";
$valid = false;
}
else
{
$segundoAppellido = test_input($_POST["Slname"]);
if (!preg_match("/^[a-zA-Z ]*$/",$segundoAppellido))
{
$segundoAppellidoErr = "Solo letras y espacios en blanco";
$valid = false;
}
}
if (empty($_POST["studentId"])) //Numero estudiante solo numeros y rayas
{
$idEstudianteErr= "";
$valid = false;
}
else
{
$idEstudiante = test_input($_POST["studentId"]);
if (!preg_match("/^[0-9-]*$/",$idEstudiante))
{
$idEstudianteErr = "Solo numeros y \"-\"";
$valid = false;
}
}
if (empty($_POST["Motive"])) //Motivos Requeridos
{
$motivosErr = "Motivo(s) Requerido(s)";
$valid = false;
}
if($valid)
{
header('Location: Save.php');
exit();
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>">
<input type="hidden" name="depName" value="Admisiones">
<input type="hidden" name="personStat" value="Espera">
<class="input1">FirstName :<input type="text" name="fname" value="<?php echo $nombre;?>">
<span class="error">* <?php echo $nombreErr;?></span> <br>
<class="input2">LastName :<input type="text" name="lname" value="<?php echo $apellido;?>">
<span class="error">* <?php echo $apellidoErr;?></span> <br>
<class="input3">Second LastName :<input type="text" name="Slname" value="<?php echo $segundoAppellido; ?>">
<span class="error"><?php echo $segundoAppellidoErr; ?></span> <br>
<class="input4">Student Id :<input type="text" name="studentId" value="<?php echo $idEstudiante;?>">
<span class="error"><?php echo $idEstudianteErr; ?></span> <br>
<select name="Motive[]" multiple="MULTIPLE">
<?php
$sql1="SELECT categoryName, estimationTime FROM Category where depName='Asistencia Economica'";
$result1=mysqli_query($con, $sql1);
while($row1 = mysqli_fetch_array($result1))
{
echo"<option value=".str_replace(" ", "_", $row1['categoryName']) .":". $row1['estimationTime'].">".$row1['categoryName']."</option>";
}
?>
</select>
<span class="error">* <?php echo $motivosErr;?></span> <br>
<input class="button" type="submit" value="Registrate">
</form>
Save.php
<?php
include('Connection.php');
$firstname = isset($_POST['fname']);
$lastname = mysqli_real_escape_string($con, $_POST['lname']);
$secondLastName = mysqli_real_escape_string($con, $_POST['Slname']);
$studentId =mysqli_real_escape_string($con, $_POST['studentId']);
$depNum= mysqli_real_escape_string($con, $_POST['depName']);
$status= $_POST['personStat'];
$mymotives= $_GET['Motive'];
$motivestring= "";
$motiveTime= 0;
echo $firstname . "where are you";
echo $lastname;
echo $secondLastName;
echo $studentId;
echo $depNum;
echo $status;
echo $mymotives;
if (!$con) {
die("Connection failed: " . mysqli_connect_error());
}
$sql ="INSERT INTO List (depName, personName, personLast, secondLast, stuId, personStatus, manyMotive, categoryTime)
VALUES ('$depNum','$firstname','$lastname', '$secondLastName','$studentId', '$status', '$motivestring', '$motiveTime')";
if (mysqli_query($con, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($con);
}
mysqli_close($con);
?>
答案 0 :(得分:1)
这是它无法正常工作的地方:
Header('Location: Save.php');
在这里,你将控制传递给Save.php,,但你没有直接传递任何数据 ....由于它不起作用,我收集到你根本没有传递它。
您可以执行以下任一操作:
如果到目前为止已执行的文件尚未生成任何输出,那么您只需include
文件Save.php:
包括&#34; Save.php&#34;
如果你想直接传递数据而且数量不大,可以把它放在GET字符串中,但要注意,它会在地址栏中以明文形式显示
标题(&#34;位置:Save.php?name = Vitruvio&amp; surname = Pietrisco&amp; age = 67&#34;);
如果您想传递更多数据,可以使用会话。
在第一个文件中:
在session_start(); //第一行
...
$ _ SESSION [&#39; Hello&#39;] =&#34; World&#34;;
在第二个文件(Save.php)中:
在session_start();
回声&#34;你说&#34; 。 $ _SESSION [&#39;你好&#39;];将回应你说世界&#39;
更新:通常,您会将验证放在执行保存的同一文件中。或者,如果要分离源,可以将所有信息包装在数组中,并将其传递给名为Save()的函数。此函数可能是Save.php文件中的唯一函数。现在你可以做到:
$data['firstname'] = $_POST['fname'];
$data['lastname'] = $_POST['lname'];
// Now you could even put $_SESSION['data'] = $data;
// and have it all in the session!
...and also simplify the use of mysql escaping with a cycle:
foreach ($data as $field => $content) {
$data[$field] = mysql_real_escape_string($con, $content);
}
// By the way: IMPORTANT!. mysql_* functions are DEPRECATED and will be REMOVED
// (maybe soon). Use PDO instead! It takes a bit to adapt, but it's worth it.
if ($valid) {
// The file is only included if it is necessary, if data are $valid.
include "Save.php";
save($data);
}