xxreenaxx1 Posted March 11, 2011 Share Posted March 11, 2011 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?? Quote Link to comment Share on other sites More sharing options...
HuggieBear Posted March 11, 2011 Share Posted March 11, 2011 $_SESSION is a super global, it exists in the global namespace which means it can be accessed from anywhere inside the script, regardless of whether in a function or not. Quote Link to comment Share on other sites More sharing options...
xxreenaxx1 Posted March 11, 2011 Author Share Posted March 11, 2011 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. Quote Link to comment Share on other sites More sharing options...
HuggieBear Posted March 11, 2011 Share Posted March 11, 2011 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)") Quote Link to comment Share on other sites More sharing options...
xxreenaxx1 Posted March 11, 2011 Author Share Posted March 11, 2011 I am getting an error message Cannot Execute:Unknown column 'blah blah' in 'field list' That Column is Tes_Name. Quote Link to comment Share on other sites More sharing options...
HuggieBear Posted March 11, 2011 Share Posted March 11, 2011 Can you actually paste the full error? Quote Link to comment Share on other sites More sharing options...
xxreenaxx1 Posted March 11, 2011 Author Share Posted March 11, 2011 the is the Full error Quote Link to comment Share on other sites More sharing options...
HuggieBear Posted March 11, 2011 Share Posted March 11, 2011 OK, in that case can you post your full code. You must have 'blah blah' specified somewhere. Quote Link to comment Share on other sites More sharing options...
xxreenaxx1 Posted March 11, 2011 Author Share Posted March 11, 2011 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. Quote Link to comment Share on other sites More sharing options...
HuggieBear Posted March 11, 2011 Share Posted March 11, 2011 You must have specified a column called 'blah blah' somewhere, or that isn't the exact error. Quote Link to comment Share on other sites More sharing options...
xxreenaxx1 Posted March 11, 2011 Author Share Posted March 11, 2011 when I type something in my form like Tes_Name= "blah blah", then its giving me that error.. and when i type in Tes_Name = Tes_Name and Tes_Description = Tes_Description, my sessions are added to the table but test name and test description field are empty Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted March 11, 2011 Share Posted March 11, 2011 Any string values in your query need to be quoted. . . . `Sub_ID`) VALUES ('$Tes_Name', '$Tes_Description', '$Use_ID', '$Sub_ID')" Quote Link to comment 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.