Jump to content

Contact form with contents stored on MYsql Database


maximus83

Recommended Posts

Hi,

 

I want to create a form with the contents stored on a database. I've got so far but now I'm totally stuck. At the moment when I click send the connection seems to work because i get the message "Connected to MySQL" when I click submit, but my table 'warranty_reg' is not been updated and I'm not getting the message "Thank you, your warranty has been registered." I'm not sure if I have inserted my table right in my database, i have tried a totally blank table and adding the fields manually eg. name, address etc. 

 

Please excuse the length of my code below but I feel it all might be relevant, is there a problem with the code or am i doing something wrong at the database end? Any help would be greatly appreciated.

 



<?php
/* Detect submit */

if (isset($_POST['submit'])) {


$username = "username";
$password = "password";
$hostname = "localhost"; 

//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL";

//select a database to work with
$selected = mysql_select_db("mydatabasename",$dbhandle)
  or die("Could not select databasename db");


/* Set variables from form data */

if (empty($_POST['first_name'])) {
$first_name = FALSE;
$validate .='<p>Please enter your first name.</p>'; 
} else { 
$first_name = $_POST['first_name'];
}

if (empty($_POST['surname'])) {
$surname = FALSE;
$validate .='<p>Please enter your last name.</p>'; 
} else { 
$surname = $_POST['surname'];
}

if (empty($_POST['email'])) {
$email = FALSE;
$validate .='<p>Please enter your email address.</p>'; 
} else { 
$email = $_POST['email'];
}	

if (empty($_POST['veremail'])) {
$veremail = FALSE;
$validate .='<p>Please re-enter your email address for verification.</p>'; 
} else { 
$veremail = $_POST['veremail'];
if ($email != $veremail){
$validate .='<p>The email address you entered does not does not match the verification address - please check you have entered both addresses correctly.</p>'; 
}
}


if($_POST['product'] == 'NULL') {
$product = FALSE;	
$validate .='<p>Please choose a product.</p>';
} else { 
$product = $_POST['product'];
}

if($_POST['retailer'] == 'NULL') {
$retailer = FALSE;	
$validate .='<p>Please tell us which retailer you purchased the product from.</p>';
} else { 
$retailer = $_POST['retailer'];
}


if (empty($_POST['address'])) {
$address = FALSE;
$validate .='<p>Please enter your postal address.</p>'; 
} else { 
$address = $_POST['address'];
}



/* Check everything is set */

if ($first_name && $surname && $email && $product && $retailer && $address &&($email == $veremail)) {

/* Add to table */

$query = "INSERT INTO warranty_reg (first_name, surname, email, product, retailer, dop, message, address, created) VALUES ('$first_name', '$surname', '$email', '$product', '$retailer', '$address', NOW() )";
$result = @mysql_query ($query);

/* If successfully added to table */

if ($result) {	


echo '<h3>Thank you, your warranty has been registered.</h3>';



/* Show tech problem message */	

} else { 
$validate = '<h3>Your warranty could not be sent due to a technical problem - please try again later. We apologize for any inconvenience.</h3>';
}

/* Show form verification messages */	

} else {
if (isset($validate)) {
	echo '<p>You need complete ALL of the fields in the contact form</p><br />'.$validate.'</p><br /><p>Thank you.</p><br /><p><FORM><INPUT TYPE="button" VALUE="Back to form" onClick="history.go(-1);return true;"> </FORM></p>';
}
}

/* Show initial form */	

} else {
?>

<!-- End of processing -->     
<form method="post" action="<?php echo htmlentities($_SERVER['REQUEST_URI']); ?>">
    <h1>Register your Warranty</h1><br />



      
      <table width="421" border="0" align="center" cellpadding="0" cellspacing="0">
            <tr>
              <td width="182" valign="top"><p>First name:</p></td>
              <td width="239" align="left" valign="top"><p>
                  <input type="text" name="first_name" id="first_name" autocomplete="off" value="<?php if (isset($POST['first_name'])) echo $_POST['first_name']; ?>">
                </p></td>
            </tr>
             <tr>
              <td valign="top"><p>Last name:</p></td>
              <td align="left" valign="top"><p>
                  <input type="text" name="surname" id="surname" autocomplete="off" value="<?php if (isset($POST['surname'])) echo $_POST['surname']; ?>">
                </p></td>
            </tr>           
            <tr>
              <td valign="top"><p>Email address:</p></td>
              <td align="left" valign="top"><p>
                  <input type="text" name="email" id="email" autocomplete="off" value="<?php if (isset($POST['email'])) echo $_POST['email']; ?>">
                </p></td>
            </tr>
            <tr>
              <td valign="top"><p>Re-enter email address:</p></td>
              <td align="left" valign="top"><p>
                  <input type="text" name="veremail" id="veremail" autocomplete="off" value="<?php if (isset($POST['veremail'])) echo $_POST['veremail']; ?>">
                </p></td>
            </tr>
            <tr>
              <td valign="top"><p>Product:</p></td>
              <td><p>
<select name="product">
  <option selected="selected" value="NULL">-- Select a product --</option>
  <option value="oso zero mount">product1</option>
  <option value="oso zero mount">product2</option>
  <option value="oso zero mount">product3</option>
  <option value="oso zero mount">product4</option>
  <option value="oso zero mount">product5</option>

</select>
                </p></td></tr>
            <tr>
              <td valign="top"><p>Which retailer did you purchase the product from?</p><br /></td>
              <td align="left" valign="top"><p>
                  <input type="text" name="retailer" id="retailer" autocomplete="off" value="<?php if (isset($POST['retailer'])) echo $_POST['retailer']; ?>">
                </p></td>
            </tr>
                <tr>
              <td valign="top"><p>Date of purchase:</p></td>
              <td align="left" valign="top"><p>DD: 
<select name="date_day" id="date_day">
<option selected="selected" value="01">01</option>
<option value="02">02</option>
<option value="03">03</option>
<option value="04">04</option>
<option value="05">05</option>
<option value="06">06</option>
<option value="07">07</option>
<option value="08">08</option>
<option value="09">09</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
<option value="30">30</option>
<option value="31">31</option>
</select>
MM: <select name="date_month" id="date_month">
<option selected="selected" value="01">01</option>
<option value="02">02</option>
<option value="03">03</option>
<option value="04">04</option>
<option value="05">05</option>
<option value="06">06</option>
<option value="07">07</option>
<option value="08">08</option>
<option value="09">09</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
</select>
                  YYYY: <input name="date_year" type="text" autocomplete="off" id="date_year" value="<?php if (isset($POST['date_year'])) echo $_POST['date_year']; ?>" size="4" maxlength="4">
                </p></td></tr>
                
                        
            <tr>
              <td valign="top"><p>Postal address:</p>
             </td>
              <td align="left" valign="top"><p>
                  <textarea name="address" id="address" cols="30" rows="6" value="<?php if (isset($POST['address'])) echo $_POST['address']; ?>"></textarea>
                </p></td>
          </tr>
            </table>
           
           <p>
                  <input name="submit" type="submit" value="Send Message">
                </p>
               
             
</form>
<?php 
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.