这是我的xml文件和我的php代码。它修改了有关学生的当前信息。但是,如果我有几个学生怎么办?我只能编辑第一个(John Doe)如何编辑我的第二个学生信息,然后它也会在我的xml文件中更改。我已将这里的代码简化为与之前的帖子相比,因为我以前的代码不起作用。谢谢
<student>
<firstname>John</firstname>
<lasttname>Doe</lasttname>
<student_id>150334</student_id>
<gender>female</gender>
<dob>16-09-94</dob>
<email>Johndoe@gmail.com</email>
<mobilenumber>12345678</mobilenumber>
<address>xxx</address>
<cohort>cohort name</cohort>
<programme>Softwaree Engineering</programme>
<mode>FT</mode>
</student>
<student>
<firstname>firnstmae</firstname>
<lasttname>hahah</lasttname>
<student_id>150500</student_id>
<gender>female</gender>
<dob>24-03-96</dob>
<email>lol@hotmail.com</email>
<mobilenumber>57056242</mobilenumber>
<address>addressname</address>
<cohort>cohort name</cohort>
<programme>web developement</programme>
<mode>FT</mode>
</student>
</students>
<?php
if(isset($_POST['submit'])) {
$data=simplexml_load_file('studentdb.xml');
$data->item->firstname=$_POST['firstname'];
$data->item->lasttname=$_POST['lastname'];
$data->item->gender=$_POST['gender'];
$data->item->dob=$_POST['dob'];
$data->item->email=$_POST['email'];
$data->item->mobilenumber=$_POST['mobilenumber'];
$data->item->address=$_POST['address'];
$data->item->cohort=$_POST['cohort'];
$data->item->programme=$_POST['programme'];
$data->item->mode=$_POST['mode'];
$handle=fopen("studentdb.xml","wb");
fwrite($handle,$data->asXML());
fclose($handle);
}
$data=simplexml_load_file('studentdb.xml');
$fName=$data->item->firstname;
$lName=$data->item->lasttname;
$gender=$data->item->gender;
$dob=$data->item->dob;
$email=$data->item->email;
$mobileNo=$data->item->mobilenumber;
$address=$data->item->address;
$cohort=$data->item->cohort;
$programme=$data->item->programme;
$mode=$data->item->mode;
?>
<?php
echo $fName . " ";
echo $lName . " ";
echo $gender . " ";
echo $dob . " ";
echo $email . " ";
echo $mobileNo . " ";
echo $address . " ";
echo $cohort . " ";
echo $programme . " ";
echo $mode . " ";
?>
<form method="post">
Firstname<input type="text" name="firstname" value="<?php echo $fName;?>" placeholder="firstname" pattern="[A-Z][a-z]+" title="Must start with capital letters!"required/> </br></br>
Lastname<input type="text" name="lastname" value="<?php echo $lName;?>"placeholder="lastname" pattern="[A-Z][a-z]+" title="Must start with capital letters!" required/></br></br>
gender
<input type="radio" name="gender" value="M" value="<?php echo $gender;?>"> Male
<input type="radio" name="gender" value="F" value="<?php echo $gender;?>"> Female<br><br>
dob<input type="date" name="dob" placeholder="dob" value="<?php echo $dob;?>" /> </br></br>
email<input type="text" name="email" placeholder="email" value="<?php echo $email;?>"/></br></br>
mobile No:<input type="text" name="mobilenumber" value="<?php echo $mobileNo;?>" placeholder="mobileno" pattern="5[0-9][0-9][0-9][0-9][0-9][0-9][0-9]" title="Must start with 5 followed by 7 digits" required/></br></br>
address<input type="text" name="address" placeholder="address" value="<?php echo $address;?>" title="Must start with capital letters!"required/></br></br>
cohort<input type="text" name="cohort" placeholder="cohort" value="<?php echo $cohort;?>" required /></br></br>
programme<input type="text" name="programme" placeholder="programme" value="<?php echo $programme;?>" required //></br></br>
mode<input type="radio" name="mode" value="FT" value="<?php echo $mode;?>"> Full-Time
<input type="radio" name="mode" value="PT"> Part-Time<br>
<br>
<input name="submit" type="submit" />
</form>
答案 0 :(得分:0)
尝试对第一个学生使用$data->student[0]
,为第二个学生使用$data->student[1]
(第n个学生为$data->student[n]
),而不是$data->item
。