Help with insert and viewing records php scripts

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
black85
Forum Newbie
Posts: 16
Joined: Mon Jun 26, 2006 1:36 pm

Help with insert and viewing records php scripts

Post by black85 »

Hi people,
I'm trying to insert record and select/view record respectively, but to no avail. Can anyone help unravel the problems with these scripts below:

addentry.php - insert record script
---------------------------------

Code: Select all

<?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", "root", "olu1bal") or die(mysql_error());
      mysql_select_db("testDB",$conn)  or die(mysql_error());

      //add to master_name table
      $add_master = "insert into master_name values (null, 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 (null, $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 (null, $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 (null, $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 (null, $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 (null, $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>
selentry.php - php script to select/view a record
----------------------------------------------

Code: Select all

<?php
 //connect to database
 $conn = mysql_connect("localhost", "root", "olu1bal") or die(mysql_error());
 mysql_select_db("testDB",$conn)  or die(mysql_error());

 if ($_POST[op] != "delete")  {
     //haven't seen the form, so show it
     $display_block = "<h1>Select an Entry</h1>";

     //get parts of records
     $get_list = "select id, concat_ws(', ', l_name, f_name) as display_name  from master_name order by l_name, f_name";
     $get_list_res = mysql_query($get_list) or die(mysql_error());

     if (mysql_num_rows($get_list_res) < 1) {
         //no records
         $display_block .= "<p><em>Sorry, no records to select!</em></p>";

     } else {
         //has records, so get results and print in a form
         $display_block .= "
         <form method=\"post\" action=\"$_SERVER[PHP_SELF]\">
         <P><strong>Select a Record to Delete:</strong><br>
         <select name=\"sel_id\">
         <option value=\"\">-- Select One --</option>";

         while ($recs = mysql_fetch_array($get_list_res)) {
             $id = $recs['id'];
             $display_name = stripslashes($recs['display_name']);

             $display_block .= "<option value=\"$id\">
                  $display_name</option>";
         }

        $display_block .= "
        </select>
        <input type=\"hidden\" name=\"op\" value=\"view\">
        <p><input type=\"submit\" name=\"submit\"  value=\"View Selected Entry\"></p>
        </FORM>";
     }

} else if ($_POST[op] == "delete") {

    //check for required fields
     if ($_POST[sel_id] == "")  {
        header("Location: selentry.php");
        exit;
    }

    //get master_info
    $get_master = "select concat_ws(' ', f_name, l_name) as display_name from master_name where id = $_POST[sel_id]";
    $get_master_res = mysql_query($get_master);
    $display_name = stripslashes(mysql_result($get_master_res, 0,'display_name'));

   $display_block = "<h1>Showing Record for $display_name</h1>";
   //get all addresses
   $get_addresses = "select address, city, state, zipcode, type from address where master_id = $_POST[sel_id]";
   $get_addresses_res = mysql_query($get_addresses);

   if (mysql_num_rows($get_addresses_res) > 0) {
       $display_block .= "<P><strong>Addresses:</strong><br>
       <ul>";

       while ($add_info = mysql_fetch_array($get_addresses_res)) {
           $address = $add_info[address];
           $city = $add_info[city];
           $state = $add_info[state];
           $zipcode = $add_info[zipcode];
           $address_type = $add_info[type];

           $display_block .= "<li>$address $city $state $zipcode ($address_type)";
       }

      $display_block .= "</ul>";
   }

  //get all tel
  $get_tel = "select tel_number, type from telephone where  master_id = $_POST[sel_id]";
  $get_tel_res = mysql_query($get_tel);

  if (mysql_num_rows($get_tel_res) > 0) {
     $display_block .= "<P><strong>Telephone:</strong><br>
     <ul>";

     while ($tel_info = mysql_fetch_array($get_tel_res)) {
          $tel_number = $tel_info[tel_number];
          $tel_type = $tel_info[type];

          $display_block .= "<li>$tel_number ($tel_type)";
     }

     $display_block .= "</ul>";
  }

  //get all fax
  $get_fax = "select fax_number, type from fax where  master_id = $_POST[sel_id]";
  $get_fax_res = mysql_query($get_fax);

  if (mysql_num_rows($get_fax_res) > 0) {
        $display_block .= "<P><strong>Fax:</strong><br>
        <ul>";
        while ($fax_info = mysql_fetch_array($get_fax_res)) {
            $fax_number = $fax_info[fax_number];
            $fax_type = $fax_info[type];

           $display_block .= "<li>$fax_number ($fax_type)";
        }

       $display_block .= "</ul>";
   }

   //get all email
   $get_email = "select email, type from email where  master_id = $_POST[sel_id]";
   $get_email_res = mysql_query($get_email);

   if (mysql_num_rows($get_email_res) > 0) {
        $display_block .= "<P><strong>Email:</strong><br>
        <ul>";

        while ($email_info = mysql_fetch_array($get_email_res)) {
            $email = $email_info[email];
            $email_type = $email_info[type];

            $display_block .= "<li>$email ($email_type)";
        }

        $display_block .= "</ul>";
   }

   //get personal note
   $get_notes = "select note from personal_notes where  master_id = $_POST[sel_id]";
   $get_notes_res = mysql_query($get_notes);

   if (mysql_num_rows($get_notes_res) == 1) {
        $note = nl2br(stripslashes(mysql_result($get_notes_res,0,'note')));

       $display_block .= "<P><strong>Personal Notes:</strong><br>$note";
   }

   $display_block .= "<br><br><P align=center><a href=\"$_SERVER[PHP_SELF]\">select another</a></p>";
}
?>
<HTML>
<HEAD>
<TITLE>My Records</TITLE>
</HEAD>
<BODY>
<? print $display_block; ?>
</BODY>
</HTML>
mysql database tables:
----------------------

