View Single Post
  #12 (permalink)  
Old 10-30-09, 04:31 PM
PopSmith PopSmith is offline
Newbie Coder
 
Join Date: May 2009
Posts: 18
Thanks: 5
Thanked 0 Times in 0 Posts
I was experimenting with this again and I can't get it to post any of the information to the database. I have it posting back to the same page and when I hit "Submit" it shows the page minus the forum. I don't have a problem there, from what I know.

However, the information isn't being recorded into the database. I modified the code a bit since I last posted so I'm posting it again. I'll keep playing with it and if I get it to work I'll report back.

The index.php file:
php Code:
  1. <?php
  2. include('../includes/constants.php');
  3. include('../includes/sanitize.php');
  4. ?>
  5. <!DOCTYPE html>
  6. <html lang="en">
  7. <head>
  8. <meta charset="utf-8">
  9. <title>Coming Soon!</title>
  10. </head>
  11.  
  12. <body>
  13. We are trying to get things running, please pardon the virtual dust!
  14. <p>If you would like us to notify you when we get setup fill out the form below with your name and email address.</p>
  15. <?php
  16. if (isset($_POST&#91;'submitted'])) {
  17.         require_once(MYSQL);
  18.        
  19.         //Sanitize data before validation
  20.         $c_fname = sanitize(3, 15, $_POST&#91;'first_name']);
  21.         $c_lname = sanitize(3, 25, $_POST&#91;'last_name']);
  22.         $c_email = sanitize(5, 60, $_POST&#91;'email']);
  23.         $c_email_2 = sanitize(5, 60, $_POST&#91;'email_2']);
  24.        
  25.         //Validate the first name
  26.         return preg_match("/^
  27.         [a-zA-Z]
  28.         [a-zA-Z.-]{1,15}/", $c_fname);
  29.        
  30.         //Validate last name
  31.         return preg_match("/^
  32.         [a-zA-Z]
  33.         [a-zA-Z-]{1,25}/", $c_lname);
  34.        
  35.         //Is the email valid and not empty?
  36.         if (empty($c_email_2)) {
  37.             echo '<p>Please enter your email address in both fields.</p>';
  38.         } else if ($c_email != $c_email_2) {
  39.             echo '<p>The email fields do not match. Please confirm them again.</p>';
  40.         } else {
  41.             function isValidEmail( $c_email = null ) {
  42.             return preg_match( "/^
  43.             [\d\w\/+!=#|$?%{^&}*`'~-]
  44.             [\d\w\/\.+!=#|$?%{^&}*`'~-]*@
  45.             [A-Z0-9]
  46.             [A-Z0-9.-]{1,60}
  47.             [A-Z0-9]\.
  48.             [A-Z]{2,6}/", $c_email);
  49.                 }
  50.         }
  51.  
  52.         if (!empty($c_email) && $c_email == $c_email_2) {
  53.             $access = "SELECT Email FROM customers WHERE Email='$c_email'";
  54.             $r = mysqli_query($dbc, $access) or trigger_error("Query: $access\n<br />MySQL error: " . mysqli_error($dbc));
  55.         }
  56.        
  57.         //Insert information if email is unique:
  58.         if (mysqli_num_rows($r) == 0) {
  59.             $access = "INSERT INTO customers (email, first, last) VALUES ('$c_email', '$c_fname', '$c_lname')";
  60.             $r = mysqli_query($dbc, $access) or trigger_error("Query: $access\n<br />MySQL error: " . mysqli_error($dbc));
  61.         } else {
  62.             echo '<p>Your email address could not be recorded due to an error, please try again.</p>';
  63. }
  64. mysqli_close($dbc);
  65. } // End Submit IF.
  66. ?>
  67. <form action="index.php" method="post">
  68. <fieldset>
  69. <p><b>First name:</b> <input type="text" name="first_name" size="20" maxlength="15" value="<?php if(isset($c_fname)) echo $c_fname; ?>" /></p>
  70. <p><b>Last name:</b> <input type="text" name="last_name" size="20" maxlength="25" value="<?php if(isset($c_lname)) echo $c_lname; ?>" /></p>
  71. <p><b>Email Address:</b> <input type="text" name="email" size="30" maxlength="60" value="<?php if(isset($c_email)) echo $c_email; ?>" /> (Required)</p>
  72. <p><b>Confirm email address:</b> <input type="text" name="email_2" size="30" maxlength="60" value="<?php if(isset($c_email_2)) echo $c_email_2; ?>" /> (Required)</p>
  73. <br />
  74. </fieldset>
  75. <div class="center"><input type="submit" name="submit" value="Register" /></div>
  76. <input type="hidden" name="submitted" value="TRUE" />
  77. </form>
  78. </body>
  79. </html>

Constants.php:
php Code:
  1. <?php
  2. define('BASE_URL', 'http://mysite.com');
  3. define('MYSQL', '../SQL/mysqli_connect.php');
  4. ?>

php Code:
  1. <?php
  2. //Set database access information:
  3. DEFINE('DB_USER', '********');
  4. DEFINE('DB_PASSWORD', '********');
  5. DEFINE('DB_HOST', '*****');
  6. DEFINE('DB_NAME', '******');
  7.  
  8. //Attempt to connect to MySQL
  9. $dbc = @mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
  10. if (!$dbc) {
  11.     trigger_error('Could not connect to MySQL: ' . mysqli_connect_error() );
  12. }
  13. ?>