+ Reply to Thread
Page 1 of 4 123 ... LastLast
Results 1 to 10 of 39

Thread: [REQ][250 credits]switching content with mySQL

  1. #1
    galaxyAbstractor's Avatar
    galaxyAbstractor is offline Community Advocate galaxyAbstractor is on a distinguished road
    Join Date
    Oct 2007
    Location
    Land of Null and Insanity
    Posts
    5,495

    [REQ][250 credits]switching content with mySQL

    I want to do this:

    I have one file, gadgets.php. When you go to gadgets.php?id=1, it shows a bit of HTML. gadget.php?id=2 shows another bit of HTML.

    It will take the HTML from a table in the database. The table has 2 rows, gcode, that holds the code and id that holds the id.

    Now gadgets.php?id=1 will show the code with the ID 1 in the database.

    An example on how to do it with files:
    http://forums.x10hosting.com/tutoria...ing-pages.html

    But I want it done by SQL.

    Like think of how a forum works. The style is stored in files but the post is stored in the database. I want it that way

  2. #2
    xPlozion's Avatar
    xPlozion is offline x10 Elder xPlozion is an unknown quantity at this point
    Join Date
    Mar 2008
    Location
    Delaware, USA
    Posts
    872

    Re: [REQ][250 credits]switching content with mySQL

    OK, not too hard. The biggest problem I may have when coding it is properly inserting/reading the html in the database without screwing anything up. Also, make sure that the row, `id` is set to auto_increment

    PHP Code:
    <?php

    // Display gcode if ?id=# exists and only contains numbers...
    if (!empty($_GET['id']) && is_numeric($_GET['id'])) {
        
    $id preg_replace("[^0-9]",""$_GET['id']);
        
    $result mysql_query("SELECT gcode FROM table WHERE id='$id' LIMIT 1");
        
    $result mysql_fetch_assoc($result);
        
    $old = array("&quot;""&apos;");
        
    $new = array('"'"'");
        
    $gcode str_replace($old$new$result['gcode']);
        echo 
    $gcode;
    } else {
        die(
    "Error: Page number invalid.  Please only use numbers (no letters, etc...");
    }
    ?>
    PHP Code:
    <?php

    // Insert HTML into database :D  This is just the basics to make it work.  You can add more to this if you have the desire to
    if (!empty($_POST['gcode']) {  // Just make sure that your field name is gcode ;)
        
    $old = array('"'"'");
        
    $new = array("&quot;""&apos;");
        
    $gcode str_replace($old$new$_POST['gcode']);
        
    $result mysql_query("INSERT INTO table (gcode) VALUES('$gcode') LIMIT 1");
        if (
    $result) {
            echo 
    "Created new page";
        } else {
            echo 
    "There was an error creating your page.<br /><br />".mysql_error();
        }
    } else {
        echo 
    "
            <form action='?submit' method='post'><div>
            <textarea name='gcode'></textarea><br /><br />
            <input type='submit' value='Create Page' name='submit' />
            </div></form>
        "
    ;
    }
    ?>
    PHP Code:
    <?php

    // Update HTML already in the database :)
    if (isset($_POST['update']) && !empty($_GET['id'])) {
        
    $id preg_replace("[^0-9]",""$_GET['id']);
        
    $old = array('"'"'");
        
    $new = array("&quot;""&apos;");
        
    $gcode str_replace($old$new$_POST['gcode']);
        
    $result mysql_query("UPDATE table SET gcode='$gcode' WHERE id='$id' LIMIT 1");
        if (
    $result) {
            echo 
    "Page: $id updated successfully";
        } else {
            echo 
    "There was a problem updating page: $id<br /><br />".mysql_error();
        }
            
    } elseif (!empty(
    $_GET['id'])) {
        
    $id preg_replace("[^0-9]",""$_GET['id']);
        
    $result mysql_query("SELECT gcode FROM table WHERE id='$id' LIMIT 1");
        
    $old = array("&quot;""&apos;");
        
    $new = array('"'"'");
        
    $gcode str_replace($old$new$result['gcode']);
        
    $result mysql_fetch_assoc($result);
        echo 
    "
            Page <b>
    $id</b><br /><br />
            <form action='?id=
    $id' method='post'><div>
            <textarea name='gcode'>
    $gcode</textarea><br /><br />
            <input type='submit' value='Update' name='update />
            </div></form>
        "
    ;

    } else {
        
    $result mysql_query("SELECT id, gcode FROM table");
        while (
    $result=mysql_fetch_assoc($result)) {
            echo 
    "<a href='?id=".$result['id']."'>Page ".$result['id']."</a><br />\n";
        }
    }
    ?>
    If there's any errors, please let me know. To the best of my knowledge, there are no errors in this script, but I have not confirmed this.

    -xP
    Last edited by xPlozion; 08-30-2008 at 09:51 AM.

  3. #3
    galaxyAbstractor's Avatar
    galaxyAbstractor is offline Community Advocate galaxyAbstractor is on a distinguished road
    Join Date
    Oct 2007
    Location
    Land of Null and Insanity
    Posts
    5,495

    Re: [REQ][250 credits]switching content with mySQL

    ummm. I can't put HTML in the submit form...

    "There was an error creating your page.

    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LIMIT 1' at line 1"

  4. #4
    xPlozion's Avatar
    xPlozion is offline x10 Elder xPlozion is an unknown quantity at this point
    Join Date
    Mar 2008
    Location
    Delaware, USA
    Posts
    872

    Re: [REQ][250 credits]switching content with mySQL

    I'm sorry, on that line, there shouldn't be a LIMIT 1. Remove that and it should be good. The only other possible thing that woud screw it up were quotes and apostrophes, which were both properly converted to avoid problems... ;)

    -xP

  5. #5
    galaxyAbstractor's Avatar
    galaxyAbstractor is offline Community Advocate galaxyAbstractor is on a distinguished road
    Join Date
    Oct 2007
    Location
    Land of Null and Insanity
    Posts
    5,495

    Re: [REQ][250 credits]switching content with mySQL

    Now it says:

    There was an error creating your page.

    Column count doesn't match value count at row 1

  6. #6
    xPlozion's Avatar
    xPlozion is offline x10 Elder xPlozion is an unknown quantity at this point
    Join Date
    Mar 2008
    Location
    Delaware, USA
    Posts
    872

    Re: [REQ][250 credits]switching content with mySQL

    try this
    PHP Code:
        $result mysql_query("INSERT INTO table (id, gcode) VALUES(NULL, '$gcode')"); 
    and make sure that id in phpMyAdmin is an integer (int) and that it's set to Primary and auto_increment...

    I'm sorry that there's problems. From now on, I will always test out my code before I post it up in the Marketplace ;)
    Last edited by xPlozion; 08-31-2008 at 11:18 AM. Reason: My apologies

  7. #7
    galaxyAbstractor's Avatar
    galaxyAbstractor is offline Community Advocate galaxyAbstractor is on a distinguished road
    Join Date
    Oct 2007
    Location
    Land of Null and Insanity
    Posts
    5,495

    Re: [REQ][250 credits]switching content with mySQL

    Well, it worked this time. But I assume it gets id 1 and I need to do gadget.php?id=1. When I do that I get a blank page

  8. #8
    xPlozion's Avatar
    xPlozion is offline x10 Elder xPlozion is an unknown quantity at this point
    Join Date
    Mar 2008
    Location
    Delaware, USA
    Posts
    872

    Re: [REQ][250 credits]switching content with mySQL

    check phpMyAdmin and see if it went into the database properly...

  9. #9
    galaxyAbstractor's Avatar
    galaxyAbstractor is offline Community Advocate galaxyAbstractor is on a distinguished road
    Join Date
    Oct 2007
    Location
    Land of Null and Insanity
    Posts
    5,495

    Re: [REQ][250 credits]switching content with mySQL

    yes it did add it. Got it! in http:// // comments out the rest of the code! because the source of the page is:

    <html>
    <head>
    </head>
    <body>

    <script src="http
    </body>
    </html>
    or, it comments it out upon adding it :/

  10. #10
    xPlozion's Avatar
    xPlozion is offline x10 Elder xPlozion is an unknown quantity at this point
    Join Date
    Mar 2008
    Location
    Delaware, USA
    Posts
    872

    Re: [REQ][250 credits]switching content with mySQL

    ok,

    where it says
    Code:
        echo $gcode;
    , change that to
    Code:
        echo str_replace('/', '\/', $gcode);
    that should escape the slash, preventing it from thinking it's a comment.
    Last edited by xPlozion; 09-01-2008 at 04:14 PM.

+ Reply to Thread
Page 1 of 4 123 ... LastLast

Similar Threads

  1. [PHP] MySQL and PHP
    By Bryon in forum Tutorials
    Replies: 43
    Last Post: 03-24-2011, 07:27 AM
  2. New Site-Suggestions?
    By mnoutside in forum Review My Site
    Replies: 9
    Last Post: 08-27-2008, 07:01 AM
  3. Urgent help please!
    By retro-bliss in forum Free Hosting
    Replies: 2
    Last Post: 10-25-2007, 03:24 PM
  4. Have a problem with my forum
    By tikloos in forum Scripts & 3rd Party Apps
    Replies: 43
    Last Post: 01-19-2006, 01:14 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers