I am new to PHP. I am following this tutorial for creating an address book, I've copied the code exactly and yet I keep on getting these Notice's. I take it displays the message because I am using variable names that have not yet previously defined. Is there a way to resolve this? Is this showing because I have PHP set to show all errors and warnings?

Here is a list of messages I've been getting:

Notice: Use of undefined constant op - assumed 'op' in F:\WEBROOT\public\main\PHP\AddressBook\addentry.php on line 2

Notice: Use of undefined constant op - assumed 'op' in F:\WEBROOT\public\main\PHP\AddressBook\addentry.php on line 51

Notice: Use of undefined constant first_name - assumed 'first_name' in F:\WEBROOT\public\main\PHP\AddressBook\addentry.php on line 54

Notice: Use of undefined constant last_name - assumed 'last_name' in F:\WEBROOT\public\main\PHP\AddressBook\addentry.php on line 54

Notice: Use of undefined constant address - assumed 'address' in F:\WEBROOT\public\main\PHP\AddressBook\addentry.php on line 75

Notice: Use of undefined constant telephone_number - assumed 'telephone_number' in F:\WEBROOT\public\main\PHP\AddressBook\addentry.php on line 86

Notice: Use of undefined constant mobile_number - assumed 'mobile_number' in F:\WEBROOT\public\main\PHP\AddressBook\addentry.php on line 94

Notice: Use of undefined constant email - assumed 'email' in F:\WEBROOT\public\main\PHP\AddressBook\addentry.php on line 102

Notice: Use of undefined constant note - assumed 'note' in F:\WEBROOT\public\main\PHP\AddressBook\addentry.php on line 110

    wow, that fixed everything except for this one

    Notice: Undefined index: op in F:\WEBROOT\public\main\PHP\AddressBook\addentry.php on line 2

      That means you're specifying "op" as the index of an array somewhere, but that index doesn't exist. Let's see your code and we'll figure out the problem in a flash!

        4 years later

        Well, I've got the same problem with the same script..Here is the code:

        <?php
             if ($_POST[op] != "add") {
                //haven't seen the form, so show it
                $display_block = "<h1>Add an Entry</h1>
               <form method=\"post\" action=\"$_SERVER[PHP_SELF]\">
               <P><strong>First/Last Names:</strong><br>
               <input type=\"text\" name=\"f_name\" size=30 maxlength=75>
               <input type=\"text\" name=\"l_name\" size=30 maxlength=75>
        
          <P><strong>Address:</strong><br>
          <input type=\"text\" name=\"address\" size=30>
        
          <P><strong>City/State/Zip:</strong><br>
          <input type=\"text\" name=\"city\" size=30 maxlength=50>
          <input type=\"text\" name=\"state\" size=5 maxlength=2>
          <input type=\"text\" name=\"zipcode\" size=10 maxlength=10>
        
          <P><strong>Address Type:</strong><br>
          <input type=\"radio\" name=\"add_type\" value=\"home\" checked> home
          <input type=\"radio\" name=\"add_type\" value=\"work\"> work
         <input type=\"radio\" name=\"add_type\" value=\"other\"> other
        
         <P><strong>Telephone Number:</strong><br>
         <input type=\"text\" name=\"tel_number\" size=30 maxlength=25>
         <input type=\"radio\" name=\"tel_type\" value=\"home\" checked> home
         <input type=\"radio\" name=\"tel_type\" value=\"work\"> work
         <input type=\"radio\" name=\"tel_type\" value=\"other\"> other
        
         <P><strong>Fax Number:</strong><br>
         <input type=\"text\" name=\"fax_number\" size=30 maxlength=25>
         <input type=\"radio\" name=\"fax_type\" value=\"home\" checked> home
         <input type=\"radio\" name=\"fax_type\" value=\"work\"> work
         <input type=\"radio\" name=\"fax_type\" value=\"other\"> other
        
         <P><strong>Email Address:</strong><br>
         <input type=\"text\" name=\"email\" size=30 maxlength=150>
         <input type=\"radio\" name=\"email_type\" value=\"home\" checked> home
         <input type=\"radio\" name=\"email_type\" value=\"work\"> work
         <input type=\"radio\" name=\"email_type\" value=\"other\"> other
        
         <P><strong>Personal Note:</strong><br>
         <textarea name=\"note\" cols=35 rows=5 wrap=virtual></textarea>
          <input type=\"hidden\" name=\"op\" value=\"add\">
        
         <p><input type=\"submit\" name=\"submit\" value=\"Add Entry\"></p>
         </FORM>";
        
          } else if ($_POST[op] == "add") {
             //time to add to tables, so check for required fields
              if (($_POST[f_name] == "") || ($_POST[l_name] == "")) {
                 header("Location: addentry.php");
                exit;
              }
        
          //connect to database
          $conn = mysql_connect("localhost", "hari", "hari")
              or die(mysql_error());
          mysql_select_db("hari",$conn) or die(mysql_error());
        
          //add to master_name table
          $add_master = "insert into master_name values ('0', now(), now(),
              '$_POST[f_name]', '$_POST[l_name]')";
          mysql_query($add_master) or die(mysql_error());
        
          //get master_id for use with other tables
          $master_id = mysql_insert_id();
        
          if (($_POST[address]) || ($_POST[city]) || ($_POST[state]) ||
               ($_POST[zipcode])) {
              //something relevant, so add to address table
              $add_address = "insert into address values ('0', $master_id,
                   now(), now(), '$_POST[address]', '$_POST[city]',
                   '$_POST[state]', '$_POST[zipcode]', '$_POST[add_type]')";
              mysql_query($add_address) or die(mysql_error());
          }
        
          if ($_POST[tel_number]) {
              //something relevant, so add to telephone table
              $add_tel = "insert into telephone values ('0', $master_id,
               now(), now(), '$_POST[tel_number]', '$_POST[tel_type]')";
              mysql_query($add_tel) or die(mysql_error());
          }
        
          if ($_POST[fax_number]) {
              //something relevant, so add to fax table
              $add_fax = "insert into fax values ('0', $master_id, now(),
               now(), '$_POST[fax_number]', '$_POST[fax_type]')";
              mysql_query($add_fax) or die(mysql_error());
         }
        
          if ($_POST[email]) {
              //something relevant, so add to email table
              $add_email = "insert into email values ('0', $master_id,
                   now(), now(), '$_POST[email]', '$_POST[email_type]')";
            mysql_query($add_email) or die(mysql_error());
          }
        
          if ($_POST[note]) {
              //something relevant, so add to notes table
            $add_note = "insert into personal_notes values ('0', $master_id,
                  now(), now(), '$_POST[note]')";
             mysql_query($add_note) or die(mysql_error());
         }
        
         $display_block = "<h1>Entry Added</h1>
         <P>Your entry has been added. Would you like to
         <a href=\"addentry.php\">add another</a>?</p>";
         }
         ?>
         <HTML>
         <HEAD>
         <TITLE>Add an Entry</TITLE>
         </HEAD>
         <BODY>
         <? print $display_block; ?>
         </BODY>
         </HTML>
        

          First off, I'd advise you not to bump this rather old (four years!) thread. Wow, it's been a while since I've been on here. You may get more help if you simply create a new thread in the Newbies section.

          That said, you didn't say exactly what error you're getting or on what line you're receiving it. I can only assume it's tripping up at the "if ($POST[op] != "add") {" line, which could be for one of two reasons:

          • You need to access string array indices within quotes, like $_POST['op']

          • You've not defined $_POST['op'] and you're not checking to see if the variable exists first. Try using isset()

            Write a Reply...