Page 1 of 1

Already printed text are removed when its DB row is removed

Posted: Thu Sep 04, 2008 1:18 pm
by osciva
I have a text in a mySQL database. When I fetch the text and print it on my website I want to directly afterwards remove its row from the database. However, when I do this, not only the row are removed from the database, but also the actual text that I just now printed on my website.

I want to be able to fetch the text, print it on a website, and then remove its row from the database but leave the text printed on the site.

Here is the code I'm using. Anybody that can help me with what I need to change to make it work?

Code: Select all

 
<?php
    function writePost() {
        $open = mysql_connect('localhost', 'db_user', 'db_pass');
        mysql_select_db('db_name');
 
        $query = "INSERT INTO tabell (entext) VALUES ('" . $_POST["texten"] . "')";
        if (mysql_query($query)) {
            echo "ok";
            mysql_close($open);
        } else {
            echo("Något blev fel<BR>");
            echo ($query);
            mysql_close($open);
        }
    }
 
    function readPost() { // id are sent in $_GET['i']
        $open = mysql_connect('localhost', 'db_user', 'db_pass');
        mysql_select_db('db_name');
 
        $query = "SELECT entext FROM tabell WHERE id = '" . $_GET['i'] . "'";
        $data = mysql_query($query);
 
        while ($rad = mysql_fetch_array($data)) {
            echo $rad["entext"];
        }
        mysql_close($open);
    }
 
    function deletePost() { // id are sent in $_GET['i']
        $open = mysql_connect('localhost', 'db_user', 'db_pass');
        mysql_select_db('db_name');
 
        $query ="DELETE FROM tabell WHERE id = '" . $_GET['i'] . "'";
        mysql_query($query);
        mysql_close($open);
    }
 
    function writeHeader() {
        echo "
            <HTML><HEAD></HEAD><BODY>
            <CENTER>
        ";
    }
 
    function writeFooter() {
        echo "
            </CENTER></BODY></HTML>
        ";
    }
 
    if ($_GET['m'] == "write"){
        writeHeader();
        writePost();
        writeFooter();
    } elseif (strlen($_GET['i']) >= 1){
        writeHeader();
        readPost();
        deletePost();
        writeFooter();
    } else {
        writeHeader();
        echo "
            <TABLE WIDTH=\"520\" ALIGN=\"top\" BORDER=\"0\" CELLPADDING=\"1\" CELLSPACING=\"0\" BGCOLOR=\"#000000\"><TR><TD>
            <TABLE WIDTH=\"100%\" ALIGN=\"top\" BORDER=\"0\" CELLPADDING=\"3\" CELLSPACING=\"0\" BGCOLOR=\"#FFFFFF\">
                <TR VALIGN=\"top\">
                    <TD WIDTH=\"210\">
                        <FORM METHOD=\"post\" ACTION=\"?m=write\">
                        <TEXTAREA NAME=\"texten\" ROWS=\"5\" COLS=\"30\">the text that will be printed</TEXTAREA><BR>
                        <INPUT TYPE=\"submit\" NAME=\"add\" VALUE=\"OK!\">
                        </FORM>
                    </TD>
                </TR>
            </TABLE></TD></TR></TABLE>
        ";
        writeFooter();
    }
?>
 
Thanks!

Re: Already printed text are removed when its DB row is removed

Posted: Thu Sep 04, 2008 1:35 pm
by greyhoundcode
The problem with what you are saying is that if you delete the information from the database then it ceases to exist.

If there is some compelling reason for doing this then the alternative is to have an HTML include file for the page (or for a specific part of the page). You could open it, append the line of text and close it again. Then, each time the page is loaded you could simply Include that file:

Code: Select all

// As part of your function that initially takes the text entry from the database:
$HTMLInclude = file_get_contents("filepath/section_of_page.html.inc");
$HTMLInclude = $HTMLInclude . $entext;
file_put_contents("filepath/section_of_page.html.inc", $HTMLInclude); 
 
// And to place on the .php file that generates the page:
include "filepath/section_of_page.html.inc";
I'm sure you get the idea, though it isn't particularly elegant. Do you have particularly stringent size constraints on your database?

Re: Already printed text are removed when its DB row is removed

Posted: Mon Sep 08, 2008 5:05 pm
by osciva
Thank you for the answer, however - and sorry if this is a really stupid comment - as I see it, the text I have written on the page should not have any "connection" to the original text and its row in the database anymore. If I have fetched the text from the DB, put it in a php variable, printed the php variable, and then remove the complete DB, the text should still exist on the page.