Code: Select all

create table master_name
(
id int not null primary key auto_increment,
date_added datetime,
date_modified datetime,
f_name varchar(75),
l_name varchar(75)
);

Code: Select all

create table address
(
id int not null primary key auto_increment,
master_id int not null,
date_added datetime,
date_modified datetime,
address varchar(255),
city varchar(30),
state char(2),
zipcode varchar(10),
type enum ('home', 'work', 'other')
);


create table telephone
(
id int not null primary key auto_increment,
master_id int not null,
date_added datetime,
date_modified datetime,
tel_number varchar(25),
type enum ('home', 'work', 'other')
);


create table fax
(
id int not null primary key auto_increment,
master_id int not null,
date_added datetime,
date_modified datetime,
fax_number varchar(25),
type enum ('home', 'work', 'other')
);

create table email
(
id int not null primary key auto_increment,
master_id int not null,
date_added datetime,
date_modified datetime,
email varchar(150),
type enum ('home', 'work', 'other')
);


create table personal_notes
(
id int not null primary key auto_increment,
master_id int not null,
date_added datetime,
date_modified datetime,
note text
);
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Sure, what are the problems?
bdlang
Forum Contributor
Posts: 395
Joined: Tue May 16, 2006 8:46 pm
Location: Ventura, CA US

Post by bdlang »

Are we really expected to read through all that and pinpoint an issue? What specific problem are you having? At what point does the script fail or give an error? What does it do / not do?
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

you are incorrectly referencing array indices.. they should read:

Code: Select all

$array['var']
not

Code: Select all

$array[var]
black85
Forum Newbie
Posts: 16
Joined: Mon Jun 26, 2006 1:36 pm

Post by black85 »

@OLE & BDLANG,
Thanks a lot for replying to my posting.

addentry.php is supposed to allow me to enter information on a html form, but unfortunately no form is displayed on the browser. Also, the message 'entry added' should be displayed on the browser as well after values have been inserted into form fields.

