Ninjakreborn Posted June 13, 2006 Share Posted June 13, 2006 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.[code]<?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. ?>[/code]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. Quote Link to comment Share on other sites More sharing options...
AndyB Posted June 13, 2006 Share Posted June 13, 2006 [!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--] 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.[/quote][code]$query = "SELECT * from tablename WHERE something = '$somevalue'";$result = mysql_query($query) or die("Error: ". mysql_error(). " with query ". $query);$matches = @mysql_numrows($result);if ($matches > 0) { // this/these already exists} else { // do something different}[/code]How that might fit into your script is something I'll leave up to you. Quote Link to comment Share on other sites More sharing options...
Ninjakreborn Posted June 13, 2006 Author Share Posted June 13, 2006 [code]$query = "SELECT * from tablename WHERE something = '$somevalue'";$result = mysql_query($query) or die("Error: ". mysql_error(). " with query ". $query);$matches = @mysql_numrows($result);if ($matches == 1) { // this already exists} else { // do something different}[/code]This is what my question is here, I saw this before, I know the $query is going to do something but how would I use it with my 2 values, I am still learning a lot here, but this is something I will need a little boost with, I need to fit it into my script but I can work that out for the most part what i need help is, how do I test the 2 values I have in my script, with them being $_POST[''] form fields and all, I just don't understand what this is doing. Quote Link to comment Share on other sites More sharing options...
AndyB Posted June 13, 2006 Share Posted June 13, 2006 [code]$query = "SELECT * from tablename WHERE something = '$somevalue'";[/code]That will yield a result with one (or more) rows where the value in the field named 'something' has the value of '$somevalue', i.e. that already exists. To check for more than one ...[code]$query = "SELECT * from tablename WHERE something = '$somevalue' AND something_else = '$some_other_value'";[/code] Quote Link to comment Share on other sites More sharing options...
Ninjakreborn Posted June 13, 2006 Author Share Posted June 13, 2006 So with me trying to insert it into my current script it should look something roughly like this[code]if($management == true) {$query = "SELECT * from signoninfo WHERE name = '$_POST['name']' AND email = '$_POST['email']'";$result = mysql_query($query)$matches = @mysql_numrows($result);if ($matches == 1) {$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; } }} [/code]like that, in my current script, if this is how it goes, then how does $matches == 1, I am sorry for all the questions but my php/mysql knowledge has tripled this past week, and now I keep running into barriers, I am trying to sort through it and learn at the same time as I go along. Thanks Quote Link to comment Share on other sites More sharing options...
AndyB Posted June 13, 2006 Share Posted June 13, 2006 Heading in the right direction but ... if you get a match then that pair of data already exists and I presume you DON'T want to add a duplicate. I think you want the case where $matches == 0 (i.e. NO matches, so it must be a unique data set). Quote Link to comment Share on other sites More sharing options...
Ninjakreborn Posted June 13, 2006 Author Share Posted June 13, 2006 it's not working I have this here.It is giving me an error, it was working before I changed it, the added part is at the bottom[code]<?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", "funnyemail", "8Ua^1jv"); //connects to db $select = mysql_select_db("funnyemailforwards"); // 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) { $query = "SELECT * from signoninfo WHERE name = '$_POST['name']' AND email = '$_POST['email']'";$result = mysql_query($query);$matches = @mysql_numrows($result);if ($matches == 0) { $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. ?>[/code]Below here is what is changed in it[code]$query = "SELECT * from signoninfo WHERE name = '$_POST['name']' AND email = '$_POST['email']'";$result = mysql_query($query);$matches = @mysql_numrows($result);if ($matches == 0) {[/code]and hte other closing bracket below that.This is what I need some help for.I don't understand why it's working it's giving me this error[code]Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/all/funnyemailforwards/public_html/apex/signupprocessor.php on line 39[/code]line 39 is[code] $query = "SELECT * from signoninfo WHERE name = '$_POST['name']' AND email = '$_POST['email']'";[/code]Any advice on how to make this work. Quote Link to comment Share on other sites More sharing options...
Ninjakreborn Posted June 13, 2006 Author Share Posted June 13, 2006 I tried doing some things to it I had learnt, and it still didn't work, these are the changes I made to just that same part of script I get the same error message.[code]if($management == true) {$query = "SELECT * from signoninfo WHERE name = " . "$_POST['name']" . "AND email =" . "$_POST['email']";$result = mysql_query($query);$matches = mysql_numrows($result);if ($matches == 0) {[/code]I could really use some quick help on this, thanks. Quote Link to comment Share on other sites More sharing options...
Ninjakreborn Posted June 13, 2006 Author Share Posted June 13, 2006 doesn't anyone have any advice on this at all [img src=\"style_emoticons/[#EMO_DIR#]/excl.gif\" style=\"vertical-align:middle\" emoid=\":excl:\" border=\"0\" alt=\"excl.gif\" /] Quote Link to comment Share on other sites More sharing options...
fenway Posted June 14, 2006 Share Posted June 14, 2006 First, you really have to try and refrain from posting entire scripts over and over again. Second, you've got quoting issues, from what I can see. Quote Link to comment Share on other sites More sharing options...
Ninjakreborn Posted June 14, 2006 Author Share Posted June 14, 2006 what are you talking about quotiing issues, and they ask you to repost code when you make too many changes. Quote Link to comment Share on other sites More sharing options...
fenway Posted June 15, 2006 Share Posted June 15, 2006 [!--quoteo(post=383732:date=Jun 14 2006, 08:18 AM:name=businessman332211)--][div class=\'quotetop\']QUOTE(businessman332211 @ Jun 14 2006, 08:18 AM) [snapback]383732[/snapback][/div][div class=\'quotemain\'][!--quotec--]what are you talking about quotiing issues, and they ask you to repost code when you make too many changes.[/quote]Look at your line 39 -- you can't have nested single quotes. Quote Link to comment Share on other sites More sharing options...
Ninjakreborn Posted June 15, 2006 Author Share Posted June 15, 2006 ah I see what you mean now, thanks. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.