我试图将值从一个页面传递到另一个页面,但我不能理解为什么我一直都会收到未定义的索引错误消息。
这是第一页的PHP代码:
<?php
include 'database_connection.php';
$id = $_GET['id'];
$sql="select engineer.id, engineer.team_id, engineer.first_name, engineer.active, engineer.last_name, engineer.role, engineer.region, engineer.phone, to_date, team.team_name, team.manager_name, team.description, team.type, engineer.email from engineer inner join team on engineer.team_id=team.id where active=0 and engineer.team_id > 0 and engineer.id = '".$id."'";
$results = mysqli_query($connection, $sql);
while($row = mysqli_fetch_array($results)) {
$id=$row['id'];
$first_name=$row['first_name'];
$last_name=$row['last_name'];
$role=$row['role'];
$email=$row['email'];
$phone=$row['phone'];
$region=$row['region'];
$type=$row['type'];
?>
<form class="form-horizontal col-sm-12" role="form" method="post" action="../admin/update.php">
<fieldset disabled>
<div class="form-group">
<label for="sso" class="col-sm-2 control-label">SSO ID</label>
<div class="col-sm-10">
<input type="text" id="id" name="id" class="form-control" value="<?php echo $id; ?>">
</div>
</div>
</fieldset>
<div class="form-group">
<label for="name" class="col-sm-2 control-label">Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="name" name="first_name" placeholder="First Name" value="<?php echo $first_name; ?>">
</div>
</div>
<div class="form-group">
<label for="lastname" class="col-sm-2 control-label">Surname</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="name" name="last_name" placeholder="Last Name" value="<?php echo $last_name; ?>">
</div>
</div>
<div class="form-group">
<label for="team" class="col-sm-2 control-label">Team</label>
<div class="col-sm-10">
<select style="width:auto;" class="btn btn-default dropdown-toggle form-control" type="button" name="team_name" value="" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
<?php
//connect to the database
include 'database_connection.php';
//query the database
$sql_team = "SELECT DISTINCT id, team_name FROM team";
//uses $sql_team variable to make a specific query
$query = mysqli_query($connection, $sql_team);
?>
<option value="<?php echo $team_name; ?>"><?php echo $team_name; ?></option>
<?php
//initilises a while loop to retrieve all the rows
while ($row = mysqli_fetch_array($query) )
{
//echos all the distinct catDesc rows into a list
echo "<option value='" . $row['id'] . "' >".htmlspecialchars($row["team_name"])."</option>";
}
?>
</select>
</div>
</div>
<div class="form-group">
<label for="role" class="col-sm-2 control-label">Role</label>
<div class="col-sm-10">
<input type="role" class="form-control" id="role" name="role" placeholder="Role" value="<?php echo $role; ?>">
</div>
</div>
<div class="form-group">
<label for="email" class="col-sm-2 control-label">Email</label>
<div class="col-sm-10">
<input type="email" class="form-control" id="email" name="email" placeholder="example@domain.com" value="<?php echo $email; ?>">
</div>
</div>
<div class="form-group">
<label for="phone" class="col-sm-2 control-label">Phone Number</label>
<div class="col-sm-10">
<input type="phone" class="form-control" id="phone" name="phone" placeholder="Business number" value="<?php echo $phone; ?>">
</div>
</div>
<div class="form-group">
<label for="region" class="col-sm-2 control-label">Region</label>
<div class="col-sm-10">
<input type="region" class="form-control" id="region" name="region" placeholder="Region e.g. South" value="<?php echo $region; ?>">
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<input type="submit" value="Save" class="btn btn-primary">
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<! Will be used to display an alert to the user>
</div>
</div>
</form>
<?php
//closes and stops the loop
}
?>
这是第二页的代码,其中显示错误消息:
<?php
include 'database_connection.php';
$id = mysqli_real_escape_string($connection, $_GET['id']);
$first_name = mysqli_real_escape_string($connection, $_POST['first_name']);
$last_name = mysqli_real_escape_string($connection, $_POST['last_name']);
$team_name = mysqli_real_escape_string($connection, $_POST['team_name']);
$role = mysqli_real_escape_string($connection, $_POST['role']);
$email = mysqli_real_escape_string($connection, $_POST['email']);
$phone = mysqli_real_escape_string($connection, $_POST['phone']);
$region = mysqli_real_escape_string($connection, $_POST['region']);
if ($role == ''){
$role = NULL;
}
if ($email == ''){
$email = NULL;
}
if ($phone == ''){
$phone = NULL;
}
if ($region == ''){
$region = NULL;
}
$sql = "UPDATE people SET id='$id', first_name='$first_name', last_name='$last_name', team_id='$team_name', role='$role', email='$email', phone=$phone', region='$region' where id='$id')";
?>
因此,当我尝试从第一页检索第二页上的ID时,会出现错误。我错过了什么?
答案 0 :(得分:0)
如果要将变量作为get方法传递,可以将其传递给form的action属性
例如:action =“xyz.php?id =”
在第3行,你已经分配了一个$ _GET []变量,你不能在那里分配,因为这些值只在提交表单后设置,所以如果你想删除错误,请像这样编写脚本
if(isset($GET['id']){
$id = $_GET['id'];
}
答案 1 :(得分:0)
使用
<input id="id" type="hidden" value=$id>
并将其读作
$_POST['id']
在第二页