SitePoint Sponsor

User Tag List

Results 1 to 18 of 18
  1. #1
    SitePoint Member
    Join Date
    Jun 2006
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help with insert and viewing records php scripts

    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
    -------------------------
    PHP 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""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>

  2. #2
    play of mind Ernie1's Avatar
    Join Date
    Sep 2005
    Posts
    1,252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    using superglobals, instead $_POST[op] you have to use single quotes, like this $_POST['op']
    my mobile portal
    ghiris.ro

  3. #3
    SitePoint Member
    Join Date
    Jun 2006
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @ernie1

    Thanks for replying to my posting. I have made changes like you suggested, but still doesn't work.

  4. #4
    play of mind Ernie1's Avatar
    Join Date
    Sep 2005
    Posts
    1,252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    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(), '".mysql_real_escape_string($_POST['f_name'])."', '".mysql_real_escape_string($_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(), '".mysql_real_escape_string($_POST['address'])."', '".mysql_real_escape_string($_POST['city'])."', '".mysql_real_escape_string($_POST['state'])."', '".mysql_real_escape_string($_POST['zipcode'])."', '".mysql_real_escape_string($_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(), '".mysql_real_escape_string($_POST['tel_number'])."',  '".mysql_real_escape_string($_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(), '".mysql_real_escape_string($_POST['fax_number'])."',  '".mysql_real_escape_string($_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(), '".mysql_real_escape_string($_POST['email'])."',  '".mysql_real_escape_string($_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(), '".mysql_real_escape_string($_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>
    Last edited by Ernie1; Jul 12, 2006 at 12:49.
    my mobile portal
    ghiris.ro

  5. #5
    SitePoint Member
    Join Date
    Jun 2006
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @Ernie1,

    I really appreciate your assistance, but i am a php/mysql newbie learning via a book, can you simplify what you just wrote for me. I dont get it.

    black85

  6. #6
    play of mind Ernie1's Avatar
    Join Date
    Sep 2005
    Posts
    1,252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    did you test the last code I've posted?

    it's just a measure to protect you when running mysql_query which prepends backslashes to the following characters \x00, \n, \r, \, ', " and \x1a.
    my mobile portal
    ghiris.ro

  7. #7
    SitePoint Member
    Join Date
    Jun 2006
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes ernie. still not working

  8. #8
    play of mind Ernie1's Avatar
    Join Date
    Sep 2005
    Posts
    1,252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    please try again, I've changed my last post.
    my mobile portal
    ghiris.ro

  9. #9
    SitePoint Member
    Join Date
    Jun 2006
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is part of the error messages displayed in my browser below:





    f (@$_POST['op'] !="add") { //haven't seen the form, so show it $display_block = "
    Add an Entry


    "; } 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(), '".mysql_real_escape_string($_POST['f_name'])."', '".mysql_real_escape_string($_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(), '".mysql_real_escape_string($_POST['address'])."', '".mysql_real_escape_string($_POST['city'])."', '".mysql_real_escape_string($_POST['state'])."', '".mysql_real_escape_string($_POST['zipcode'])."', '".mysql_real_escape_string($_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(), '".mysql_real_escape_string($_POST['tel_number'])."', '".mysql_real_escape_string($_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(), '".mysql_real_escape_string($_POST['fax_number'])."', '".mysql_real_escape_string($_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(), '".mysql_real_escape_string($_POST['email'])."', '".mysql_real_escape_string($_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(), '".mysql_real_escape_string($_POST['note'])."')"; mysql_query($add_note) or die(mysql_error()); } $display_block = "
    Entry Added

    Your entry has been added. Would you like to add another?
    "; } ?>

  10. #10
    play of mind Ernie1's Avatar
    Join Date
    Sep 2005
    Posts
    1,252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP 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""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(), '".mysql_real_escape_string($_POST['f_name'])."', '".mysql_real_escape_string($_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(), '".mysql_real_escape_string($_POST['address'])."', '".mysql_real_escape_string($_POST['city'])."', '".mysql_real_escape_string($_POST['state'])."', '".mysql_real_escape_string($_POST['zipcode'])."', '".mysql_real_escape_string($_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(), '".mysql_real_escape_string($_POST['tel_number'])."',  '".mysql_real_escape_string($_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(), '".mysql_real_escape_string($_POST['fax_number'])."',  '".mysql_real_escape_string($_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(), '".mysql_real_escape_string($_POST['email'])."',  '".mysql_real_escape_string($_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(), '".mysql_real_escape_string($_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>
    ok here you are, this is good.
    Last edited by Ernie1; Jul 12, 2006 at 16:19.
    my mobile portal
    ghiris.ro

  11. #11
    SitePoint Member
    Join Date
    Jun 2006
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nothing is displayed in the browser.

  12. #12
    SitePoint Member
    Join Date
    Jun 2006
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You are right, i just tested and it works perfectly. I need to recopy the script.

  13. #13
    play of mind Ernie1's Avatar
    Join Date
    Sep 2005
    Posts
    1,252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok I can see your entries on my tables.

    Yeah, just take the last code and everything it's ok
    my mobile portal
    ghiris.ro

  14. #14
    SitePoint Member
    Join Date
    Jun 2006
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    what last code?

  15. #15
    play of mind Ernie1's Avatar
    Join Date
    Sep 2005
    Posts
    1,252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    my mobile portal
    ghiris.ro

  16. #16
    SitePoint Member
    Join Date
    Jun 2006
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have copied it and still doesn't work. Do you think the problem i'm having is to with php.ini configuration file?

  17. #17
    play of mind Ernie1's Avatar
    Join Date
    Sep 2005
    Posts
    1,252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well, I really don't know. You may test on some free hosting server with php - mysql suport.
    my mobile portal
    ghiris.ro

  18. #18
    SitePoint Member
    Join Date
    Jun 2006
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @ Ernie1

    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.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •