0

This is my xml file and my php code. It modifies current information about student. however, what if I have several students. I can only edit the first one (John Doe) how can I edit my second student information then it also changes in my xml file. I have simplified the code here compare to my previous post as my previous code are not working. thanks

<student>
         <firstname>John</firstname>
        <lasttname>Doe</lasttname>
        <student_id>150334</student_id>
        <gender>female</gender>
        <dob>16-09-94</dob>

        <email>[email protected]</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>[email protected]</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>
1

1 Answer 1

0

Try to use $data->student[0] for first student and $data->student[1] for second ($data->student[n] for nth student) instead of $data->item.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.