Jump to content

[SOLVED] PHP / SQL - getting the auto_increment variable


HughbertD

Recommended Posts

Hi there,

 

I have a form which adds records to a DB. The information is stored over 2 tables, so as a result I need to add the records primary_key and a foreign key to another table.

 

My problem is that since I am using an auto_increment value for primary key, I don't ever reference the sql variable, e.g. $_POST[$studentID] and so don't know how I go about grabbing this value to put into my second table.

 

My code is posted below.

 

If any one can help at all I would really really appreciate it.

 

	<?php
		$con = mysql_connect("localhost","root","mickey");
		if (!$con)
  			{
  				die('Could not connect: ' . mysql_error());
  			}

		mysql_select_db("jostark", $con);

		$sql="INSERT INTO student(courseID, firstName, lastName, otherName, title, houseNum, streetName, postalTown, city, postCode,  county, country, termHouseNum, termStreetName, termPostalTown, termCity, termPostCode, termCounty, termCountry, phoneNum, mobilePhoneNum, termPhoneNum, email, termEmail,personalEmail, dateOfBirth, gender, facebook, myspace, homeWebsite)
		VALUES
		('$_POST[course]','$_POST[firstname]','$_POST[lastname]','$_POST[othername]','$_POST[title]','$_POST[housenum]', '$_POST[streetname]','$_POST[postaltown]',  '$_POST[city]','$_POST[postcode]', '$_POST[county]', '$_POST[country]', '$_POST[termhousenum]', '$_POST[termstreetname]', '$_POST[termpostaltown]','$_POST[termcity]','$_POST[termpostcode]','$_POST[termcounty]', '$_POST[termcountry]', '$_POST[phonenum]', '$_POST[mobilenum]', '$_POST[termphonenum]','$_POST[email]', '$_POST[termemail]', '$_POST[personalemail]', '$_POST[dobYear]$_POST[dobMonth]$_POST[dobDay]', '$_POST[gender]', '$_POST[facebook]', '$_POST[myspace]', '$_POST[homewebsite]')";

		if (!mysql_query($sql,$con))
  			{
			echo ($sql);
  				die('Error: ' . mysql_error());
  			}
		else


		$con = mysql_connect("localhost","root","mickey");
		if (!$con)
  			{
  				die('Could not connect: ' . mysql_error());
  			}

		mysql_select_db("jostark", $con);

		$sql="INSERT INTO studentplacement(studentID, companyID) values ('$_POST[]', '$_POST[placement]')";


		if (!mysql_query($sql,$con))
  			{
			echo ($sql);
  				die('Error: ' . mysql_error());
  			}
		else




		$url = "http://localhost/joStark/css/addStudent.php";
		$content = file_get_contents($url);

		echo $content; 
		//<!--<html>
		//<body>
		//Record added!<br />
		//<a href="http://localhost/joStark/addStudentPlacement.php">Give the student a placement?</a>
		//<br />
		//<a href="http://localhost/joStark/addStudentFinalTest.php">Go Back to previous page?</a>
		//</body>
		//</html>-->



		mysql_close($con)
	?>

 

 

 

This is not the problem.

 

The first table holds the student's details. (studentID, name, course)

 

The second table must hold the studentID and the companyID as a company can have many students and vice versa.

 

So I can add to the first table fine, I don't reference studentID because like you said it auto increments.

 

But I also want to, at the same time add the studentID and companyID to the second table.

 

What do I use to reference the newly created studentID?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.