Jump to content


Photo

need help


  • Please log in to reply
12 replies to this topic

#1 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 13 June 2006 - 09:12 PM

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 script
if (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.

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#2 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 13 June 2006 - 09:16 PM

[!--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]

$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
}

How that might fit into your script is something I'll leave up to you.
Legend has it that reading the manual never killed anyone.
My site

#3 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 13 June 2006 - 09:23 PM

$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
}

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.

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#4 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 13 June 2006 - 09:27 PM

$query = "SELECT * from tablename WHERE something = '$somevalue'";
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 ...

$query = "SELECT * from tablename WHERE something = '$somevalue' AND something_else = '$some_other_value'";

Legend has it that reading the manual never killed anyone.
My site

#5 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 13 June 2006 - 09:31 PM

So with me trying to insert it into my current script it should look something roughly like this

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;
    }
  }
} 
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

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#6 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 13 June 2006 - 09:47 PM

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).
Legend has it that reading the manual never killed anyone.
My site

#7 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 13 June 2006 - 09:55 PM

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
<?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 script
if (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.


         
?>
Below here is what is changed in it

$query = "SELECT * from signoninfo WHERE name = '$_POST['name']' AND email = '$_POST['email']'";
$result = mysql_query($query);
$matches = @mysql_numrows($result);
if ($matches == 0) {
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

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

line 39 is
    $query = "SELECT * from signoninfo WHERE name = '$_POST['name']' AND email = '$_POST['email']'";

Any advice on how to make this work.

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#8 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 13 June 2006 - 10:12 PM

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.

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) {

I could really use some quick help on this, thanks.

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#9 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 13 June 2006 - 10:41 PM

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\" /]

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#10 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 14 June 2006 - 06:31 AM

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.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#11 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 14 June 2006 - 12:18 PM

what are you talking about quotiing issues, and they ask you to repost code when you make too many changes.

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#12 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 15 June 2006 - 05:07 PM

[!--quoteo(post=383732:date=Jun 14 2006, 08:18 AM:name=businessman332211)--][div class=\'quotetop\']QUOTE(businessman332211 @ Jun 14 2006, 08:18 AM) View Post[/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.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#13 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 15 June 2006 - 05:10 PM

ah I see what you mean now, thanks.

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users