Jump to content

Print Primary Key


xxreenaxx1

Recommended Posts

I am using function to insert into database.  But the primary key is automatic and I used

$_SESSION['Tes_ID'] = mysql_insert_id();

to retrieve this. But now that I use function method. I am not sure how to retrieve the primary key on to the next page.

 

$value = modulesql($postVar1, $postVar2, $SessionVar1, $SessionVar2);

$_SESSION['Tes_ID'] = mysql_insert_id();



echo $value, $_SESSION['Tes_ID'];

 

 

<?php
function modulesql($Tes_Name, $Tes_Description, $Use_ID, $Sub_ID){
	$con = OpenConnection();

	mysql_select_db("examination", $con);
	$module = ("INSERT INTO test (`Tes_Name`, `Tes_Description`, `Use_ID`, `Sub_ID`)
	VALUES ($Tes_Name, $Tes_Description, $Use_ID, $Sub_ID)")
	or die('Cannot Execute:'. mysql_error());


CloseConnection($con);
return $module;
}
?>

 

Have I lost you with my question??

Link to comment
https://forums.phpfreaks.com/topic/230319-print-primary-key/
Share on other sites

Thank you for the information.

 

My data was adding before I did function. Now it wont add to my database.  >:(

 

The below code is where the form should be adding to the database.

 

session_start();
include '../Database/connection.php';
include '../Database/test_form_sql.php';
$val= OpenConnection();
mysql_select_db("examination");


$postVar1 = mysql_real_escape_string(trim($_POST['Tes_Name']));
$postVar2 = mysql_real_escape_string(trim($_POST['Tes_Description']));
$SessionVar1 = mysql_real_escape_string(trim($_SESSION['ID']));
$SessionVar2 = mysql_real_escape_string(trim($_SESSION['myselect'])); 

$value = modulesql($postVar1, $postVar2, $SessionVar1, $SessionVar2);



header("location:Add_Question_Form.php");

?>

 

The function method is below.

 

function modulesql($Tes_Name, $Tes_Description, $Use_ID, $Sub_ID){
	$con = OpenConnection();

	mysql_select_db("examination", $con);
	$module = ("INSERT INTO test (`Tes_Name`, `Tes_Description`, `Use_ID`, `Sub_ID`)
	VALUES ($Tes_Name, $Tes_Description, $Use_ID, $Sub_ID)")
	or die('Cannot Execute:'. mysql_error());
	$_SESSION['Tes_ID'] = mysql_insert_id();




CloseConnection($con);
return $module;

 

When I type something in the form  and go through the action code, it does redirect to  header("location:Add_Question_Form.php");, but nothing is added to the database.

Link to comment
https://forums.phpfreaks.com/topic/230319-print-primary-key/#findComment-1186105
Share on other sites

I expect this line

$module = ("INSERT INTO test (`Tes_Name`, `Tes_Description`, `Use_ID`, `Sub_ID`) VALUES ($Tes_Name, $Tes_Description, $Use_ID, $Sub_ID)")

is meant to be

$module = mysql_query("INSERT INTO test (`Tes_Name`, `Tes_Description`, `Use_ID`, `Sub_ID`) VALUES ($Tes_Name, $Tes_Description, $Use_ID, $Sub_ID)")

Link to comment
https://forums.phpfreaks.com/topic/230319-print-primary-key/#findComment-1186111
Share on other sites

My form

 

  <body>

<h3>Test</h3>




<table border="0">
<form method="POST" action="Test_descrip_action.php">
<tr><td>Tes_Name</td><td>:</td><td><input type="text" name="Tes_Name" size="20"></td></tr>
<tr><td>Tes_Description</td><td>:</td><td><input type="text" name="Tes_Description" size="20"></td></tr>

<tr><td> </td><td> </td><td><input type="submit" value="Submit"></td></tr>
</form>
</table>

</body>
</html>

 

I am posting this to the blow code page

 

$value = modulesql($_POST['Tes_Name'], $_POST['Tes_Description'], $_SESSION['ID'], $_SESSION['myselect']);

header("location:Add_Question_Form.php");

 

My function code

 

function modulesql($TestName, $TestDescription, $UserID, $SubjID){
	$con = OpenConnection();


	$module = mysql_query("INSERT INTO test (Tes_Name, Tes_Description , `Use_ID`, `Sub_ID`)
	VALUES ($TestName, $TestDescription, $UserID, $SubjID)")
	or die('Cannot Execute:'. mysql_error());
	$_SESSION['Tes_ID'] = mysql_insert_id();




CloseConnection($con);
return $module;

 

Thats all I am working with. And the connection code. Which works.

 

 


Link to comment
https://forums.phpfreaks.com/topic/230319-print-primary-key/#findComment-1186152
Share on other sites

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.