Jump to content

PHP Form insert MySql


Go to solution Solved by barkly,

Recommended Posts

Hi,

 

I created a form that inserts first - last name, address, and email address into a mysql databse table.

The record inserts correctly  - No problems here.  

 

The problem is I only want to insert the data if the email and address doesn't exist.   

 

I set it up for just the email right now to try to get this to work but have failed.

 

When the form is submitted and the record exists it inserts the record anyway and sends the email which it's not suppose to do. 

 

The form is suppose to after submitting - 

 

1. validate

2. insert only if the record doesn't exist

3. send the email. 

 

Here's the code

<?php

$to = "email@email.com";

if(isset($_POST['submit'])) 
{ 
  
 
// VALIDATION
  
  
  if(empty($_POST['firstName'])) 
  { 
    
    $errorfirstName .= "First Name Required";
		
  } 
  
     
  
 if(empty($_POST['lastName'])) 
  { 
    $errorfirstName .= "Last Name Required";
  }
  
   
 
  if(empty($error))   # No error go ahead and email it. 
  { 
    
	$to = "$to";
	$subject = 'the form';

   $msg .="<html><head>
		<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
        <title>The Form</title>
    	</head>
    <body>
    <table>
    <tr>
   <td>Email sent for confirmation</td>
      </tr>
       </table>
     </body>
     </html>";


   $mail($to, $subject, $headers, $msg); 
				   
				   
  if(!$result) 
    { 
      $error = "<div id='errors'>There was an unknown error </div>"; 
    } 
    else 
    { 
   
      include('connection.php');
 
	$firstName = mysqli_real_escape_string($con, $_POST['firstName']);
	$lastName = mysqli_real_escape_string($con, $_POST['lastName']);
	$address = mysqli_real_escape_string($con, $_POST['address']);
	$email = mysqli_real_escape_string($con, $_POST['email']);

	   	$email = $_POST['email'];
			
		$sql = "SELECT * FROM table WHERE `email` = '{$email}'";
        
               $result = mysql_query($sql);
		 
		if ( mysql_num_rows ( $result ) > 0 )
		{
						
			$error = "Email Exists."; 
		}
		
		else
		{
		  
		   $error = "Email does not exist. Insert it!!!"; 
		   
		   $sql="INSERT INTO table (firstName, lastName, address, email) VALUES ('$_POST[firstName]','$_POST[lastName]','$_POST[address]', '$_POST[email]')";
		   
		   
		}
		 
		if (!mysqli_query($con,$sql))
		  {
		  die('Error: ' . mysqli_error($con));
		  }
		
		mysqli_close($con)
      { 
       
      } 
    } 
  } 
}
?> 
	
<!-- Form -->
<html>
 <head></head>   
 <body>
 <section>
  <form method="POST" action="theform" name="for" onsubmit="return validateForm(this)">                 
    <?php 
     if(!empty($error)) 
      { 
       echo "$error"; 
       }
       ?>
    This where the inputs would go not going to include them because not having an issue with the form  
              </form>
        </section> 
    </body>
</html>               
Edited by barkly
Link to comment
https://forums.phpfreaks.com/topic/292084-php-form-insert-mysql/
Share on other sites

You're mixing mysql with mysqli

 

 

Yes you're right!!! Thank You. 

 

 

I fixed that but now having problems with - 

$sql = "SELECT * FROM table WHERE `email` = '{$email}'";

I tried 'email' = '{$email}',  'address' = '{$address}'";   but it's inserting when the exact email and address exist.  How can I code this to have muliple vars? 

 

 

Also I can enter emal@email  and it validates and says it's not correct but it insert the data  - the validation works but it doesn't stop the insert. 

Edited by barkly

I fixed the email validation issue where it was validating because the address was missing .com but inserting the record anyway. 

 

The issues now - 

 

1.  If the record exists the error displays but email is sent anyway.  

2.  When I add address to -  $sql = "SELECT * FROM table WHERE (`email` = '{$email}',`address` = '{$address}')";   the data is inserted anyway.  

 

Can someone help?  

Edited by barkly
  • Solution

I got it to work.  I coded it like

 

`email` = '{$email}' OR `address` = '{$address}'";   I used OR in case one of the two were the same.  

The other issue I move the db code in a different area.  

 

 

Thanks SocialCloud for your help earlier. 

 
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.