Need help with scripting to store data on MySQL

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • luckysanj
    New Member
    • May 2009
    • 68

    #61
    Dear mideastgirl the above bytes.zip file works very nicely which is given by Mr. anfetienne so follow him.

    I have down load this above bytes.zip & copy to all byte on htdocs folder & create database which name is bytes. emport the table from above. & Change here with

    mysql_connect($ hostname,$usern ame,$password);
    @mysql_select_d b($dbname) or die( "Unable to select database");

    Replace with

    mysql_connect(" localhost","roo t"," ");
    @mysql_select_d b(''addMember) or die( "Unable to select database");

    Then after test it. It work nicely.

    Thank You

    Comment

    • anfetienne
      Contributor
      • Feb 2009
      • 424

      #62
      hi, the sample i gave is a complete working model with no errors....thank you luckysanj for testing it.

      mideastgirl please note that he changed the sql connection to suit him but you don't have to as i used what you had in your previous code.

      all that you needed to change on my code was the $_POST['changeMeHere'] to match the names you have in your table....you didn't have to change anything else, or add dollar signs because it was all written for you.

      Comment

      • mideastgirl
        New Member
        • Jun 2009
        • 65

        #63
        I went back through and just copied and pasted the files, which I HATE doing but I am apparently too unintelligent to do otherwise. I am still getting these warnings:

        Warning: session_start() [function.sessio n-start]: Cannot send session cache limiter - headers already sent (output started at /home/content/m/i/d/mideasthonors/html/addmember_db.ph p:1) in /home/content/m/i/d/mideasthonors/html/addmember_db.ph p on line 29

        Warning: Cannot modify header information - headers already sent by (output started at /home/content/m/i/d/mideasthonors/html/addmember_db.ph p:1) in /home/content/m/i/d/mideasthonors/html/addmember_db.ph p on line 31

        Here is the coding, which I am sure you already have since it is exactly what you gave me/ except without the error "error_reportin g(E_ALL);" on it since it is not for the error page
        [code=php]
        <?
        include ("admintasks-dbcon.php");


        $memberID = $_POST['memberID'];
        $institution = $_POST['institution'];
        $address1 = $_POST['address_1'];
        $address2 = $_POST['address_2'];
        $city = $_POST['city'];
        $state = $_POST['state'];
        $zipCode = $_POST['zip'];
        $website = $_POST['website'];
        $contactName = $_POST['contactName'];
        $position = $_POST['position'];
        $phoneNumber = $_POST['phone'];
        $email = $_POST['email'];

        // OPEN CONNECTION --->
        mysql_connect($ hostname,$usern ame,$password);
        @mysql_select_d b($dbname) or die( "Unable to select database");


        $sql = "SELECT * FROM addMember WHERE email = '{$email}'";
        $res = mysql_query( $sql ) or die( mysql_error );

        if ( mysql_num_rows( $res ) > 0 )
        {

        session_start() ;
        $_SESSION["contactNam e"] = $contactName;
        header("locatio n:alreadyMember .php");

        }
        else{

        $query="INSERT addMember (

        memberID,
        institution,
        address1,
        address2,
        city,
        state,
        zip,
        website,
        contactName,
        position,
        phoneNumber,
        email)

        VALUES ( '".$memberID."' ,
        '".$institution ."',
        '".$address1."' ,
        '".$address2."' ,
        '".$city."',
        '".$state."' ,
        '".$zipCode. "',
        '".$website. "',
        '".$contactName ."',
        '".$position."' ,
        '".$phoneNumber ."',
        '".$email."' )";

        $result=mysql_q uery($query) or die("Error in query:".mysql_e rror());

        session_start() ;
        $_SESSION["contactNam e"] = $contactName;
        header("locatio n:thankyouforbe comingamember.p hp");
        }

        mysql_close();
        // CLOSE CONNECTION --->
        ?>
        [/code]

        The other files were literally copy and pasted with no changes made.

        Comment

        • anfetienne
          Contributor
          • Feb 2009
          • 424

          #64
          as i said before you are getting those warnings because the form you have uses different field names to what is in the $_POST vars on the php code. some of the $_POST vars aren't being filled with the data from the form, so it leaves them with no values and not set. now because the session looks for the value within the set var it gives you an error because there isn't any values within the var.......

          thats why i have been stating for you to either change the names within the $_POST to match what you have on your forms or change all field names in your forms to match what you have in your $_POST vars

          change the name of these below to match your form field names.

          Code:
           $memberID = $_POST['memberID'];
           $institution = $_POST['institution'];
           $address1 = $_POST['address_1']; 
           $address2 = $_POST['address_2'];
           $city = $_POST['city'];
           $state = $_POST['state'];
           $zipCode = $_POST['zip'];
           $website = $_POST['website'];
           $contactName = $_POST['contactName'];
           $position = $_POST['position'];
           $phoneNumber = $_POST['phone'];
           $email = $_POST['email'];

          Comment

          • mideastgirl
            New Member
            • Jun 2009
            • 65

            #65
            The form= the table in mysql? or the form= what is on the website on http://mideasthonors.org/addmember.php?????

            Comment

            • mideastgirl
              New Member
              • Jun 2009
              • 65

              #66
              OOOOOKKKKK! So I have redone the scripting, ALL of it! And I think I did it...but now I am getting an unexpected end!!!!

              Here is my code:
              [code=php]
              <?
              // OPEN CONNECTION --->
              include ("admintasks-dbcon.php");
              mysql_connect($ hostname,$usern ame,$password);
              mysql_select_db ($dbname) or die( "Unable to select database");
              $sql = "SELECT * FROM addMember WHERE email = '{$email}'";
              $res = mysql_query( $sql ) or die( mysql_error );

              function protect ($VALUE){
              $VALUE = mysql_real_esca pe_string ($VALUE);
              $VALUE = stripslashes (VALUE);
              }
              $institution_na me = $_POST['institution_na me'];
              $address_1 = $_POST['address_1'];
              $address_2 = $_POST['address_2'];
              $city = $_POST['city'];
              $state = $_POST['state'];
              $zip_code = $_POST['zip_code'];
              $website = $_POST['website'];
              $contact_name = $_POST['contact_name'];
              $position = $_POST['position'];
              $phone_number = $_POST['phone_number'];
              $email = $_POST['email'];
              protect ($institution_n ame);
              protect ($address_1);
              protect ($address_2);
              protect ($city);
              protect ($state);
              protect ($zip_code);
              protect ($website);
              protect ($contact_name) ;
              protect ($position);
              protect ($phone_number) ;
              protect ($email);

              if(Isset($insti tution_name) && Isset($address_ 1) && Isset($address_ 2)
              && Isset($city) && Isset($state) && Isset($zip_code ) && Isset($website) && Isset($contact_ name)
              && Isset($position ) && Isset($phone_nu mber) && Isset($email)){


              $sql = "SELECT * FROM addMember WHERE institution_nam e = '$institution_n ame'";
              $res = mysql_query( $sql ) or die( mysql_error());
              if ( mysql_num_rows( $res ) > 0 ){
              echo "This instituion is already a member!";
              }else{
              $sql = "SELECT * FROM addMember WHERE email= '$email'";
              $res = mysql_query( $sql ) or die( mysql_error());
              if ( mysql_num_rows( $res ) > 0 ){
              }else{
              $sql = "INSERT INTO addMember(
              institution_nam e,
              address_1,
              address_2,
              city, state,
              zip_code,
              website,
              contact_name,
              position,
              phone_number,
              email
              )
              VALUES (
              '$instituion_na me',
              '$address_1',
              '$address_2',
              '$city',
              '$state',
              '$zip_code',
              '$website',
              '$contact_name' ,
              '$position',
              '$phone_number' ,
              '$email'
              );";
              $res = mysql_query ($sql) or die(mysql_error ());



              session_start() ;
              $_SESSION["contactNam e"] = $contactName;
              header("locatio n:alreadyMember .php");




              session_start() ;
              $_SESSION["contactNam e"] = $contactName;
              header("locatio n:thankyouforbe comingamember.p hp");
              }

              mysql_close();
              // CLOSE CONNECTION --->
              ?>
              [/code]

              Here is my table:
              CREATE TABLE `addMember` (
              `id` int(6) NOT NULL auto_increment,
              `memberID` int(12) NOT NULL default '0',
              `institution_na me` varchar(45) NOT NULL default '',
              `address_1` varchar(45) NOT NULL default '',
              `address_2` varchar(45) NOT NULL default '',
              `city` varchar(45) NOT NULL default '',
              `state` char(2) NOT NULL default '',
              `zip_code` int(10) NOT NULL default '0',
              `website` text NOT NULL,
              `contact_name` varchar(45) NOT NULL default '',
              `position` varchar(25) NOT NULL default '',
              `phone_number` int(12) NOT NULL default '0',
              `email` text NOT NULL,
              PRIMARY KEY (`id`)
              ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT= 2 ;

              --
              -- Dumping data for table `addMember`
              --

              INSERT INTO `addMember` VALUES(1, 0, '', '', '', '', 'Il', 0, 'http://www.', '', '', 0, '');

              Comment

              • Markus
                Recognized Expert Expert
                • Jun 2007
                • 6092

                #67
                You have neglected to complete one of your conditional statements, and I can see why - you format your code terribly, or should I say you don't format your code at all. Formatting maintains the readability of code, and thus the prevention and debugging of errors is greatly helped.

                See for yourself:

                Code:
                <?
                // OPEN CONNECTION --->
                include ("admintasks-dbcon.php");
                mysql_connect($hostname, $username, $password);
                mysql_select_db($dbname) or die("Unable to select database");
                $sql = "SELECT * FROM addMember WHERE email = '{$email}'";
                $res = mysql_query($sql) or die(mysql_error);
                
                function protect($VALUE) {
                    $VALUE = mysql_real_escape_string($VALUE);
                    $VALUE = stripslashes(VALUE);
                }
                $institution_name = $_POST['institution_name'];
                $address_1 = $_POST['address_1'];
                $address_2 = $_POST['address_2'];
                $city = $_POST['city'];
                $state = $_POST['state'];
                $zip_code = $_POST['zip_code'];
                $website = $_POST['website'];
                $contact_name = $_POST['contact_name'];
                $position = $_POST['position'];
                $phone_number = $_POST['phone_number'];
                $email = $_POST['email'];
                protect($institution_name);
                protect($address_1);
                protect($address_2);
                protect($city);
                protect($state);
                protect($zip_code);
                protect($website);
                protect($contact_name);
                protect($position);
                protect($phone_number);
                protect($email);
                
                if (Isset($institution_name) && Isset($address_1) && Isset($address_2) && Isset($city) && Isset($state) && Isset($zip_code) && Isset($website) && Isset($contact_name) && Isset($position) && Isset($phone_number) && Isset($email)) {
                
                    
                    $sql = "SELECT * FROM addMember WHERE institution_name = '$institution_name'";
                    $res = mysql_query($sql) or die(mysql_error());
                    if (mysql_num_rows($res) > 0) {
                        echo "This instituion is already a member!";
                    } else {
                        $sql = "SELECT * FROM addMember WHERE email= '$email'";
                        $res = mysql_query($sql) or die(mysql_error());
                        if (mysql_num_rows($res) > 0) {
                        } else {
                            $sql = "INSERT INTO addMember(
                institution_name, 
                address_1, 
                address_2, 
                city, state,
                zip_code, 
                website, 
                contact_name, 
                position, 
                phone_number, 
                email
                )
                VALUES (
                '$instituion_name',
                '$address_1',
                '$address_2',
                '$city',
                '$state',
                '$zip_code',
                '$website',
                '$contact_name',
                '$position',
                '$phone_number',
                '$email'
                );";
                            $res = mysql_query($sql) or die(mysql_error());
                
                            
                            session_start();
                            $_SESSION["contactName"] = $contactName;
                            header("location:alreadyMember.php");
                
                            
                            session_start();
                            $_SESSION["contactName"] = $contactName;
                            header("location:thankyouforbecomingamember.php");
                        }
                
                    // Just by the level of indentation, you can tell the closing '}'
                    // should be around this point.
                        
                        mysql_close();
                        // CLOSE CONNECTION --->
                        
                ?>

                Comment

                • mideastgirl
                  New Member
                  • Jun 2009
                  • 65

                  #68
                  my scripting looks nothing like what you posted, how the heck did it come out like that?!

                  Comment

                  • Markus
                    Recognized Expert Expert
                    • Jun 2007
                    • 6092

                    #69
                    Originally posted by mideastgirl
                    my scripting looks nothing like what you posted, how the heck did it come out like that?!
                    I formatted it in my IDE (Aptana Studio with PHP plugin).

                    Formatting your code is a big part of writing it - it increases readability and thus maintainence is much easier.

                    Comment

                    • mideastgirl
                      New Member
                      • Jun 2009
                      • 65

                      #70
                      Gotcha! I added the extra } because you were right I was missing some. However now instead of the unexpected $end I am just getting a blank page:( It is not going on to the next page that says thank you for becoming a member?!

                      Any ideas on that?

                      Comment

                      • Markus
                        Recognized Expert Expert
                        • Jun 2007
                        • 6092

                        #71
                        Nothing jumps out at me - try some debugging.

                        When you're using conditional statements, it's always beneficial, in terms of debugging, to know which conditionals fail, and which don't. You can then determine where your script has been, and where it ended.

                        Try the following and see if you get any output (we're looking for either 'not going to thank you', or 'going to thank you'). From there, we can narrow down the causes of your problem.

                        Code:
                        <?
                        // OPEN CONNECTION --->
                        include ("admintasks-dbcon.php");
                        mysql_connect($hostname, $username, $password);
                        mysql_select_db($dbname) or die("Unable to select database");
                        $sql = "SELECT * FROM addMember WHERE email = '{$email}'";
                        $res = mysql_query($sql) or die(mysql_error);
                         
                        function protect($VALUE) {
                            $VALUE = mysql_real_escape_string($VALUE);
                            $VALUE = stripslashes(VALUE);
                        }
                        $institution_name = $_POST['institution_name'];
                        $address_1 = $_POST['address_1'];
                        $address_2 = $_POST['address_2'];
                        $city = $_POST['city'];
                        $state = $_POST['state'];
                        $zip_code = $_POST['zip_code'];
                        $website = $_POST['website'];
                        $contact_name = $_POST['contact_name'];
                        $position = $_POST['position'];
                        $phone_number = $_POST['phone_number'];
                        $email = $_POST['email'];
                        protect($institution_name);
                        protect($address_1);
                        protect($address_2);
                        protect($city);
                        protect($state);
                        protect($zip_code);
                        protect($website);
                        protect($contact_name);
                        protect($position);
                        protect($phone_number);
                        protect($email);
                         
                        if (Isset($institution_name) && Isset($address_1) && Isset($address_2) && Isset($city) && Isset($state) && Isset($zip_code) && Isset($website) && Isset($contact_name) && Isset($position) && Isset($phone_number) && Isset($email)) {
                         
                         
                            $sql = "SELECT * FROM addMember WHERE institution_name = '$institution_name'";
                            $res = mysql_query($sql) or die(mysql_error());
                            if (mysql_num_rows($res) > 0) {
                                echo "This instituion is already a member!";
                            } else {
                                $sql = "SELECT * FROM addMember WHERE email= '$email'";
                                $res = mysql_query($sql) or die(mysql_error());
                                if (mysql_num_rows($res) > 0) {
                                    echo "not going to thank you - institution name already exists';
                                } else {
                                    echo 'going to thank you - institution name doesn\'t exist';
                                    $sql = "INSERT INTO addMember(
                        institution_name, 
                        address_1, 
                        address_2, 
                        city, state,
                        zip_code, 
                        website, 
                        contact_name, 
                        position, 
                        phone_number, 
                        email
                        )
                        VALUES (
                        '$instituion_name',
                        '$address_1',
                        '$address_2',
                        '$city',
                        '$state',
                        '$zip_code',
                        '$website',
                        '$contact_name',
                        '$position',
                        '$phone_number',
                        '$email'
                        );";
                                    $res = mysql_query($sql) or die(mysql_error());
                         
                         
                                    session_start();
                                    $_SESSION["contactName"] = $contactName;
                                    header("location:alreadyMember.php");
                         
                         
                                    session_start();
                                    $_SESSION["contactName"] = $contactName;
                                    header("location:thankyouforbecomingamember.php");
                                }
                         
                            // Just by the level of indentation, you can tell the closing '}'
                            // should be around this point.
                         
                                mysql_close();
                                // CLOSE CONNECTION --->
                         
                        ?>

                        Comment

                        Working...