Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> newsletter - problem z zapisaniem do bazy
traczu1985
post 26.08.2019, 17:41:09
Post #1





Grupa: Zarejestrowani
Postów: 7
Pomógł: 0
Dołączył: 26.08.2019

Ostrzeżenie: (0%)
-----


Jeśli nie ten dział to proszę moderatorów o przesunięcie do innego

Mam gotową stronę ze starym skryptem wyglądowo jest ok ale nie funkcjonuje zapisywanie do bazy.

Nie bardzo wiem gdzie szukać przyczyny po za tym że index jest w html

jest to formularz ,który ma tylko zapisać lub wypisać z bazy.

  1. <!DOCTYPE html>
  2. <!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
  3. <!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
  4. <!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]-->
  5. <!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
  6. <head>
  7. <meta charset="utf-8">
  8. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  9. <title>Zapisz się i zgarnij LODA Proteinowego</title>
  10. <meta name="description" content="">
  11. <meta name="viewport" content="width=device-width, initial-scale=1">
  12.  
  13. <!-- Place favicon.ico and apple-touch-icon.png in the root directory -->
  14.  
  15. <link rel="stylesheet" href="css/normalize.css">
  16. <link rel="stylesheet" href="css/main.css">
  17. <link href='http://fonts.googleapis.com/css?family=Open+Sans:400,300,700,800' rel='stylesheet' type='text/css'>
  18. <link href="//netdna.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css" rel="stylesheet">
  19. <script src="js/vendor/modernizr-2.6.2.min.js"></script>
  20. </head>
  21. <body>
  22. <!--[if lt IE 7]>
  23. <p class="browsehappy">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve your experience.</p>
  24. <![endif]-->
  25.  
  26. <!-- Add your site or application content here -->
  27. <form id="subscribe-form" class="subscribe-form hide">
  28. <div class="form-title">Zapisz się i zgarnij Loda Proteinowego</div>
  29. <hr>
  30. <div class="form-loading hide"><i class="fa fa-circle-o-notch fa-spin"></i></div>
  31. <div class="form-message hide">
  32. Dzięki teraz zgłoś się po LODA !</div>
  33. <div class="form-content">
  34. <div class="group">
  35. <label for="email">Twój adres email</label>
  36. <div class="input-prefix">
  37. <i class="fa fa-user"></i>
  38. <input id="email" name="email" class="form-control" type="email" placeholder="Your email">
  39. </div>
  40. </div>
  41. <div class="group">
  42. <label for="name">Twoje imię</label>
  43. <div class="input-prefix">
  44. <i class="fa fa-envelope"></i>
  45. <input id="name" name="name" class="form-control" type="text" placeholder="Your name">
  46. </div>
  47. </div>
  48. <div class="group group-submit">
  49. <label class="empty"></label>
  50. <div align="right"><button class="btn-submit" type="submit">Zapisz się</button></div>
  51. </div>
  52. </div>
  53. <div class="form-footer">
  54. <a class="btn" data-toggle="unsubscribe" href="#">Wypisz się</a>
  55. </div>
  56. <input type="hidden" name="action" value="subscribe">
  57. </form>
  58.  
  59. <form id="unsubscribe-form" class="subscribe-form hide">
  60. <div class="form-title unsubscribe">Unsubscribe from our newsletter</div>
  61. <hr>
  62. <div class="form-loading hide"><i class="fa fa-circle-o-notch fa-spin"></i></div>
  63. <div class="form-message hide">
  64. You've been unsubscribed.</div>
  65. <div class="form-content">
  66. <div class="group">
  67. <label for="unsubscribe_email">Your email</label>
  68. <div class="input-prefix">
  69. <i class="fa fa-user"></i>
  70. <input id="unsubscribe_email" name="unsubscribe_email" class="form-control" type="email" placeholder="Your email">
  71. </div>
  72. </div>
  73. <div class="group">
  74. <label class="empty"></label>
  75. <div>
  76. <input id="confirm" type="checkbox" name="confirm" value="T">
  77. <label for="confirm">Click to confirm you want to unsubscribe</label>
  78. </div>
  79. </div>
  80. <div class="group group-submit">
  81. <label class="empty"></label>
  82. <div align="right"><button class="btn-submit" type="submit">Unsubscribe</button></div>
  83. </div>
  84. </div>
  85. <div class="form-footer">
  86. <a class="btn" data-toggle="subscribe" href="#">Subscribe</a>
  87. </div>
  88. <input type="hidden" name="action" value="unsubscribe">
  89. </form>
  90.  
  91. <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
  92. <script>window.jQuery || document.write('<script src="js/vendor/jquery-1.10.2.min.js"><\/script>')</script>
  93. <script src="js/plugins.js"></script>
  94. <script src="js/main.js"></script>
  95. </body>
  96. </html>




  1. CREATE TABLE IF NOT EXISTS `subscription_form` (
  2. `id` int(11) NOT NULL AUTO_INCREMENT,
  3. `email` varchar(250) NOT NULL,
  4. `name` varchar(250) NOT NULL,
  5. `date_subscribe` datetime NOT NULL,
  6. `date_unsubscribe` datetime NOT NULL,
  7. `status` char(1) NOT NULL,
  8. PRIMARY KEY (`id`)
  9. ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;


  1. # CONFIG
  2. define('_DB_HOST', 'xxxxxxxxxxxxxxx');
  3. define('_DB_NAME', 'xxxxxxxxxxxxxxx');
  4. define('_DB_USER', 'xxxxxxxxxxxxxxx');
  5. define('_DB_PASS', 'xxxxxxxxxxxxxxx');
  6.  
  7.  
  8. $subscribe = (isset($_POST['action']) && $_POST['action'] == 'unsubscribe')?false:true;
  9.  
  10. if ($subscribe){
  11. $fields = array(
  12. array('name' => 'email', 'valid' => array('require', 'email')),
  13. array('name' => 'name', 'valid' => array('require')),
  14. );
  15. }else{
  16. $fields = array(
  17. array('name' => 'unsubscribe_email', 'valid' => array('require', 'email')),
  18. array('name' => 'confirm', 'valid' => array('require'), 'err_message' => 'Please confirm'),
  19. );
  20. }
  21.  
  22. // Connect to database
  23. $connection = mysql_connect(_DB_HOST, _DB_USER, _DB_PASS) or die ('Unable to connect to MySQL server.<br ><br >Please make sure your MySQL login details are correct.');
  24. $db = mysql_select_db(_DB_NAME, $connection) or die ('request "Unable to select database."');
  25.  
  26. $error_fields = array();
  27. $get = array();
  28. foreach ($fields AS $field){
  29. $value = isset($_POST[$field['name']])?$_POST[$field['name']]:'';
  30. if (is_array($value)){
  31. $value = implode('/ ', $value);
  32. }
  33. $value = stripslashes($value);
  34. }
  35. $get[$field['name']] = mysql_real_escape_string($value);
  36. $is_valid = true;
  37. $err_message = '';
  38. if (!empty($field['valid'])){
  39. foreach ($field['valid'] AS $valid) {
  40. switch ($valid) {
  41. case 'require':
  42. $is_valid = $is_valid && strlen($value) > 0;
  43. $err_message = 'Field required';
  44. break;
  45. case 'email':
  46. $is_valid = $is_valid && preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", $value);
  47. $err_message = 'Email required';
  48. break;
  49. default:
  50. break;
  51. }
  52. }
  53. }
  54. if (!$is_valid){
  55. if (!empty($field['err_message'])){
  56. $err_message = $field['err_message'];
  57. }
  58. $error_fields[] = array('name' => $field['name'], 'message' => $err_message);
  59. }
  60. }
  61.  
  62. if (empty($error_fields)){
  63. if ($subscribe){
  64. $data = array(
  65. 'email' => "'".$get['email']."'",
  66. 'name' => "'".$get['name']."'",
  67. 'date_subscribe' => 'NOW()',
  68. 'status' => "'T'",
  69. );
  70. $sql = "REPLACE INTO subscription_form (`".implode("`, `", array_keys($data))."`) VALUES(".implode(", ", array_values($data)).")";
  71. }else{
  72. $sql = "UPDATE subscription_form SET date_unsubscribe = NOW(), status = 'F' WHERE email = '".$get['unsubscribe_email']."'";
  73. }
  74. if (!empty($sql)){
  75. $sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);
  76. }
  77. echo (json_encode(array('code' => 'success')));
  78. }else{
  79. echo json_encode(array('code' => 'failed', 'fields' => $error_fields));
  80. }


Ten post edytował traczu1985 26.08.2019, 17:43:32
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 10.11.2024 - 18:59