Advertisement
karose

not entering db

Nov 4th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.28 KB | None | 0 0
  1. hello guys i've been battling with this no show my data can't get into the database it just prints the else part of my function
  2.  
  3. karose-services_db.php
  4.  
  5. error_reporting( E_ALL & ~E_DEPRECATED & ~E_NOTICE );
  6.  
  7. $servername = "localhost";
  8. $username = "root";
  9. $password = "";
  10. $db= "karose-services_db";
  11.  
  12.  
  13. register.php
  14.  
  15. <?php
  16. /**
  17. * Created by IntelliJ IDEA.
  18. * User: Karachi
  19. * Date: 10/29/2017
  20. * Time: 7:14 AM
  21. */
  22.  
  23.  
  24. require_once 'karose-services_db.php';
  25.  
  26.  
  27. error_reporting(E_ALL); ini_set('display_errors', 1);
  28. session_start();
  29.  
  30. if(isset($_SESSION['user_id'])) {
  31. header("Location: index.php");
  32. }
  33.  
  34.  
  35. /**
  36. * validate form
  37. * @return boolean return true if no error found, otherwise return false
  38. */
  39. $errors= array();
  40. $firstname='';
  41. $lastname='';
  42. $email='';
  43. $mobile='';
  44. $c_name='';
  45. $s_experience='';
  46. $t_size='';
  47. $m_turnover='';
  48. $a_details='';
  49. $services='';
  50. $o_services='';
  51. $city='';
  52. $o_city='';
  53. $s_location='' ;
  54.  
  55. function validate_form(){
  56. global $firstname,$lastname,$email,$mobile,$c_name, $s_experience, $t_size,$m_turnover,
  57. $a_details,$services,$o_services,$city,$o_city,$s_location,$errors ;
  58.  
  59. if(isset($_POST['txtfirstname'])!=''){
  60. $firstname= filter_var($_POST['txtfirstname'], FILTER_SANITIZE_STRING);
  61.  
  62. if ($firstname==''){
  63. $errors[]="hey! name is required";
  64. }
  65. }else{
  66. $errors[]="you must enter your name";
  67. }
  68.  
  69. if(isset($_POST['txtlastname'])!=''){
  70. $lastname= filter_var($_POST['txtlastname'], FILTER_SANITIZE_STRING);
  71. if ($lastname==''){
  72. $errors[]="hey! name is required";
  73. }
  74. }else{
  75. $errors[]="you must enter your name";
  76. }
  77.  
  78. if(isset($_POST['txtemail']) !=''){
  79. $email= filter_var($_POST['txtemail'], FILTER_SANITIZE_STRING);
  80. if( !filter_var($email, FILTER_VALIDATE_EMAIL)){
  81. $errors[]='email is not valid';
  82. }
  83. }else{
  84. $errors[]='please email is required';
  85. }
  86.  
  87. if(isset($_POST['txtmobile'])!=''){
  88. $mobile= filter_var($_POST['txtmobile'],FILTER_SANITIZE_NUMBER_INT);
  89. if($mobile==''){
  90. $errors[]='please enter your number';
  91. }
  92. }else{
  93. $errors[]='enter valid number';
  94. }
  95.  
  96. if(isset($_POST['txtcompany'])!=''){
  97. $c_name= $_POST['txtcompany'];
  98. }else{
  99. $errors[]='please enter company name';
  100. }
  101.  
  102. if(isset($_POST['selexperience' ])!=''){
  103. $s_experience=$_POST['selexperience'];
  104. }else{
  105. $errors[]= ' please select one';
  106. }
  107.  
  108. if(isset($_POST['selteamsize'])!=''){
  109. $t_size= $_POST['selteamsize'];
  110. }else{
  111. $errors[]=' please select one';
  112. }
  113.  
  114. if(isset($_POST['selmon_turnover'])!=''){
  115. $m_turnover=$_POST['selmon_turnover'];
  116. }else{
  117. $errors[]='please select one ';
  118. }
  119.  
  120. if(isset($_POST['additional_details'])!=''){
  121. $a_details= filter_var($_POST['additional_details'], FILTER_SANITIZE_STRING);
  122. if($a_details==''){
  123. $errors[]='please enter valid information or contact us ';
  124. }
  125. }
  126.  
  127. if(isset($_POST['selservices[]'])){
  128. $services=$_POST['selservices[]'];
  129. }
  130. if(isset($_POST['txtotherservice']) !=''){
  131. $o_services=filter_var($_POST['txtotherservice'], FILTER_SANITIZE_STRING);
  132. if($o_services== $services){
  133. $errors[]='please enter valid information';
  134. }else{
  135. $errors[]='please enter valid information or contact us ';
  136. }
  137. }
  138.  
  139. if(isset($_POST['selcity[]']) !=''){
  140. $city= $_POST['selcity[]'];
  141. if($city==''){
  142. $errors[]='enter something valid';
  143. }
  144. }
  145.  
  146. if(isset($_POST['txtothercity']) !=''){
  147. $o_city= filter_var($_POST['txtothercity'], FILTER_SANITIZE_STRING);
  148. if($o_city==''){
  149. $errors[]='check what you entered';
  150. }
  151. }
  152.  
  153. if(isset($_POST['txtotherlocation'])!=''){
  154. $s_location= filter_var($_POST['txtotherlocation'],FILTER_SANITIZE_STRING );
  155. if ($s_location){
  156. $errors[]='please enter something right';
  157. }
  158. }
  159.  
  160. if(count($errors)){
  161. return false;
  162. }else{
  163. return true;
  164. }
  165. }
  166.  
  167. var_dump(validate_form());
  168.  
  169. //function display_value($fieldName){
  170. // echo isset($_POST[$fieldName]) ? $_POST[$fieldName] : '';
  171. //}
  172.  
  173. function insert_user($firstname,$lastname,$email,$mobile,$c_name, $s_experience, $t_size,$m_turnover,
  174. $a_details,$services,$o_services,$city,$o_city,$s_location){
  175. global $dbh, $msg;
  176.  
  177.  
  178. echo "here{$GLOBALS['dbh']}";
  179. // construct SQL insert statement
  180. $sql_insert = "INSERT INTO partners(firstname, lastname, mail, mobilenumber, companyname,experience, teamsize, monthlyturnover,qualifications,servicesprovided, anotherservice, city, anothercity, specificareas)
  181. VALUES(" . $dbh->quote($firstname) .", " . $dbh->quote( $lastname) . ", " . $dbh->quote($email) . ", " . $dbh->quote($mobile) . ", " . $dbh->quote($c_name) . ", " . $dbh->quote($s_experience) . ", " . $dbh->quote($t_size) . ", " . $dbh->quote($m_turnover) . ", " . $dbh->quote($a_details) . ", " . $dbh->quote($services) . ",
  182. " . $dbh->quote($o_services) . ", " . $dbh->quote($city) . ", " . $dbh->quote($o_city) . ", " . $dbh->quote($s_location) . ")";
  183.  
  184. if($dbh->exec($sql_insert) === false){
  185. $msg = 'Error inserting the partner.';
  186. return false;
  187. }else{
  188. $msg = "The new partner is created";
  189. return true;
  190. }
  191. }
  192.  
  193. function start_form(){
  194.  
  195. // if user submitted the form
  196. if(isset($_POST['btncreatepartner']))
  197. {
  198. // validate form
  199. if(validate_form())
  200. {
  201. require_once 'karose-services_db.php';
  202.  
  203. $dsn = "mysql:host=$servername;dbname=$db";
  204. try {
  205. // create database connection
  206. new PDO($dsn, $username, $password);
  207. insert_user(firstname, lastname, mail, mobilenumber, companyname,experience, teamsize, monthlyturnover,qualifications,servicesprovided, anotherservice, city, anothercity, specificareas);
  208.  
  209. } catch (PDOException $e) {
  210. echo $e->getMessage();
  211. }
  212. }
  213. }else{
  214. echo 'error ';
  215. }
  216. }
  217.  
  218. start_form();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement