totxone Posted February 19, 2013 Share Posted February 19, 2013 HI, I have created a form where user can input some values with 2 buttons submit and clear. Then in the same form I have added php code for submit button , if this button is clicked, a seperate php page is called which has some functions written in it. One of the functions in the page include connecting to database, table and command to insert a record from the data entered in the form. However when I run it and click on submit the page just refrehes and nothing is added to my database. Can someone please tell me what could be the problem. Here are the codes for reference. page 1 <html> <head> <title>Enter Session Details Here</title> <script language="javascript" type="text/javascript" src="datetimepicker.js"> //Date Time Picker script </script> <? include("db_ops.php3"); ?> </head> <body> <form method="POST" name="stdFrm" action="sessionDetailsEntry.php3"> <U><B><CENTER>Session Information Entry</CENTER></B></U> <TABLE align="center"> <TR> <TD>Teacher:</TD> <TD><input type=text name="teacherName" size="25" maxlength="25"</TD> </TR> <TR> <TD>Student Name:</TD> <TD><input type=text name="studentName" size="10" maxlength="10" maxlength="10"></TD> </TR> <TR> <TD>Topic Taught:</TD> <TD><input type=text name="topic" size="25" maxlength="25"></TD> </TR> <TR> <TD>Session Date and Time:</TD> <TD><input id="sessionDateTime" type="text" size="25"><a href="javascript:NewCal('sessionDateTime','ddmmyyyy',true, 12)"><img src="cal.gif" width="16" height="16" border="0" ></a></TD> </TR> <TR> <TD>Assignment Score:</TD> <TD><input type=text name="score" size="10" maxlength="10"></TD> </TR> <TR> <TD>Comments:</TD> <TD><textarea name="comments" cols=80 rows=4 maxlength="500"></textarea></TD> </TR> <TR> <TD>Technical Issues:</TD> <TD><textarea name="technicalIssues" cols=80 rows=4 maxlength="300"></textarea></TD> </TR> <TR> <TD>Next Topic:</TD> <TD><input type=text name="nextTopic" size="25" maxlength="25"></TD> </TR> <TR> <TD align="right"><input type="Submit" value="Save"></TD> <TD align="left"><input type="reset" value="Clear"></TD> </TR> </TABLE> </form> <? php if(isset($_POST['Submit'])) { $table_nm = 'session_entry'; add_to_table(table_nm); echo "Back"; } ?> </body> </html> page 2 with functions <html> <body> <?php $link; function add_to_table($table_nm) { connect_to_db(); mysql_select_db('etz', $link); $add_to_table = "INSERT INTO session_entry (teacher_id, student_id, topic, assignment_score, comments, session_issues, next_topic, date, timestamp) VALUES ('$_POST[teacherName]', '$_POST[studentName]', '$_POST','$_POST[score]', '$_POST[comments]', '$_POST[technicalIssues]', '$_POST[nextTopic]', '$_POST[sessionDateTime]', '$_POST[sessionDateTime]')"; echo "Adding the records"; mysql_query ($ad_to_table) or die(mysql_error()); mysql_close($link); } function connect_to_db() { $link = mysql_connect('localhost' , 'root' , '' ); if (!$link) { die('Could not connect: ' . mysql_error()); } echo 'connected successfully' . "\n" ; } ?> TIA Quote Link to comment https://forums.phpfreaks.com/topic/274660-adding-records-to-database/ Share on other sites More sharing options...
P5system Posted February 19, 2013 Share Posted February 19, 2013 which version of php you are using? Quote Link to comment https://forums.phpfreaks.com/topic/274660-adding-records-to-database/#findComment-1413259 Share on other sites More sharing options...
PaulRyan Posted February 19, 2013 Share Posted February 19, 2013 You use $_POST['Submit'] to check for the form being posted, yet you haven't actually set the submit buttons name to "Submit". Try that, or using: if($_SERVER['REQUEST_METHOD'] == 'POST') { //### Process for here } Quote Link to comment https://forums.phpfreaks.com/topic/274660-adding-records-to-database/#findComment-1413304 Share on other sites More sharing options...
Barand Posted February 19, 2013 Share Posted February 19, 2013 mysql_query ($ad_to_table) Quote Link to comment https://forums.phpfreaks.com/topic/274660-adding-records-to-database/#findComment-1413333 Share on other sites More sharing options...
TOA Posted February 19, 2013 Share Posted February 19, 2013 In addition to what Barand and PaulRyan said, add a $ to the variable you're passing into add_to_table() $table_nm = 'session_entry'; add_to_table(table_nm); echo "Back"; Quote Link to comment https://forums.phpfreaks.com/topic/274660-adding-records-to-database/#findComment-1413338 Share on other sites More sharing options...
Barand Posted February 19, 2013 Share Posted February 19, 2013 On closer inspection, $link is local to the connect function and not returned, so it will be undefined when you use it. You should be getting PHP and mysql errors reported. Quote Link to comment https://forums.phpfreaks.com/topic/274660-adding-records-to-database/#findComment-1413350 Share on other sites More sharing options...
totxone Posted February 21, 2013 Author Share Posted February 21, 2013 Thank you all for your reply. The version that I am using is 5.3.0. I have tried the above made recommendations, but none seem to work. So I have changed the entire code and here is it. This is also not working. All it does is refresh the page and does not even execute the php code in the first page.Appreciate your replies! Page 1 defining the layout for user input and calling the function to enter the record in database in page 2 <html> <head> <title>Enter Session Details Here</title> <script language="javascript" type="text/javascript" src="datetimepicker.js"> //Date Time Picker script </script> </head> <body> <form method="POST" name="stdFrm" action="sessionDetailsEntry.php3"> <U><B><CENTER>Session Information Entry</CENTER></B></U> <TABLE align="center"> <TR> <TD>Teacher:</TD> <TD><input type=text name="teacherName" size="25" maxlength="25"</TD> </TR> <TR> <TD>Student Name:</TD> <TD><input type=text name="studentName" size="10" maxlength="10" maxlength="10"></TD> </TR> <TR> <TD>Topic Taught:</TD> <TD><input type=text name="topic" size="25" maxlength="25"></TD> </TR> <TR> <TD>Session Date and Time:</TD> <TD><input id="sessionDateTime" name="sessionDateTime" type="text" size="25"><a href="javascript:NewCal('sessionDateTime','ddmmyyyy',true, 12)"><img src="cal.gif" width="16" height="16" border="0" ></a></TD> </TR> <TR> <TD>Assignment Score:</TD> <TD><input type=text name="score" size="10" maxlength="10"></TD> </TR> <TR> <TD>Comments:</TD> <TD><textarea name="comments" cols=80 rows=4 maxlength="500"></textarea></TD> </TR> <TR> <TD>Technical Issues:</TD> <TD><textarea name="technicalIssues" cols=80 rows=4 maxlength="300"></textarea></TD> </TR> <TR> <TD>Next Topic:</TD> <TD><input type=text name="nextTopic" size="25" maxlength="25"></TD> </TR> <TR> <TD align="right"><input type="submit" name='submit' value="Save"></TD> <TD align="left"><input type="reset" value="Clear"></TD> </TR> </TABLE> <? php include 'db_ops.php3'; echo "calling submit"; if (isset($_POST['submit'])) { add_to_db(); } else {echo "Insert attemp failed";} ?> </form> </body> </html> page 2 with the function <?php function add_to_db() { $link = mysql_connect('localhost' , 'root' , '' ); if (!$link) { die('Could not connect: ' . mysql_error()); } echo 'connected successfully' . "\n" ; mysql_select_db('etz', $link); $qurSQL = "INSERT INTO session_entry (teacher_id, student_id, topic, assignment_score, comments, session_issues, next_topic, date, timestamp) VALUES (' ".$_POST['teacherName']."', '".$_POST['studentName']."', '".$_POST['topic']."', '".$_POST['score']."', '".$_POST['comments']."', '".$_POST['technicalIssues']."', '".$_POST['nextTopic']."', '".$_POST['sessionDateTime']."', '".$_POST['sessionDateTime']."')"; echo $qurSQL; mysql_query ($qurSQL) or die(mysql_error()); mysql_close($link); echo 'connected close' . "\n" ; } Quote Link to comment https://forums.phpfreaks.com/topic/274660-adding-records-to-database/#findComment-1413792 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.