943,603 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 2921
  • PHP RSS
Jul 6th, 2008
0

Problem inserting data into Access database via php

Expand Post »
Hi everyone, I'm new to programming and I'm doing an online registration form for my project. I have a problem with my form. After submitting my form, a confirmation page will show up with the information displayed on the page. When I click submit on the confirmation page, It is suppose to submit the data into the database. However I get HTTP 500 internal error The page cannot be displayed. I'm using ODBC for my connection and the connection is working as I can submit and retrieve information from the database using codes for other projects.
This is the code for the form and confirmation page


PHP Syntax (Toggle Plain Text)
  1. <?php
  2. error_reporting(E_ALL ^ E_NOTICE);
  3. ?>
  4. <?php
  5.  
  6. function checkphone() {
  7. global $tel;
  8. $phone = $_POST['hometel'];
  9. if (!preg_match('/^[0-9]{8}$/', $phone)) {
  10. $tel = '<font color="red"><b>Your telephone number is invalad</b></font>';
  11. $GLOBALS['tel'] = $tel; }
  12. else{
  13. $GLOBALS['showtel'] = "true";
  14. }
  15. }
  16.  
  17.  
  18. function checkemail() {
  19. global $mail;
  20. $email = $_POST['email'];
  21. if (!preg_match('/^([a-zA-Z0-9])+([a-zA-Z0-9\.\\+=_-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/', $email)) {
  22. $mail = '<font color="red"><b>Your Email is invalid</b></font>';
  23.  
  24. $GLOBALS['mail'] = $mail; }
  25. else
  26. {
  27. $GLOBALS['showmail'] = "true";
  28. }
  29. }
  30.  
  31. if($_POST['submit'])
  32. {
  33. checkphone();
  34. checkemail();
  35. }
  36. if(!$showtel||!$showmail){
  37. ?>
  38. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  39. <html>
  40. <head>
  41. <title>FYP</title>
  42. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  43. </head>
  44.  
  45. <body>
  46. <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
  47. <p align="center">Name:
  48. <input type="text" name="username" />
  49. </p>
  50. <p align="center">Admin No.:
  51.  
  52. <input type="text" name="admin" />
  53. </p>
  54. <p align="center">Gender:
  55. <select name="gender">
  56. <option value="male" selected>Male</option>
  57. <option value="female">Female</option>
  58. </select>
  59. </p>
  60. <p align="center">Address:
  61. <textarea name="address" cols="40"></textarea />
  62. </p>
  63. <p align="center">Home Phone:
  64. <input type="text" name="hometel" /> <br>
  65. <?php echo $tel; ?>
  66. </p>
  67. <p align="center">Mobile Phone:
  68. <input type="text" name="hp" />
  69. </p>
  70. <p align="center">Contact Email:
  71. <input type="text" name="email" /> <br>
  72. <?php echo $mail; ?>
  73. </p>
  74. <p align="center">Course:
  75. <select name="course">
  76. <option value="ECC">ECC</option>
  77. </select>
  78. </p>
  79. <p align="center">Acad Year:
  80. <input type="text" name="year" />
  81. </p>
  82. <p align="center">Path:
  83. <select name="path">
  84. <option value="1">B1</option>
  85. <option value="2">B2</option>
  86. <option value="3">B3</option>
  87. </select>
  88. </p>
  89. <p align="center">Project Title:
  90. <textarea name="title" cols="40"></textarea>
  91. </p>
  92. <p align="center">Project Supervisor:
  93. <select name="supervisor">
  94. <option value="Mr. Stephen Liew K C" selected>Mr. Stephen Liew K C</option>
  95. <option value="Mr. Steven Ng">Mr. Steven Ng</option>
  96. </select>
  97. </p>
  98. <p align="center">
  99. <input name="submit" type="submit" />
  100. </p>
  101.  
  102. </form>
  103.  
  104. <?php
  105. } else {
  106. ?>
  107. <form action="datasubmit.php" method="post">
  108. <?php
  109.  
  110. echo'<div align="center">';
  111. echo '<p>Name: '.$_POST["username"].' <br />';
  112. echo 'Admin No.: '.$_POST["admin"].' <br />';
  113. echo 'Gender: '.$_POST["gender"].' <br />';
  114. echo 'Address: '.$_POST["address"].' <br />';
  115. echo 'Home Phone: '.$_POST["hometel"].' <br />';
  116. echo 'Handphone: '.$_POST["hp"].' <br />';
  117. echo 'Contact Email: '.$_POST["email"].' <br />';
  118. echo 'Course: '.$_POST["course"].' <br />';
  119. echo 'Acad Year: '.$_POST["year"].' <br />';
  120. echo 'Path: '.$_POST["path"].' <br />';
  121. echo 'Project title:'.$_POST["title"].' <br />';
  122. echo 'Project Supervisor: '.$_POST["supervisor"].' <br />';
  123.  
  124. ?>
  125. <p>
  126. <input type="submit" name="Submit" value="Submit">
  127. </form>
  128. </p>
  129. <p>
  130. <input name="button" type=button onClick=window.history.back() value=Back back>
  131. </p>
  132. </div>
  133.  
  134. <?php }?>
  135.  
  136. </body>
  137. </html>


