Jump to content

HELP


Recommended Posts

How do I check a database to see if something exists, I have my whole script worked out, it works perfectly but I don't know how to get it to where it checks the database first.

<?php// This is a page that holds 2 different scripts, this completely controls the results coming from the form on signup.php, the purpose of these 2 scripts is to detect which button was pressed, if subscribe was pressed the top set of scripts under the giant if statement run.  If unsubscribe is pressed then the if statement corresponding to unsubscribe is ran.// Begin subscribe scriptif (isset($_POST['subscribe'])) {  // tests if subscribe was clicked on, on the form page$errorhandler = ""; // This will control holding error information$management = true; // Gives me full control over when the script runs and stops$regex = "^[A-Za-z0-9\._-]+@([A-Za-z0-9][A-Za-z0-9-]{1,62})(\.[A-Za-z][A-Za-z0-9-]{1,62})+$";	// regex variables has a regex value initialized into it, this allows me to quickly run	// the regex functions through the email field to test it's format.  It makes sure it 	// contacts an at symbol, and a period, and in the proper places.	if ($_POST['name'] == "") { // checks if name is empty, and puts error info into string  $errorhandler .= "The Name Field was not filled in correctly<br />";	}	if ($_POST['email'] == "") { // checks if email is blank  $errorhandler .= "The Email Field was not filled in correctly<br />";	}	if ($_POST['email'] != $_POST['verifyemail']) { //checks if email fields match  $errorhandler .= "The email address DO NOT match<br />";	}	if (!ereg("$regex", $_POST['email'])) { //tests emails format with regex variable above  $errorhandler .= "The email address is improperly formatted<br />";	}	if ($errorhandler != "") { //This tests the error handler to see if it has recieved  print "$errorhandler"; //any information or not, if it has then it prints the errors  $management = false;  // and changes management to false, which prevents other parts of	}    // the script from running later on down the road	if($management == true) {// This is were management first comes into play.  $connect = mysql_connect("localhost", "####", "######"); //connects to db  $select = mysql_select_db("######"); // selects db 	 if (!$connect || !$select){ //Tests the connection and sellection 	 echo "There was a problem interacting with the database please contact us at "; 	 echo "info@theyellowpagesnetwork.com";  // If it doesn't connect or select it returns 	 $management = false; //errors and changes the management to prevent action during other 	 } //parts of the script  }  // It gets closed 2 times, once for the first if, a second for the other if statement 	   if($management == true) { 	 $inserter = "INSERT INTO signoninfo (name, email) VALUES ('" . mysql_real_escape_string($_POST['name']) . "', '" . mysql_real_escape_string($_POST['email']) . "')"; 	 if(mysql_query($inserter)){  	 $management = true; 	 }else { 	 echo "There was some sort of error interacting with the database.<br />"; 	 echo "Your information was not inserted into it for some unknown reason.<br />"; 	 echo "Please contact us at info@theyellowpagesnetwork.com and notify us of this<br />"; 	 echo "we thank you for your patience!<br />"; 	 $management = false; 	 }  }   	if($management == true) { // start major if mail system construct 	 $mailmanager = true; // sets another manager to manage all email scripts    if($mailmanager == true) {  //tests for the mail manager script    $to = "{$_POST['email']}"; // sets where the email goes to    $subject = "Funny Email Forwards News Letter Notification";  // sets the subject    $message = " This is to notify {$_POST['name']} that you have signed up to ";    $message .= "Funny Email Forwards at {$_POST['email']}.  You should be ";    $message .= "Recieving an email within the next hour that will give you a full list ";    $message .= "of all funny related entries from our database. ";    $message .= "If you do not recieve the other email within 24 hours please contact ";    $message .= "info@theyellowpagesnetwork.com and we will manually send you one.";    // everything under the message variables sets the contents for the mail   	 if(mail($to, $subject, $message)){ // opens up another if construct for email to user   	 echo "Thank you for signing up to the news letter<br />"; // confirmation message   	 echo "You should recieve a confirmation and first letter within 3 hours"; //same here   	 $mailmanager = true; // sets mail manager for use with script below this one   	 }else { // if it doesn't mail then instead it does this   	 echo "There was some sort of problem sending you a confirmation email"; // error message   	 echo "this problem is direcly relating to sending you a confirmation email"; //same   	 echo "I apologize for this error, please contact us at info@theyellowpagesnetwork.com";   	 echo "and we will manually get the process started for you, thanks for your"; //same   	 echo "time and patience"; // same   	 $mailmanager = false; // sets mail manager to false to discontinue the script later      } //closes the else construct above   	 }// closes the bigger if construct containing mail function          if($mailmanager = true){ // starts admin mail section      $to = "businessman332211@hotmail.com"; // sets email to send to      $subject = "Funny Email Forwards Subscription"; // sets subject      $message = "This is to inform you that {$_POST['name']} has signed up to the ";      $message .= "newsletter under {$_POST['email']} at Funny Email Forwards.com.";      // sets email message      mail($to, $subject, $message); // send an email to admin  } //  final if, encloses all coding in admin mail section	} // closes entire mail related script} // closes entire section pertaining to subscribe // End Subscribe Script	//Begin Unsubscribe Script// The script below here is used if someone clicked on unsubscribe on the form. 	 ?>

someone please help me with this, I learnt a lot but I still ran into this problem, i have a few ideas but none of them worked. I wish they just allower LIMIT 1 to work in this situation, it would be a whole lot easier, than forcing another way. Any advice thanks.

Link to comment
Share on other sites

Do you want to check if the username or email already exists in the DB before the INSERT statement in your code?Try this...$query = "SELECT * FROM tblname WHERE uniquefldname='$phpvariable'";$result = mysql_query($query);$numrows = mysql_num_rows($result);if($numrows == 0){//INSERT SQL query will be here...mysql_query($inserter);}else{echo "sorry that user already exsists for that course";}

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...