SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Jan 2001
    Location
    Toronto
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The following problem:

    I started last night with PHP Scripting, thanks to Mr. Yank I was able to get a pretty quick grasp on the logic associations of interaction between PHP and MySQL.
    Now I created a very simple example of a DB driven page (some of you will crack up)...you can see it http://www.cte-networks.com/test.php . So I was able to put in the admin section the ability to input text onto the "test.php"...no problem with that. But how can I modify the Admin section so when I enter the section, I see in the Text Boxes the already existing Text and I can delete, edit or add text to the database...sorry I know it is probably the easiest thing to do, but don't forget I am only 1 day old in PHP and MySQL. Here is the Code I am currently using in the Admin Section:

    <html>
    <head>
    <title>Admin Section</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>

    <body bgcolor="#FFFFFF" text="#000000">

    <FORM ACTION="<?php echo($PHP_SELF); ?>" METHOD=POST>
    <P><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Type your Text
    for the left column hereBR>
    <TEXTAREA NAME="textleft" ROWS=10 COLS=40 WRAP>
    </TEXTAREA>
    </font><BR>
    <INPUT TYPE=SUBMIT NAME="submittext1" VALUE="SUBMIT">
    </FORM>

    <?php

    // Connect to the database server
    $dbcnx = @mysql_connect("localhost",
    "root", "password");
    if (!$dbcnx) {
    echo( "<P>Unable to connect to the " .
    "database server at this time.</P>" );
    exit();
    }

    // Select the test database
    if (! @mysql_select_db("dbname") ) {
    echo( "<P>Unable to locate the joke " .
    "database at this time.</P>" );
    exit();
    }

    // If a text has been submitted,
    // add it to the database.
    if ("SUBMIT" == $submittext1) {
    $sql = "INSERT INTO test1 SET " .
    "TextLeft='$textleft' ";
    if (mysql_query($sql)) {
    echo("<P>Your Text has been added.</P>");
    } else {
    echo("<P>Error adding submitted text: " .
    mysql_error() . "</P>");
    }
    }

    ?>

    <FORM ACTION="<?php echo($PHP_SELF); ?>" METHOD=POST>
    <P><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Type your Text
    for the right column hereBR>
    <TEXTAREA NAME="textright" ROWS=10 COLS=40 WRAP>
    </TEXTAREA>
    </font><BR>
    <INPUT TYPE=SUBMIT NAME="submittext2" VALUE="SUBMIT">
    </FORM>

    <?php

    // Connect to the database server
    $dbcnx = @mysql_connect("localhost",
    "root", "password");
    if (!$dbcnx) {
    echo( "<P>Unable to connect to the " .
    "database server at this time.</P>" );
    exit();
    }

    // Select the test database
    if (! @mysql_select_db("dbname") ) {
    echo( "<P>Unable to locate the joke " .
    "database at this time.</P>" );
    exit();
    }

    // If a text has been submitted,
    // add it to the database.
    if ("SUBMIT" == $submittext2) {
    $sql = "INSERT INTO test2 SET " .
    "TextRight='$textright' ";
    if (mysql_query($sql)) {
    echo("<P>Your Text has been added.</P>");
    } else {
    echo("<P>Error adding submitted text: " .
    mysql_error() . "</P>");
    }
    }

    ?>


    <p><font size="2" face="Verdana, Arial, Helvetica, sans-serif"><a href="test.php">Go
    back to Test Site</a></font></p>
    </body>
    </html>

  2. #2
    Serial Publisher silver trophy aspen's Avatar
    Join Date
    Aug 1999
    Location
    East Lansing, MI USA
    Posts
    12,937
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Easy.

    Just query your DB to get the current info then print it out in the form.

    <input type = "text" name = "field1" value = "<? print("$field1"); ?>">

    and so on and so forth.

    Chris Beasley - I publish content and ecommerce sites.
    Featured Article: Free Comprehensive SEO Guide
    My Guide to Building a Successful Website
    My Blog|My Webmaster Forums

  3. #3
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Location
    Melbourne, Australia
    Posts
    2,571
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hi, Peter!

    Have you heard the saying, "A little knowledge is a dangerous thing"? I think it applies in this situation.

    May I suggest that you continue reading my article. Chapter 6 in particular treats effective design of content management systems, and provides copious examples (including downloadable sample code) of forms for adding, editing, and deleting database entries. You really need to look at more examples to better understand how to design and structure database-driven pages using PHP. In your code above, for example, you are connecting to the same MySQL server and selecting the same database twice for no reason.

    If after reading you find you're still stuck, please feel free to ask for help!

    Incidentally, the URL you provided gives me an internal server error...
    Kevin Yank
    CTO, sitepoint.com
    I wrote: Simply JavaScript | BYO PHP/MySQL | Tech Times | Editize
    Baby’s got back—a hard back, that is: The Ultimate CSS Reference


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
  •