And this is the code that I'm trying to use to submit into the database.

PHP Syntax (Toggle Plain Text)
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <head>
  4. <title>Untitled Document</title>
  5. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  6. </head>
  7. <body>
  8. <?php
  9. $username = $_POST['username'];
  10. $admin = $_POST['admin'];
  11. $gender = $_POST['gender'];
  12. $address = $_POST['address'];
  13. $phone = $_POST['hometel'];
  14. $hp = $_POST['hp'];
  15. $email = $_POST['email'];
  16. $course = $_POST['course'];
  17. $year = $_POST['year'];
  18. $path = $_POST['path'];
  19. $title = $_POST['title'];
  20. $supervisor = $_POST['supervisor'];
  21. echo "Data has been submitted";
  22.  
  23. function Enter_New_Entry($username,$admin) {
  24.  
  25.  
  26. if(!$cnx = odbc_connect( 'FYP' , 'root', '' ))die("error with connection");
  27. $SQL_Exec_String = "Insert Into rank (name, admin, gender, address, hometel, hp, email, course, year, path, title, supervisor )
  28. Values ('$username', '$admin', '$gender', '$address', '$phone', '$hp', '$email', '$course', '$year', '$path', '$title', '$supervisor')";
  29.  
  30. $cur= odbc_exec( $cnx, $SQL_Exec_String );
  31. if (!$cur) {
  32. Error_handler( "Error in odbc_exec( no cursor returned ) " , $cnx );
  33. }
  34.  
  35. odbc_close( $cnx);
  36. }
  37. Enter_New_Entry($username,$admin);
  38. ?>
  39.  
  40. </body>
  41. </html>
Reputation Points: 10
Solved Threads: 0
Newbie Poster
joeey is offline Offline
8 posts
since Jul 2008
Jul 7th, 2008
0

Re: Problem inserting data into Access database via php

I found out that the http 500 error was due to Data type mismatch critearia expression. After changing, the data type from number to text, I get a new error. It stats that every value is undefined and there are missing argument for Enter_New_Entry. Does anyone know how to solve this problem ?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
joeey is offline Offline
8 posts
since Jul 2008
Jul 8th, 2008
0

Re: Problem inserting data into Access database via php

It seems that the register_global being off could be the problem for my code. I've tried turning register_global on and using session, it manage to insert the username and admin into the database. However everything starting with gender is being submitted as blank. I've read online that turning register_global is quite dangerous. Is there any way to get around this problem ?
Last edited by joeey; Jul 8th, 2008 at 12:28 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
joeey is offline Offline
8 posts
since Jul 2008
Jul 8th, 2008
0

Re: Problem inserting data into Access database via php

Hi,

Try to print the $SQL_Exec_String and check what query its creating.
If you can post the query then it will also help.
Reputation Points: 28
Solved Threads: 19
Junior Poster
vicky_rawat is offline Offline
137 posts
since Jun 2008
Jul 8th, 2008
0

Re: Problem inserting data into Access database via php

Thanks for the advice vick_rawat but it seems to be working now. I'm using $_SESSION['username'] = $username to get it working. Also is there any tutorial you can reccomand on updating the database?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
joeey is offline Offline
8 posts
since Jul 2008
Jul 9th, 2008
0

Re: Problem inserting data into Access database via php

Click to Expand / Collapse  Quote originally posted by joeey ...
Thanks for the advice vick_rawat but it seems to be working now. I'm using $_SESSION['username'] = $username to get it working. Also is there any tutorial you can reccomand on updating the database?
http://w3schools.com/php/php_mysql_update.asp Google is your friend !
Moderator
Featured Poster
Reputation Points: 524
Solved Threads: 356
Purple hazed!
nav33n is offline Offline
3,878 posts
since Nov 2007
Jul 10th, 2008
0

Re: Problem inserting data into Access database via php

Thank you for the link but I'm using MS Access as my database. Is there any other websites you can reccomand? I've tried googling but most of them are for insert and making the connection. I need to learn how to update the database via a form rather than using codes.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
joeey is offline Offline
8 posts
since Jul 2008
Jul 10th, 2008
0

Re: Problem inserting data into Access database via php

What do you mean by 'update database via a form than using codes' ? Maybe this can help.
http://www.configure-all.com/php_access.php
Moderator
Featured Poster
Reputation Points: 524
Solved Threads: 356
Purple hazed!
nav33n is offline Offline
3,878 posts
since Nov 2007
Jul 10th, 2008
0

Re: Problem inserting data into Access database via php

Sorry for causing a misunderstanding, I think the right word I should use is modify. I would like to modify my contents in the database via a website instead of typing the values into the codes. For example if I made a mistake and typed 110 for my age, I would like to have a function where I can show the entry with the mistake and correct it in a webpage.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
joeey is offline Offline
8 posts
since Jul 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Paypal integration with website
Next Thread in PHP Forum Timeline: new folder





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC