Jump to content

Hi, just need help adding data to a linker table.


deansaddigh

Recommended Posts

First off can i please show you my code for adding data to db.

I have done it to the best of my ability but i am wondering what happens if some parts of the sql fail. and some parts dont, then ill have incomplete data in the table.

i have this code after each sql statement.

 

//Execute sql statement 1
if (!mysql_query($student_details,$conn)) {
	die('Error: ' . mysql_error());
}

 

But at the moment i have 4 of them, for the indivdual form parts.

so my question is what happens if one of them fail but the others go through ok?

How can i ensure each sql statement gets entered into the db? and if it doesnt how can i undo the previous insert statement???

 

I hope thats clear but i am terrible at explaining what i mean.

 

 

The other thing i wanted to know was i have linker tables for example.

i have a table called Payment_Student.

 

all thats in the above table is student id and payment id.

 

But i have know idea how i write the student id and payment id to it as the student id is auto incremented in the student table and like wise the payment id is auto incremented in the payment table, how do i had these into the payment_student table.

 

 

Any help would be gratefully appreciated.

 

I have also included my page so you can see what i have done so far.

 

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<?php include("includes/ico.html"); ?>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<?php include("db/connection.php");  //Connection string for db?>
<link rel="stylesheet" href="css/layout.css" type="text/css" media="screen" />
</head>

<body>


<div id="container">
<div id="header">
    	
    </div>

    <div id="sidebar">

	<div id="menu9"><?php include("includes/menu/menu.html"); ?></div>
</div>

    <div id="content"> 

      <div align="center">
        <?php 



// Variable for Student details part of form 
$title=mysql_real_escape_string($_POST['title']);
$First_name=mysql_real_escape_string($_POST['First_name']);
$username=mysql_real_escape_string($_POST['username']);
$Second_name=mysql_real_escape_string($_POST['Second_name']);
$password=mysql_real_escape_string($_POST['password']);
$DOB=mysql_real_escape_string($_POST['DOB']);
$Nationality=mysql_real_escape_string($_POST['Nationality']);
$Tel=mysql_real_escape_string($_POST['Tel']);
$emergency_tel=mysql_real_escape_string($_POST['emergency_tel']);
$email=mysql_real_escape_string($_POST['email']);
$address=mysql_real_escape_string($_POST['address']);
$Proffesion=mysql_real_escape_string($_POST['Proffesion']);
$faxnumber=mysql_real_escape_string($_POST['faxnumber']);
$number_years=mysql_real_escape_string($_POST['years_of_study']);
$smoke=mysql_real_escape_string($_POST['Smoke']);
$Special_requirements=mysql_real_escape_string($_POST['Special_requirements']);
// End of Variables for Student details 

// Sql Query sends all data from the form into the student table 
$student_details="INSERT INTO Student (Title,	First_Name,Username,Surname,Password,Dob,Nationality,Telephone_number,Emergency_number,Email,Address,Proffesion,Years_of_english_already_studied,Smoker,Special_requirements,Fax_number) VALUES ('$title','$First_name','$username','$Second_name','$password','$DOB','$Nationality','$Tel','$emergency_tel','$email','$address','$Proffesion','$number_years','$smoke','$Special_requirements','$faxnumber')"; 
//end of query one 

// Variable for Course details part of form 
$Course_name=mysql_real_escape_string($_POST['Course_name']);
$Course_start_date=mysql_real_escape_string($_POST['Course_start_date']);
$Course_end_date=mysql_real_escape_string($_POST['Course_end_date']);
$Number_of_weeks=mysql_real_escape_string($_POST['Number_of_weeks']);
//end Variables

// Sql Query sends all data from the form into the Course table 
$course_details="INSERT INTO Course (Course_Name,Course_start_date,Course_end_date,Number_of_weeks)
VALUES
('$Course_name','$Course_start_date','$Course_end_date','$Number_of_weeks')";
// end of query

//Variable for Accomodation part of form
$accomodation_type=mysql_real_escape_string($_POST['accomodation_type']);
$accomodation_start_date=mysql_real_escape_string($_POST['accomodation_start_date']);
$accomodation_end_date=mysql_real_escape_string($_POST['accomodation_end_date']);
//end variables