As a test I have changed the code so the DB row isnt removed. Now I have a new bool column in the table that is put "true" when the text first is fetched. Then I have a regular true statement that first checkes the bool value, and if the value is true the following text is printed "The text is gone" (even though the text still exists in the DB). The following else statement prints the text as it is written in the DB. Even after this change the text is first printed on the page, only to magically disappear after less than 0,5 seconds and are replaced by "The text is gone". It seems like the if statement is run several times; first the text is read, then the bool value is set true, and the the text tries to be read again for some reason?

If we go back to the original code:

Code: Select all

...
} elseif (strlen($_GET['i']) >= 1){
   writeHeader();
   readPost();
   deletePost();
   writeFooter();
} else {
...
can it be so, that this elseif-statement are read more than once for some reason; first readPost() is read, then deletePost(), and then readPost() again before the page stops to load?

Big thanks for all the help!

Re: Already printed text are removed when its DB row is removed

Posted: Tue Sep 09, 2008 5:00 am
by greyhoundcode
Have to admit I'm getting a bit confused now, but, you seem to be saying that once you have printed the text to the page then the text should still exist.

If I have a file called text.php and it contains a statement like echo "Hello!"; then every time I load text.php in my browser I will see Hello! on the screen. That isn't because Hello! is printed onto the file as such, it's because every time the page is loaded the PHP instructions get parsed and executed.

Does that make sense?

Re: Already printed text are removed when its DB row is removed

Posted: Tue Sep 09, 2008 5:12 am
by osciva
Absolutely, that makes totally sense.

However, in your example I can load text.php which will print "Hello!" in my browser. Now, still having my browser window open and _not_ hitting reload or anything, I can delete the text.php from my file server but still have "Hello!" printed in my browser. It won't be until I try to load text.php again that "Hello!" will be removed (and in this case replaced by a 404).

Back to my example; I can totally understand that my text will disappear when I have removed it from the database and I press reload in my browser.

Re: Already printed text are removed when its DB row is removed

Posted: Tue Sep 09, 2008 3:18 pm
by greyhoundcode
OK, sorry if I misunderstood, but I think I get what you are saying now. You are retrieving data from the database, displaying it, then deleting it from the database - all in one script - except that the text is not printed to the screen.

I tried simplifying the problem a bit:

Code: Select all

<?php
 
function AddToDatabase()
{
    // Establish a connection
    $database = mysql_connect('localhost', 'root', '');
    mysql_select_db('test');
 
    // Write to the database
    if (!mysql_query("INSERT INTO `testdata` (`FieldA`) VALUES ('123')"))
    {
        echo 'Error! Could not write to the database';
        exit();
    }
}
 
function RetrieveFromDatabase()
{
    // Query the database
    $result = mysql_query("SELECT `FieldA` FROM `testdata` WHERE 1");
    $result = mysql_fetch_row($result);
 
    // Print the query result to the page
    echo $result[0];
}
 
function DeleteFromDatabase()
{
    // Now delete the entry from the database
    mysql_query("DELETE FROM `testdata` WHERE `FieldA`='123'");
}
 
if (isset($_POST['textItem']))
{
    $textItem = $_POST['textItem'];
    
    AddToDatabase($textItem);
    RetrieveFromDatabase($textItem);
    DeleteFromDatabase($textItem);
}
 
?>
 
<form action="test.php" method="post">
 
    <input type="text" name="textItem">
    <input type="submit" name="submit">
 
</form>
OK so that isn't going to replicate what you want with your code but in principle I think it does what you ask - it puts data from a form onto the database, retrieves it, displays it and then deletes it.

The problem I had trying to test the code in your original post was partly that I couldn't see where $_GET['i'] was being generated. Anyway ... don't suppose this is helping you too much! ... :|

Re: Already printed text are removed when its DB row is removed

Posted: Fri Sep 12, 2008 12:27 pm
by osciva
Thank's for your help greyhoundcode! Actually it seems to have solved itself. When I tried my code with only the code (no design for the site) the described problem came. However now when I have added all the design related code, css and such, the problem is gone... quite strange.

Thanks anyway!

UPDATE: The problem isn't solved. For those of you who might have found this after searching for a similar problem. I don't think it has anything to do with databases and have continued the topic here: viewtopic.php?f=1&t=88149&start=0