selentry.php is supposed to allow me to select a record from a drop-down menu and view such record (with the message 'showing record for jane doe or whatever') on the browser thereafter, but unfortunately, no drop down menu is displayed and cannot view a record on the browser.
Last edited by black85 on Wed Jul 12, 2006 10:41 am, edited 1 time in total.
black85
Forum Newbie
Posts: 16
Joined: Mon Jun 26, 2006 1:36 pm

Post by black85 »

@Jenk

Thanks a lot for your response.
bdlang
Forum Contributor
Posts: 395
Joined: Tue May 16, 2006 8:46 pm
Location: Ventura, CA US

Post by bdlang »

black85 wrote:@OLE & BDLANG,
Thanks a lot for replying to my posting.

addentry.php is supposed to allow me to enter information on a html form, but unfortunately no form is displayed on the browser. Also, the message 'entry added' should be displayed on the browser as well after values have been inserted into form fields.

selentry.php is supposed to allow me to select a record from a drop-down menu and view such record (with the message 'showing record for jane doe or whatever') on the browser thereafter, but unfortunately, no drop down menu is displayed and cannot view a record on the browser.
I feel like we just went through this on the other post (which I notice you never commented back on). Both instances on these scripts utilize short tags to escape HTML and perform a PHP function, displaying the variable values (which just happen to not display). Do you, in fact, have short_open_tag enabled? The main body of your scripts use the standard <?php tags, try changing these one-liners to use the <?php tags as well.

The alternative would be to enable short tags, but due to the conflict with XML I don't recommend it.

Otherwise, what HTML or output do the scripts display? Do you actually get the HTML output you wrote, simply missing the values that should be displayed on those lines?

[ OT ]
As far as your post is concerned, although it is alot to weed through, I do applaud you for using the correct PHP tags and showing all of the relevant code, along with the table makeup. Normally users will show pseudocode or some made-up example that has nothing to do with their actual script, or leave out so many details that it's like you're playing 20 questions with them. So thanks for that.
[/ OT ]
black85
Forum Newbie
Posts: 16
Joined: Mon Jun 26, 2006 1:36 pm

Post by black85 »

@bdlang

The html form is not being shown in the browser, let alone show the messages on the browser. I currently use php version 5 and mysql 5 as well. I'm just stuck. I just cannot believe its still not working considering the fact that i copied all the php scripts and mysql database tables from a book by a well known php/mysql author. Thanks for your assistance, man
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

take the assignment of $display_block out of its conditional block.
That is remove the first if:

Code: Select all

if ($_POST[op] != "add") {
Change this:

Code: Select all

} else if ($_POST[op] == "add") {
to

Code: Select all

if ($_POST['op'] == 'add') {
and add an else to it so you know when its failing

Code: Select all

} else echo 'op isn\'t equal to add  =: (';
black85
Forum Newbie
Posts: 16
Joined: Mon Jun 26, 2006 1:36 pm

Post by black85 »

Thanks OLE for replying to my posting. I have made changes like you suggested, but still doesn't work.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

do you get "op isn't equal to add =: (" ?
black85
Forum Newbie
Posts: 16
Joined: Mon Jun 26, 2006 1:36 pm

Post by black85 »

Ole wrote: do you get "op isn't equal to add =: (" ?

What do you mean? I have made changes like you suggested and still doesn't work.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

If you made the changes that i told you to then one of them was this:

Code: Select all

} else echo 'op isn\'t equal to add  =: (';
which will echo

Code: Select all

op isn't equal to add  =: (
no?

It that in the output or not?
black85
Forum Newbie
Posts: 16
Joined: Mon Jun 26, 2006 1:36 pm

Post by black85 »

@Ole
I think the php scripts are not doing what they are supposed to be doing cos they are not compatible with php 5 and mysql 5. I have downloaded php scripts that are compatible with php and mysql 5 on the advice of the author of the book i copied those scripts. Thanks a lot for your assistance.
Post Reply