"fopen ()" does not update an existing file

0

I am working on a code that publishes articles on a page. This code works perfectly when creating the article, however, when trying to update it (publishing the same article with the same name but with different content from my admin panel), it is not updated. I search in the database and the info is the same (the changes that should be made in the UPDATE of the code have no effect) and when opening the article from the browser, it appears the same as before. Can you help me find the error, please?

PHP Code

//Crear archivo html del artículo

    $nombreDelArchivo = url_amigable($titulo); //mando el título del artículo  a una función que me hace el nombre en "formato" url amigable (para que no haya problemas al abrir el artículo desde el navegador

    require_once("../includes/crearHTMLdeArticulo1.php");
    require_once("../includes/crearHTMLdeArticulo2.php");
    require_once("../includes/crearHTMLdeArticulo3.php");

    $contenidoArt = $contenido1 . "<title>" . $titulo . " - Level Up</title>" . $contenido2 . '
                <article id="contenedorArticuloCompleto">
                    <h1 id="tituloArtComp">' . $titulo . '</h1>
                    <div style="background-image:' . $foto . ';" id="imgArtComp"></div>
                    <div id="contenidoArtComp">' . $contenido . '</div>
                </article>
                ' . $contenido3;

//Hasta acá es una mezcla entre html y php para armar el contenido de la página

    $ruta = "../articles/" . $nombreDelArchivo . ".php";

    if (file_exists($ruta)){ //Acá debería entrar si la  ruta recién formada (conformada por ubicación del archivo y el nombre del mismo) es igual a alguna existente

        $query1 = "UPDATE articles SET titulo = '$titulo', img = '$foto', descripcion = '$descripcion', contenido = '$contenido' WHERE titulo = '$titulo'";

        if($resp1 = mysql_query($conexion, $query1)){

            $archivo = fopen($ruta, "w");
            fputs ($archivo, $contenidoArt);
            fclose ($archivo);

            $ok = true; //Pongo el valor "true" a la variable "ok" creada al inicio del código que tenía el valor predeterminado de "false"

            echo $ok;

        }else{
            echo $ok;
        }

    }else{

        $query2 = "INSERT INTO articles (titulo, img, descripcion, contenido) VALUES ('$titulo', '$foto', '$descripcion', '$contenido')";

        if($resp2 = mysqli_query($conexion, $query2)){

            $archivo = fopen($ruta, "w");

            fputs ($archivo, $contenidoArt);

            fclose ($archivo);

            $ok = true;

            echo $ok;
        }else{
            echo $ok;
        }
    
asked by Criss 30.04.2016 в 23:03
source

2 answers

1

The Title

The problem in the code revolves around the title of the article. When you load the object from the database, it saves the same variable $titulo . You use this variable to calculate the path of the file. Example:

When you upload the article
The value of $title is "Título original" and therefore the path to the file you are modifying is: "../articles/titulo-original.php"
When you modify the article
The value of $title becomes "Título nuevo" so the path to the file changes to "../articles/titulo-nuevo.php" .
Thus, the file_exists("../articles/titulo-nuevo.php") method evaluates to false .

SQL statement

The failure in the SQL statement that you are executing is caused by the title:

$query1 = "UPDATE articles SET titulo = '$titulo', img = '$foto', descripcion = '$descripcion', contenido = '$contenido' WHERE titulo = '$titulo'";

If you modify the title of the article
At this point you are passing the value of $titulo that has already been modified, so when you do the update the filter WHERE titulo = '$titulo' does not find the original record.
This could be resolved by storing the original title of the article in another variable, for example $titulo_original . In this way the SQL statement would be as follows:

$query1 = "UPDATE articles SET titulo = '$titulo', img = '$foto', descripcion = '$descripcion', contenido = '$contenido' WHERE titulo = '$titulo_original'";

On the other hand, mysql_query() always will return true if no error has occurred, even if you do not modify any record in the database. To know if, indeed, any record has been affected, use the mysql_affected_rows() method. mysql_affected_rows (php.net)

  

Documentation php.net about mysql_affected_rows()

     

Returns the number of affected rows in case of success, and -1 if the last query failed.

This would be this portion of code:

mysql_query($conexion, $query1);
if (mysql_affected_rows() !== 0) {
    // operaciones sobre el archivo
}

About the files

By the piece of code provided I deduce that you make simple writings about the files. For this you can use the file_put_contents() method that performs fopen() and fclose() as well as overwrite the file automatically. file_put_contents (php.net)

Your code would be reduced to:

$resultado = file_put_contents($ruta, $contenidoArt, LOCK_EX);
if ( $resultado !== false) {
    // operaciones adicionales
}

In the third parameter I specify the flag LOCK_EX that indicates to the method that it should obtain the exclusive right to access the file.

Recommendations

When you modify an article I recommend saving some parameters of it just when you get it, this way you have more game when saving. For example, delete the file with the previous title.

I also recommend using an invariant identifier ID for articles, so you do not have to keep track of the $titulo_original that I mentioned before and the SQL statement would be such that:

$query1 = "UPDATE articles SET titulo = '$titulo', img = '$foto', descripcion = '$descripcion', contenido = '$contenido' WHERE id = '$id'";

This will also make the tasks of loading and deleting the articles more comfortable since you will not have to work with chains. But this only if these tasks become more complex.

And as you mentioned @ Hackerman, I recommend changing to mysqli or PDO . ;-)

    
answered by 05.10.2016 в 16:01
0

I think you have this part wrong

$ query1="UPDATE articles SET title = '$ title', img = '$ photo', description = '$ description', content = '$ content' WHERE title = '$ title'";

try this

$ query1="UPDATE articles SET title = '". $ title. "', img = '". $ photo. "', description = '". $ description. "', content = '". $ content . "'WHERE title ='". $ Title. "'";

    
answered by 06.05.2016 в 00:03