Jump to content

[SOLVED] Checking the Database to see if submission already exists...


suttercain

Recommended Posts

Good Friday to everyone,

 

I have set up a form. Once the form is submitted the information is sent to this page (see code) below. I am trying to use an if statement to check if the VIN already exists in the database before it is INSERTed into MySQL. If the vin does exist the user gets an error saying so. If it doesn't exist the ELSE statement INSERTs the information into the database and the user gets a thank you message.

 

It worked once... but stopped... I may have changed something to mess it up.

 

<?php
//If Everything Appears to be Okay, the User Sends to the Database by clicking submit
if(isset($_POST['final'])){
	$first_name=$_POST[first_name];
	$last_name=$_POST[last_name];
	$street=$_POST[street];
	$city=$_POST[city];
	$state=$_POST[state];
	$zip_code=$_POST[zip_code];
	$phone=$_POST[phone];
	$vehicle_year=$_POST[vehicle_year];
	$vehicle_make=$_POST[vehicle_make];
	$vehicle_model=$_POST[vehicle_model];
	$vin=$_POST[vin];
	$phone=$_POST[phone];
	$date = date('l F jS, Y');
	$time = date('g:i A');
	$next_month = date('l F jS, Y', strtotime("+28 day", time()));
	require ('get_connected.php');
	$sql = ("SELECT vin FROM canada");
    	if ($result = mysql_query($sql)) {
      	if (mysql_num_rows($result)) {
        $row = mysql_fetch_assoc($result);
	if ($vin == $row['vin']) {
	echo "<font color='red'>You request was not submitted.</font> The Vehicle Identification Number $vin is already in our database.";
	} else {

	require ('get_connected.php');
	mysql_query ("INSERT INTO canada (first_name, last_name, street, city, state, zip_code, phone, vehicle_year, vehicle_make, 	  	    vehicle_model, vin, date, time)  VALUES ('$first_name', '$last_name', '$street', '$city', '$state', '$zip_code', 	 	  	 	 			        '$phone','$vehicle_year', '$vehicle_make', '$vehicle_model', '$vin', '$date', '$time')") or die('Error In 	                         Query: '.mysql_error());

		echo "<b>Thank You, $first_name $last_name</b><br><p>
			  Your information was submitted on $date.";
			  			}
			  		}
				}
			}
?>

 

Thanks for any help or suggestions.

Link to comment
Share on other sites

The else statement should then process it... correct?

 

} else {

	require ('get_connected.php');
	mysql_query ("INSERT INTO canada (first_name, last_name, street, city, state, zip_code, phone, vehicle_year, vehicle_make, 	  	    vehicle_model, vin, date, time)  VALUES ('$first_name', '$last_name', '$street', '$city', '$state', '$zip_code', 	 	  	 	 			        '$phone','$vehicle_year', '$vehicle_make', '$vehicle_model', '$vin', '$date', '$time')") or die('Error In 	                         Query: '.mysql_error());

		echo "<b>Thank You, $first_name $last_name</b><br><p>
			  Your information was submitted on $date.";

 

Or am I way off base?

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.