// Sql Query sends all data from the form into the accomodation table 
$accomodation_details="INSERT INTO Accomodation  (Type_of_accommodation,Accom_Start_date,Accom_End_date)
VALUES
('$accomodation_type','$accomodation_start_date','$accomodation_end_date')";
// end of query


//Variable for flight part of form
$airport_transfer=mysql_real_escape_string($_POST['airport_transfer']);
$Arrival_date=mysql_real_escape_string($_POST['Arrival_date']);
$Departure_date=mysql_real_escape_string($_POST['Departure_date']);
$Arrival_flight_no=mysql_real_escape_string($_POST['Arrival_flight_no']);
$Departure_flight_no=mysql_real_escape_string($_POST['Departure_flight_no']);
$Arrival_time=mysql_real_escape_string($_POST['Arrival_time']);
$Departure_time=mysql_real_escape_string($_POST['Departure_time']);
$arrival_airport=mysql_real_escape_string($_POST['arrival_airport']);
$departure_airport=mysql_real_escape_string($_POST['departure_airport']);
//end variables

// Sql Query sends all data from the form into the flight table 
$flight_details="INSERT INTO Flight 
(Arrival_flight_no,departure_flight_no,arrival_date,departure_date,arrival_time,departure_time,Arrival_airport,departure_airport,Transfer_needed)
VALUES
('$Arrival_flight_no','$Departure_flight_no','$Arrival_date','$Departure_date','$Arrival_time','$Departure_time','$arrival_airport','$departure_airport','$airport_transfer')";
// end of query


//Variable for payment part of form
$payment_details=mysql_real_escape_string($_POST['payment_details']);
$payment_type=mysql_real_escape_string($_POST['payment_type']);
$card_type=mysql_real_escape_string($_POST['card_type']);
$card_holders_name=mysql_real_escape_string($_POST['card_holders_name']);
$card_holders_address=mysql_real_escape_string($_POST['card_holders_address']);
$card_number=mysql_real_escape_string($_POST['card_number']);
$card_expiry_date=mysql_real_escape_string($_POST['card_expiry_date']);
$cv_number=mysql_real_escape_string($_POST['cv_number']);

//end variables





//Sql Query sends all data from the form to the payment table

$payment_details="INSERT INTO Payment 
(Deposit_or_full,Payment_type,Card_type,Cardholders_name,Cardholders_address,Card_number,Card_ccv,Expiry_date)
VALUES
('$payment_details','$payment_type','$card_type','$card_holders_name','$card_holders_address','$card_number','$cv_number','$card_expiry_date')";


//end query




//******************execute sql statements******************************* //

//Execute sql statement 1
if (!mysql_query($student_details,$conn)) {
	die('Error: ' . mysql_error());
}
//Execute sql statement 2
if (!mysql_query($course_details,$conn)) {
	die('Error: ' . mysql_error());
}

//Execute sql statement 3
if (!mysql_query($accomodation_details,$conn)) {
	die('Error: ' . mysql_error());
}

//Execute sql statement 4
if (!mysql_query($flight_details,$conn)) {
	die('Error: ' . mysql_error());
}

//Execute sql statement 5
if (!mysql_query($payment_details,$conn)) {
	die('Error: ' . mysql_error());
}
//******************end sql statements******************************* //

echo "<h2>Thanks For submittiing the data to the us, we will be in-touch shortly</h2><br />";




include("db/close_connection.php"); //close db connection
?>
        
        
          </div>
  </div>

<div id="footer">


<?php include("includes/footer.html"); ?>
</div>

</div>


</body>
</html>

Link to comment
Share on other sites

If I understand correctly.......

To make a link between student id and payment id you need something in common.  You could make a new field in each table called student name and that would serve as a common denominator between the two tables.

so....

student id      student name

08934            John Smith

0796              Sue Meeh

 

payment id    student name

Money05        Sue Meeh

Money93        John Smith

 

 

You can then get both the student id and the payment id for a named student.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.