heshan Posted August 26, 2012 Share Posted August 26, 2012 Hi All, I want to add parent details for each student. Therefore i have created a separate form to enter data (studentRegistrationFormDetails.php) and it will redirect to the studentRegistrationParentDetailsDatabase.php page. The problem is the admission number comes as "1" even though a user enter different admission number such as "2012/1." When a user enters the second record there was an error message showing "duplicate primary key". Can anyone help me out regarding this? parent_info (admission_no, ---) admission number is set as the primary key. studentRegistrationFormDetails.php <form id="form1" name="form1" method="post" action="studentRegistrationParentDetailsDatabase.php"> <?php $admission_no=$_POST['admission_no']; ?> <input type="checkbox" name="checkbox" id="checkbox" value="father" onclick="father()"/> <label>Fathers details</label></td> <td> </td> <td> </td> <td> </td> <td> </td> </tr> <tr> <td> </td> <td><label>Name </label> </td> <td> <input type="text" name="Name1" id="textfield" disabled="disabled" /> </td> <td> </td> <td> </td> <td> </td> </tr> <tr> <td> </td> <td><label>Occupation </label> </td> <td> <input type="text" name="Occupation1" id="textfield2" disabled="disabled" /> </td> <td> </td> <td> </td> <td> </td> </tr> <tr> <td> </td> <td><label>Office Address</label> </td> <td> <textarea name="Office_Address1" id="textarea" cols="45" rows="5" disabled="disabled"></textarea> </td> <td> </td> <td> </td> <td> </td> </tr> <tr> <td> </td> <td><label>Office Telephone Number</label> </td> <td> <input type="text" name="Office_tel1" id="textfield3" disabled="disabled" /> </td> <td> </td> <td> </td> <td> </td> </tr> <tr> <td> </td> <td><label>Mobile Phone Number</label> </td> <td> <input type="text" name="Mobile_pho1" id="textfield4" disabled="disabled" /> </td> <td> </td> <td> </td> <td> </td> </tr> <tr> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> </tr> <tr> <td> </td> <td> <input type="checkbox" name="checkbox2" id="checkbox2" value="mother" onclick="mother()"/> <label>Mothers details</label></td> <td> </td> <td> </td> <td> </td> <td> </td> </tr> <tr> <td> </td> <td><label>Name </label> </td> <td> <input type="text" name="Name2" id="textfield5" disabled="disabled"/> </td> <td> </td> <td> </td> <td> </td> </tr> <tr> <td> </td> <td><label>Occupation</label> </td> <td> <input type="text" name="Occupation2" id="textfield6" disabled="disabled" /> </td> <td> </td> <td> </td> <td> </td> </tr> <tr> <td> </td> <td><label>Office Address </label> </td> <td> <textarea name="Office_Address2" id="textarea2" cols="45" rows="5" disabled="disabled"></textarea> </td> <td> </td> <td> </td> <td> </td> </tr> <tr> <td> </td> <td><label>Office Telephone Number</label> </td> <td> <input type="text" name="Office_tel2" id="textfield7" disabled="disabled" /> </td> <td> </td> <td> </td> <td> </td> </tr> <tr> <td> </td> <td><label>Mobile Phone Number</label> </td> <td> <input type="text" name="Mobile_pho2" id="textfield8" disabled="disabled" /> </td> <td> </td> <td> </td> <td> </td> </tr> <tr> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> </tr> <tr> <td> </td> <td> <input type="checkbox" name="checkbox3" id="checkbox3" value="guardians" onclick="Guardian()" /> <label>Guardians details</label></td> <td> </td> <td> </td> <td> </td> <td> </td> </tr> <tr> <td> </td> <td><label>Name </label> </td> <td> <input type="text" name="Name3" id="textfield9" disabled="disabled" /> </td> <td> </td> <td> </td> <td> </td> </tr> <tr> <td> </td> <td><label>Occupation </label> </td> <td> <input type="text" name="Occupation3" id="textfield10" disabled="disabled" /> </td> <td> </td> <td> </td> <td> </td> </tr> <tr> <td> </td> <td><label>Office Address </label> </td> <td> <textarea name="Office_Address3" id="textarea3" cols="45" rows="5" disabled="disabled"></textarea> </td> <td> </td> <td> </td> <td> </td> </tr> <tr> <td> </td> <td><label>Office Telephone Number</label> </td> <td> <input type="text" name="Office_tel3" id="textfield11" disabled="disabled" /> </td> <td> </td> <td> </td> <td> </td> </tr> <tr> <td> </td> <td><label>Mobile Phone Number</label> </td> <td> <input type="text" name="Mobile_pho3" id="textfield12" disabled="disabled" /> </td> <td> </td> <td> </td> <td> </td> </tr> <td> <input type="submit" name="button" id="button" value="Save" onClick="return Validate();"/> </td> </tr> <tr> <td> </td> <td><input type="hidden" name="admission_no" value="<?PHP echo $admission_no; ?>" /></td> </tr> </form> studentRegistrationParentDetailsDatabase.php <?php $fstate=isset($_POST["checkbox"]); $mstate=isset($_POST["checkbox2"]); $gstate=isset($_POST["checkbox3"]); $admission_no=isset($_POST['admission_no']); $name1=isset($_POST['Name1']); $occupation1=isset($_POST['Occupation1']); $office_address1=isset($_POST['Office_Address1']); $office_tel1=isset($_POST['Office_tel1']); $mobile_no1=isset($_POST['Mobile_pho1']); $name2=isset($_POST['Name2']); $occupation2=isset($_POST['Occupation2']); $office_address2=isset($_POST['Office_Address2']); $office_tel2=isset($_POST['Office_tel2']); $mobile_no2=isset($_POST['Mobile_pho2']); $name3=isset($_POST['Name3']); $occupation3=isset($_POST['Occupation3']); $office_address3=isset($_POST['Office_Address3']); $office_tel3=isset($_POST['Office_tel3']); $mobile_no3=isset($_POST['Mobile_pho3']); $con=mysql_connect("localhost","root",""); mysql_select_db("student_management",$con); //insert data to database if($fstate=="father") { $query="insert into parent_info values('$admission_no','$fstate','$name1','$occupation1','$office_address1','$office_tel1','$mobile_no1')" ; $result=mysql_query($query) or die (mysql_error()); } if($mstate=="mother"){ $query="insert into parent_info values('$admission_no','$mstate','$name2','$occupation2','$office_address2','$office_tel2','$mobile_no2')" ; $result=mysql_query($query) or die (mysql_error()); } if($gstatee=="guardians") { $query="insert into parent_info values('$admission_no','$gstate','$name3','$occupation3','$office_address3','$office_tel3','$mobile_no3')" ; $result=mysql_query($query) or die (mysql_error()); } header("location:../new student registration/newStudentRegistrationForm.php"); exit(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/267599-problem-arises-when-passing-data-to-the-next-page/ Share on other sites More sharing options...
Psycho Posted August 26, 2012 Share Posted August 26, 2012 Why don't you post the relevant parts of the code instead of making us sift through hundreds of line of code? You know the problem is due to the admission number, so extract the parts that deal with that value. however, I have a couple guesses as to what may be wrong. 1) The field you are using for admission number may be set as an integer in one of the tables. So a value of "2012/1." is getting converted to an integer. 2) If you are using an auto-incrementing id field in one of your tables, your INSERT queries are not going to work since you are simply passing values and not indicating what fields those values are for. $query="insert into parent_info values('$admission_no','$fstate','$name1','$occupation1','$office_address1','$office_tel1','$mobile_no1')" ; If the first field in the table is the auto-incrementing id field, then that query is trying to insert the value of '$admission_no' into the FIRST field of the table. You should explicitly indicate which fields are included in the INSERT query. Quote Link to comment https://forums.phpfreaks.com/topic/267599-problem-arises-when-passing-data-to-the-next-page/#findComment-1372617 Share on other sites More sharing options...
heshan Posted August 26, 2012 Author Share Posted August 26, 2012 @Psycho ; Sorry for the inconvenience. I have posted all the coding because I'm not sure whether i could made some other mistake which could affect this one. Anyway admission number is set as VAR CHAR and it is NOT incremented also..So the issue remains... Quote Link to comment https://forums.phpfreaks.com/topic/267599-problem-arises-when-passing-data-to-the-next-page/#findComment-1372625 Share on other sites More sharing options...
MMDE Posted August 26, 2012 Share Posted August 26, 2012 $query="insert into parent_info values('$admission_no','$fstate','$name1','$occupation1','$office_address1','$office_tel1','$mobile_no1')" ; $query="insert into parent_info values('$admission_no','$mstate','$name2','$occupation2','$office_address2','$office_tel2','$mobile_no2')" ; $query="insert into parent_info values('$admission_no','$gstate','$name3','$occupation3','$office_address3','$office_tel3','$mobile_no3')" ; The problem is the admission number comes as "1" even though a user enter different admission number such as "2012/1." When a user enters the second record there was an error message showing "duplicate primary key". Can anyone help me out regarding this? parent_info (admission_no, ---) admission number is set as the primary key. Is there any chance all three of those queries are used? I mean, if that is the case, then you use the same $admission_no in several different rows, and if it's supposed to be unique (primary key) in the database table, therefor it will complain about duplicate primary key. Quote Link to comment https://forums.phpfreaks.com/topic/267599-problem-arises-when-passing-data-to-the-next-page/#findComment-1372634 Share on other sites More sharing options...
Psycho Posted August 26, 2012 Share Posted August 26, 2012 @Psycho ; Sorry for the inconvenience. I have posted all the coding because I'm not sure whether i could made some other mistake which could affect this one. Anyway admission number is set as VAR CHAR and it is NOT incremented also..So the issue remains... Did you look at the 2nd potential issue I proposed? What are the fields in the parent_info table? Quote Link to comment https://forums.phpfreaks.com/topic/267599-problem-arises-when-passing-data-to-the-next-page/#findComment-1372662 Share on other sites More sharing options...
heshan Posted August 27, 2012 Author Share Posted August 27, 2012 @ Psycho; Yes i checked with that also. But it gives me same error. parent_info (admission_no, state, name, occupation, office_address, office_telephone, mobile_number) $query="INSERT INTO parent_info (admission_no, state, name, occupation, office_address, office_telephone, mobile_number) VALUES('$admission_no','$fstate','$name1','$occupation1','$office_address1','$office_tel1','$mobile_no1')" ; $result=mysql_query($query) or die (mysql_error()); @ MMDE ; Yeah. It is the case. Those 3 queries have been used to enter fathers details, mother details or guardian details with any combination. Then what can i do? Quote Link to comment https://forums.phpfreaks.com/topic/267599-problem-arises-when-passing-data-to-the-next-page/#findComment-1372896 Share on other sites More sharing options...
Psycho Posted August 27, 2012 Share Posted August 27, 2012 OK, you need to take a step back and find out exactly which query is causing the error and what values are being passed in the query. So, change these lines $result=mysql_query($query) or die (mysql_error()); To something like this $result=mysql_query($query) or die ("Line: " . __LINE__ . "<br>Query: $query<br>Error: " . mysql_error()); That will tell you where the query is getting generated from and the actual query that is trying to be run. Also, have you actually checked the HTML source of the FORM page to ensure that the admission number in the hidden field is what you expect? Quote Link to comment https://forums.phpfreaks.com/topic/267599-problem-arises-when-passing-data-to-the-next-page/#findComment-1372911 Share on other sites More sharing options...
heshan Posted August 27, 2012 Author Share Posted August 27, 2012 I have changed the coding and it works ONLY when a user wants to enter one parent (either father, mother or guardian) It does not work when he wants to add both his mother and father or any combination. I have used GET method instead of POST in my form. <?php $admission_no=$_GET['admission_no']; ?> In the page where it passes the data to the database i have used only POST variable instead of isset_POST <?php $fstate=$_POST["checkbox"]; $mstate=$_POST["checkbox2"]; $gstate=$_POST["checkbox3"]; $admission_no=$_POST['admission_no']; $name1=$_POST['Name1']; $occupation1=$_POST['Occupation1']; $office_address1=$_POST['Office_Address1']; $office_tel1=$_POST['Office_tel1']; $mobile_no1=$_POST['Mobile_pho1']; $name2=$_POST['Name2']; $occupation2=$_POST['Occupation2']; $office_address2=$_POST['Office_Address2']; $office_tel2=$_POST['Office_tel2']; $mobile_no2=$_POST['Mobile_pho2']; $name3=$_POST['Name3']; $occupation3=$_POST['Occupation3']; $office_address3=$_POST['Office_Address3']; $office_tel3=$_POST['Office_tel3']; $mobile_no3=$_POST['Mobile_pho3']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/267599-problem-arises-when-passing-data-to-the-next-page/#findComment-1372972 Share on other sites More sharing options...
Psycho Posted August 27, 2012 Share Posted August 27, 2012 So, my assumption is that you have made the 'admission_no' in that table unique. But, you want to be able to insert multiple records. So, obviously that won't work. I can see multiple solutions to your problem: 1. Remove the unique requirement on that field. You can instead check if there is an existing mother record for a 'admission_no' before inserting a new record. You can then decide whether you want to prevent the request outright or instead use it to update the existing record. I assume you want the ability to update records as well, so this makes the most sense to me. 2. You can make the combination of 'admission_no' and 'state'. So it would only throw an error if there is an existing record with the same admission and state. Quote Link to comment https://forums.phpfreaks.com/topic/267599-problem-arises-when-passing-data-to-the-next-page/#findComment-1372983 Share on other sites More sharing options...
heshan Posted August 27, 2012 Author Share Posted August 27, 2012 I have created a combination of admission number and state. It would be easy for me... Thanks everyone who supported me in this thread. Thanks again... Quote Link to comment https://forums.phpfreaks.com/topic/267599-problem-arises-when-passing-data-to-the-next-page/#findComment-1372998 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.