Need help with scripting to store data on MySQL

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • anfetienne
    Contributor
    • Feb 2009
    • 424

    #31
    there are also errors on your form and error in sql connection....

    e.g.

    zip code on your form is named zip and in your php zip code

    Comment

    • mideastgirl
      New Member
      • Jun 2009
      • 65

      #32
      Ok I have updated the php document I am still returning a mysql error. I added member ID as a column in my table, I have updated zip to zip code within the table as well. I have added quotes around the values. I am not sure what I should do next!
      [code=php]
      <?php
      include ("admintasks-dbcon.php");
      mysql_connect($ hostname,$usern ame, $password) OR DIE ('Unable to connect to database! Please try again later.');
      mysql_select_db ($dbname);
      $query = "SELECT * FROM add_member WHERE memberID = '{$memberID}'";
      $result = mysql_query( $sql ) or die( mysql_error );

      if ( mysql_num_rows( $res ) > 0 )
      {

      $query="
      SELECT add_member SET
      name = $_POST ['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 Address = $_POST ['email address'];)

      WHERE memberID ='{$memberID}'" ;
      }
      else{

      $query="

      INSERT INTO add_member (

      'Member ID',
      'Name',
      'Address 1',
      'Address 2',
      'City',
      'State',
      'Zip Code',
      'Website',
      'Contact Name',
      'Position',
      'Phone Number',
      'Email Address')

      VALUES

      '{$_POST['memberID']}',
      '{$_POST['name']}',
      '{$_POST['address 1']}',
      '{$_POST['address 2']}',
      '{$_POST['city']}',
      '{$_POST['state']}',
      '{$_POST['zip code']}',
      '{$_POST['website']}',
      '{$_POST['contact name']}',
      '{$_POST['position']}',
      '{$_POST['phone number']}',
      '{$_POST['email address']}')";

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

      if ( mysql_num_rows( $result ) > 0 )
      echo mysql_affected_ rows()." row inserted into the database effectively.";

      mysql_close();
      }

      session_start() ;
      $_SESSION["Name"] = $_POST['name'];
      header("locatio n:thankyouforbe comingamember.p hp");
      ?>
      [/code]

      Here is the table:

      Institution MemberID Address 1 Address 2 City State Zip Code Website Contact Name Position Phone Number Email

      Comment

      • anfetienne
        Contributor
        • Feb 2009
        • 424

        #33
        correct and complete code

        ok i have corrected everything from your form down to the database. here is the coding of each page. they are saved as the name states as php files (i have attached a zip with all files).....

        to see it in action go to database test

        index.php (form page)

        Code:
         <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
         <html xmlns="http://www.w3.org/1999/xhtml">
          
         <head>
         <meta http-equiv="Content-Language" content="en-us" />
         <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
         <title>Error</title>
         <style type="text/css">
         .style1 {
             font-family: Arial, Helvetica, sans-serif;
             font-size: medium;
         }
          
         .style29 {
             font-family: Arial, Helvetica, sans-serif;
         }
         h2 {
             font-family: Verdana, Arial, Helvetica, sans-serif;
             font-size: 17px;
             font-weight: bold;
             color: #2F77F1;
             line-height: 20px;
         }
         .style30 {
             font-family: geneva, arial;
             font-size: 10pt;
         }
          
         .style31 {
             font-family: Verdana, Arial, Helvetica, sans-serif;
             font-size: 12px;
             line-height: 11pt;
             margin: 3px 0;
             padding-bottom: 9px;
         }
         </style>
         </head>
          
         <body>
          <?php $memberID = rand(0000000000,9999999999);?>
         <p class="style1"><strong>Error: All fields have not been filled in, or information is inaccurate. Please complete form.</strong></p>
         <form method="post" action="addmember.php" style="width: 169px">
         *<input type="hidden" name="memberID" id="memberID" value="<?php print $memberID;?>" />
          
             <table>
          
                 <tr>
                     <td colspan=2><h2 class="style29">Institution Information</h2></td>
                 </tr>
          
                 <tr>
                     <td class='style30'><p class="style31"><label for='name'>Institution*</label></p></td>
                     <td class="style30"><input type="text" name="institution" size=45 maxlength=100 value="" /></td>
                 </tr>
          
                 <tr>
                     <td class='style30'><p class="style31"><label for='address_1'>Address*</label></p></td>
                     <td class="style30"><input type="text" name="address1" size=45 maxlength=100 value="" /></td>
                 </tr>
          
                 <tr>
                     <td class="style30">&nbsp;</td>
                     <td class="style30"><input type="text" name="address2" size=45 maxlength=100 value="" /></td>
                 </tr>
          
                 <tr>
                     <td class='style30'><p class="style31"><label for='city'>City*</label></p></td>
                     <td class="style30"><input type="text" name="city" size=45 maxlength=50 value="" /></td>
                 </tr>
          
                 <tr>
                     <td class='style30'><p class="style31"><label for='state'>State*</label>
                     <td class="style30">
                         <select name='state'>
                         <?php
                         $states = array("Illinois", "Indiana", "Kentucky", "Michigan", "Ohio", "Pennsylvania", "Tennessee", "West Viriginia");
                         foreach($states as $a => $value){
                             echo "<option name=".$value ." value=".$value .">$value</option>";
                         }
                         ?>
                         </select>
                     </td>
                 </tr>
          
                 <tr>
                     <td class='style30'><p class="style31"><label for='zip'>ZIP Code*</label></p></td>
                     <td class="style30"><input type="text" name="zip" size=10 maxlength=10 value="" /></td>
                 </tr>
          
                 <tr>
                     <td class='style30'><p class="style31"><label for='website'>Website*</label></p></td>
                     <td class="style30"><input type="text" name="website" value="http://www." size=45 maxlength=100 value="" /></td>
                 </tr>
          
                 <tr>
                     <td colspan=2><h2 class="style29">Contact Information</h2></td>
                 </tr>
          
                 <tr>
                     <td class='style30'><p class="style31"><label for='contact_name'>Contact Name*</label></p></td>
                     <td class="style30"><input type="text" name="contactName" size=45 maxlength=100 value="" /></td>
                 </tr>
          
                 <tr>
                     <td class='style30'><p class="style31"><label for='contact_position'>Position</label></p></td>
                     <td class="style30"><input type="text" name="position" size=45 maxlength=100 value="" /></td>
                 </tr>
          
                 <tr>
                     <td class='style30'><p class="style31"><label for='contact_phone'>Phone Number*</label></p></td>
                     <td class="style30"><input type="text" name="phone" size=14 maxlength=14 value="" /></td>
                 </tr>
          
                 <tr>
                     <td class='style30'><p class="style31"><label for='contact_email'>Email Address*</label></p></td>
                     <td class="style30"><input type="text" name="email" size=45 maxlength=100 value="" /></td>
                 </tr>
          
                 <tr>
                     <td class="style30">&nbsp;</td>
                     <td class="style30"><input type="submit" name ="Add Member" value="Add Member" /></td>
                 </tr>
          
            </table>
        addmember.php (please note how i assigned variables to each $_POST and used lower case on all first words the capitals on second words with no spaces this helps it be read easier with no confusion. The UPDATE query has been removed as its for signups and take note of how i put the variables in single and double quotes for the values in the INSERT query. this is how it should have been and this is what you were missing before along with all other mistakes and naming mistakes)

        Code:
        <? error_reporting(E_ALL);
        
        include ("admintasks-dbcon.php");
        
        $memberID = $_POST['memberID'];
        $institution = $_POST['institution'];
        $address1 = $_POST['address1']; 
        $address2 = $_POST['address2'];
        $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,$username,$password);
        @mysql_select_db($database) 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["contactName"] = $contactName;
        header("location: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_query($query) or die("Error in query:".mysql_error()); 
        
        session_start();
        $_SESSION["contactName"] = $contactName;
        header("location:thankYou.php");
        }
        
        mysql_close();
        
        ?>
        thankYou.php (please note the session start at the top and how their name is including on the page)

        Code:
        <?
        session_start();
        $contactName = $_SESSION["contactName"];
        ?>
        
        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
        <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Untitled Document</title>
        </head>
        
        <body>
        Thank You <?php echo $contactName;?>... You have signed up SUCCESSFULLY!
        </body>
        </html>
        alreadyMember.p hp (note the same as above)

        Code:
        <?
        session_start();
        $contactName = $_SESSION["contactName"];
        ?>
        
        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
        <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Untitled Document</title>
        </head>
        
        <body>
        We're Sorry <?php echo $contactName;?>... But it seems that you are already a member!
        </body>
        </html>
        MySql Table Structure (please take note of the columns ive included and as above how i have named them lower case first words wit Capital second words for ease....please delete the add_member table you already have and import the addMember.sql file included within the attached zip file)

        Code:
        -- phpMyAdmin SQL Dump
        -- version 2.11.9.5
        -- http://www.phpmyadmin.net
        --
        -- Host: localhost
        -- Generation Time: Jul 27, 2009 at 10:47 AM
        -- Server version: 5.0.77
        -- PHP Version: 5.2.6
        
        SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
        
        --
        -- Database: `theau10_resources`
        --
        
        -- --------------------------------------------------------
        
        --
        -- Table structure for table `addMember`
        --
        
        CREATE TABLE IF NOT EXISTS `addMember` (
          `id` int(6) NOT NULL auto_increment,
          `memberID` int(12) NOT NULL,
          `institution` varchar(45) NOT NULL,
          `address1` varchar(45) NOT NULL,
          `address2` varchar(45) NOT NULL,
          `city` varchar(45) NOT NULL,
          `state` varchar(2) NOT NULL,
          `zip` int(10) NOT NULL,
          `website` text NOT NULL,
          `contactName` varchar(45) NOT NULL,
          `position` varchar(25) NOT NULL,
          `phoneNumber` int(12) NOT NULL,
          `email` text NOT NULL,
          PRIMARY KEY  (`id`)
        ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
        
        --
        -- Dumping data for table `addMember`
        --
        admintasks-dbcon.php (update this to your details)

        Code:
        <?
        $hostname="localhost";
        $username="";
        $password="";
        $dbname="";
        ?>
        Attached Files

        Comment

        • mideastgirl
          New Member
          • Jun 2009
          • 65

          #34
          As much as I would love to tell you that worked....it didn't! Sorry!

          This is the error I got:

          Notice: Undefined index: memberID in /home/content/m/i/d/mideasthonors/html/addmember_db.ph p on line 6

          Notice: Undefined index: institution in /home/content/m/i/d/mideasthonors/html/addmember_db.ph p on line 7

          Notice: Undefined index: address1 in /home/content/m/i/d/mideasthonors/html/addmember_db.ph p on line 8

          Notice: Undefined index: address2 in /home/content/m/i/d/mideasthonors/html/addmember_db.ph p on line 9

          Notice: Undefined index: contactName in /home/content/m/i/d/mideasthonors/html/addmember_db.ph p on line 14

          Notice: Undefined index: position in /home/content/m/i/d/mideasthonors/html/addmember_db.ph p on line 15

          Notice: Undefined index: phone in /home/content/m/i/d/mideasthonors/html/addmember_db.ph p on line 16

          Notice: Undefined index: email in /home/content/m/i/d/mideasthonors/html/addmember_db.ph p on line 17

          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 30

          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 32

          This is what is in that file:
          [code=php]
          <? error_reporting (E_ALL);

          include ("admintasks-dbcon.php");


          $memberID = $_POST['memberID'];
          $institution = $_POST['institution'];
          $address1 = $_POST['address1'];
          $address2 = $_POST['address2'];
          $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]

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #35
            if there's no post (aka $_POST is empty) there will be no indexes to access giving you these errors.

            the header error is a direct result of the previous error messages, no need to worry now.

            Comment

            • mideastgirl
              New Member
              • Jun 2009
              • 65

              #36
              so something is wrong with my header is what you are telling me???

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #37
                Originally posted by mideastgirl
                so something is wrong with my header is what you are telling me???
                nope, just the opposite. the error messages about "undefined index" (which counts as output) cause your header function to throw out these header errors.

                Comment

                • mideastgirl
                  New Member
                  • Jun 2009
                  • 65

                  #38
                  oh. What can I do to fix that???

                  Comment

                  • Dormilich
                    Recognized Expert Expert
                    • Aug 2008
                    • 8694

                    #39
                    Originally posted by mideastgirl
                    oh. What can I do to fix that???
                    fix the other errors

                    Comment

                    • mideastgirl
                      New Member
                      • Jun 2009
                      • 65

                      #40
                      oh you are so vague! Ok so I would go about fixing the undefined indexes HOW!!!!!

                      Comment

                      • anfetienne
                        Contributor
                        • Feb 2009
                        • 424

                        #41
                        you might be getting the errors because you haven't changed the names in your form therefore its not populating the $_POST.

                        i gave you complete files from the form to sql....and i also gave a link to show exactly how it works. everything starts with the form so check to make sure the field names corresponding to the errored $_POST are correctly named the same as the $_POST.



                        download the attachment from my post and just copy all files.....renam e them as you wish but there is no need to change the coding. but the must do is import the sql.

                        let me know and send a test link when you have done that

                        Comment

                        • Dormilich
                          Recognized Expert Expert
                          • Aug 2008
                          • 8694

                          #42
                          Originally posted by mideastgirl
                          oh you are so vague!
                          am I? I'd consider my answers rather clear, though I try to point you in the direction of the solution rather than posting code.

                          btw, programming does require thinking* (though a logical mind helps a lot). copy & paste won't get you too far.

                          * - compare it to games (say, chess). you don't become a master without effort. you have to play over and over again.

                          Comment

                          • anfetienne
                            Contributor
                            • Feb 2009
                            • 424

                            #43
                            i wouldn't go as far as calling dorm vague....he is right and he has a big point.....i posted my test codes which work 100% which you can see at the link i've given twice but you changed parts of the coding and have got errors again.

                            i don't know exactly why you got the errors but its because the $_POST weren't being populated and that logically means its a form error....the same as you had before by not using the correct names in your php that you had on your form. i corrected this and gave you everything but you haven't followed it and changed it.

                            so to advise you go back to the form and double check the names. post a link to your test so we can see it please

                            Comment

                            • mideastgirl
                              New Member
                              • Jun 2009
                              • 65

                              #44
                              I am not sure how to post a link to my test, and I did not change anything from your code. The only portion I changed was the thank you name of the php.

                              Comment

                              • anfetienne
                                Contributor
                                • Feb 2009
                                • 424

                                #45
                                did you use the form i changed and did you import the sql file to your database?

                                Comment

                                Working...