Pamitenot Posted June 28, 2009 Share Posted June 28, 2009 Hi all, I'm working on a project but the function that I want doesn't work. There is this button called "add student" and a new row will appear when I clicked that button. However the data I wrote in the previous first row will disappear when I click that button for a 2nd row. Here is the code <?php session_start(); if (isset($_POST['AddStudent'])) { // If user click on Add new student button $temp = $_SESSION['numberOfStudents']; $temp = $temp + 1; $_SESSION['numberOfStudents'] = $temp; } /*else if (isset($_POST['FinalCheckBtn'])) { // If user click on Final Check button go to Final check page header('Location: http://localhost/SJAA/final_check.php') ; }*/ else { // if User enter this page the first time from "create new class page " // the variable that hold the credentials to connect to the database $dbserver = "localhost"; $dbuser = "root"; $dbpassword = ""; $dbname = "fyp - sem 1"; // connect to DB mysql_connect($dbserver, $dbuser, $dbpassword) or die(mysql_error()); // select the correct db. mysql_select_db($dbname) or die(mysql_error()); // Read variables from the $_POST array $class_name = $_POST['class_name']; $class_venue = $_POST['class_venue']; $date_of_lesson =$_POST['date_of_lesson']; $time_of_lesson =$_POST['time_of_lesson']; $class_duration =$_POST['class_duration']; $date_of_exam =$_POST['date_of_exam']; $class_in_charge =$_POST['class_in_charge']; $contact =$_POST['contact']; $email =$_POST['email']; // Prepare the query $sql = "INSERT INTO b_class (class_name,class_venue,date_of_lesson,time_of_lesson,class_duration,date_of_exam,class_in_charge,contact,email) VALUES ('$class_name','$class_venue','$date_of_lesson','$time_of_lesson','$class_duration','$date_of_exam','$class_in_charge','$contact','$email')"; // Execute the query mysql_query($sql) or die(mysql_error()); $_SESSION['numberOfStudents'] = 0; $_SESSION['class_name']=$_POST['class_name']; $_SESSION['class_venue'] = $_POST['class_venue']; $_SESSION['date_of_lesson'] = $_POST['date_of_lesson']; $_SESSION['time_of_lesson'] = $_POST['time_of_lesson']; $_SESSION['class_duration'] = $_POST['class_duration']; $_SESSION['date_of_exam'] = $_POST['date_of_exam']; $_SESSION['class_in_charge'] = $_POST['class_in_charge']; $_SESSION['contact'] = $_POST['contact']; $_SESSION['email'] = $_POST['email']; } ?> <html> <form enctype="multipart/form-data" action = 'student_details.php' method='post' /> <img style="width: 190px; height: 184px;" alt="" src="images/Picture1.jpg"> <head><title>Students' Particulars</title></head> <h1>Students' Particulars</h1> <h3> Please enter student's particulars. All fields are to be filled. </h3> <table style="text-align: left; width: 333px; height: 144px;" border="1" cellpadding="2" cellspacing="2"> <tbody> <tr> <td><?php echo "Class Name: "; echo $_SESSION['class_name'];?> </td> </tr> <tr> <td><?php echo "Class Venue: "; echo $_SESSION['class_venue'];?> </td> </tr> <tr> <td><?php echo "Date of Lesson: "; echo $_SESSION['date_of_lesson'];?> </td> </tr> <tr> <td><?php echo "Time: "; echo $_SESSION['time_of_lesson'];?> </td> </tr> <tr> <td><?php echo "Class Duration: "; echo $_SESSION['class_duration'];?> </td> </tr> <tr> <td><?php echo "Tentative Date of Exam: "; echo $_SESSION['date_of_exam'];?> </td> </tr> <tr> <td><?php echo "Class-In-Charge: "; echo $_SESSION['class_in_charge'];?> </td> </tr> <tr> <td><?php echo "Contact: "; echo $_SESSION['contact'];?> </td> </tr> <tr> <td><?php echo "Email: "; echo $_SESSION['email'];?> </td> </tr> <body> <table border="1"> <tr> <th>Name</th> <th>NRIC no.</th> <th>Age</th> </tr> <br> <input type="submit" value="Add new Student" name="AddStudent"> </form> <?php $cnt = 0; echo "<form enctype='multipart/form-data' action = 'final_check.php' method='post'>"; while ($cnt < $_SESSION['numberOfStudents']) { $NameCnt = "Name".$cnt; $NRICcnt = "NRICno".$cnt; $AgeCnt = "Age".$cnt; if (isset($_POST[$NameCnt])) { ?> <tr> <td><input type = 'text' name = 'student_name<?php echo $cnt ?>' value='<?php echo $_POST[$NameCnt] ?>'></td> <td><input type = 'text' name = 'nric<?php echo $cnt ?>' value='<?php echo $_POST[$NRICcnt] ?>'></td> <td><input type = 'text' name = 'age<?php echo $cnt ?>' value='<?php echo $_POST[$AgeCnt] ?>'></td> </tr> <?php } else { ?> <tr> <td><input type = 'text' name = 'student_name<?php echo $cnt ?>'></td> <td><input type = 'text' name = 'nric<?php echo $cnt ?>'></td> <td><input type = 'text' name = 'age<?php echo $cnt ?>'></td> </tr> <?php } $cnt = $cnt +1; } ?> </table> <br> <br> <tr> <td></td> <table style="text-align: left;" <td><input type = 'submit' value = 'Continue' name='FinalCheckBtn'></td> </tr> </table> </form> </html> Link to comment https://forums.phpfreaks.com/topic/163969-need-help-in-php/ Share on other sites More sharing options...
RichardRotterdam Posted June 28, 2009 Share Posted June 28, 2009 Uhm a couple of questions for you. 1. why are you using sessions it doesn't seem you really need it. 2. why are you using enctype="multipart/form-data" I don't see you are using any file upload. 3. What is it that your script is exactly supose to do? To be honest (and hope you don't take it as a offence) I think your script should be rewritten completely. Link to comment https://forums.phpfreaks.com/topic/163969-need-help-in-php/#findComment-865009 Share on other sites More sharing options...
GingerRobot Posted June 28, 2009 Share Posted June 28, 2009 Your issue is with these lines: $NameCnt = "Name".$cnt; $NRICcnt = "NRICno".$cnt; $AgeCnt = "Age".$cnt;/td> There's no POST field with the name "Namexx", "NRICnoxx" or "Agexx" (where xx is some number). You should have 'student_name'.$cnt. Like you do on these lines: <tr> <td><input type = 'text' name = 'student_name<?php echo $cnt ?>' value='<?php echo $_POST[$NameCnt] ?>'></td> <td><input type = 'text' name = 'nric<?php echo $cnt ?>' value='<?php echo $_POST[$NRICcnt] ?>'></td> <td><input type = 'text' name = 'age<?php echo $cnt ?>' value='<?php echo $_POST[$AgeCnt] ?>'></td> </tr> Though it would be muuuch neater to use arrays. Link to comment https://forums.phpfreaks.com/topic/163969-need-help-in-php/#findComment-865035 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.