我希望每个学生在 <id></id> 标签中的值是唯一的,我该如何实现它


I want values within the <id></id> tag unique for each student, how can i achieve it?

<?xml version="1.0"?>
 <students>
        <student>
             <id>1</id>
             <name>xyz</name>
             </student>
        <student>
             <id>1</id>
             <name>abc</name>
        </student>
        </students>

形式.html

<form action="insert.php" method="post">
<p>Name</p>
<input name="name" required><br>
<p>ID</p>
<input name="id" required><br>
<input type="submit" value="submit" name="submit">
</form>

数据.php

<?php

function c_element($e_name,$parent)    
{    
global $xml;    
$node=$xml->createElement($e_name);    
$parent->appendChild($node);    
return $node;    
}
function c_value($value,$parent)
{
 global $xml;
 $value=$xml->createTextNode($value);
 $parent->appendChild($value);
 return $value;
 }
 ?>

插入.php

<?php    
require 'data.php';    
if(isset($_POST['id'])&& isset($_POST['name'])){    
$s_id=$_POST['id'];    
$s_name=$_POST['name'];    
$xml=new DOMDocument("1.0");   
$xml->load("mydata.xml");   
$root=$xml->getElementsByTagName("students")->item(0);   
$student=c_element("student",$root);   
$id=c_element("id",$student);    
c_value("$s_id",$id);   
$name=c_element("name",$student);   
c_value("$s_name",$name);   
$xml->formatOutput=true;   
$xml->save("mydata.xml");   
}   

?>   

我希望每个学生的标签中的值都是唯一的,我该如何实现它?动态它应该添加唯一值.从表单中获取数据.html,将其发布到 insert.php.我在数据中制作了 2 个函数.php用于添加子项和向 IAM 从插入调用的子项添加值.php

尝试使用simplexml函数:

$xml = simplexml_load_file('students.xml');
foreach($xml as $student)
{
    echo "Id: ".$student->id;
    echo "Name: ".$student->name;
}