简体   繁体   中英

Update mysql data using php and form

I´ve been having a weird problem trying to create a php page that uses html forms to update mysql data.

The idea is to create a page that retrieves all the rows from a "news" table that I have, and inserts all the data into html forms as "default" values, so I can see what is already written before changing whatever I want in this form. Each form is generated exclusively for each row of data retrieved.

For that I use the POST method and two php files, one called "updateNews.php" which retrieves data and renders forms, and another one called "newsUpdater.php" which injects the updated data.

I have two problems here. One, the form doesn´t post the new data written in the form, but instead it posts the original data posted as "default". I guess this is a problem in my form code. I guess I´m not coding "default" values right.

The second problem is pretty strange. I retrieve rows from "news" table in reverse order, but when I "submit" the form associated with a particular row, it posts the data from the first row, not the row I´m interested in.

This is my code in the first php file, which retrieves data and renders forms:

<html>

<head>          
    <?php
        include "connectToNews.php";
        mysqli_set_charset($conToNews,"utf8");
        $query = mysqli_query ($conToNews, "SELECT * FROM news ORDER BY id DESC");

    ?>

</head>

<body>

    <?php 

        while ($newsArray = mysqli_fetch_array($query, MYSQLI_ASSOC)) {
            echo "<form action='newsUpdater.php' method='post' enctype='multipart/form-data'>";
            echo "<p>".$newsArray['id']."</p><br>";
            echo "<input name='Id' type='hidden' value='".$newsArray['id']."'>";
            echo "<input class='input' name='Fecha' type='text' value='".$newsArray['fecha']."'><br>";
            echo "<textarea class='textarea' name='Headline' type='text'>".$newsArray['headline']."</textarea><br>";
            echo "<textarea class='textarea' name='Story' type='text'>".$newsArray['story']."</textarea><br>";
            echo "<input type='submit' value='Actualizar'><br><br><br>";
            echo "</form>";
        }
    ?>

</body>
</html>

So, as you can see, I render a new <Form> for each existing row. I use 2 <input> tags and 2 <textarea> tags. One of the <input> tags is hidden and has he "Id" info associated with the particular row data. In anycase, I use "echo" with this Id data to verify that is retrieving ok (and it is). I use "value" attribute to set the retrieved text as default text in this <input> tags. In the <textarea> tags, I use the space between the opening tag and the closing tag to locate the "default" text.

At this point, everything renders ok, I get as many forms as there are rows in "news" table and and when i press submit button, it takes me to the second php file.

The second php file is the "data updater". The code is the faollowing:

<html>

<head>
    <?php 
        $Id=$_POST['Id'];
        $Fecha=$_POST['Fecha'];
        $Headline=$_POST['Headline'];
        $Story=$_POST['Story'];

        echo "<p>".$Id."</p><br>";
        echo "<p>".$Fecha."</p><br>";
        echo "<p>".$Headline."</p><br>";
        echo "<p>".$Story."</p><br>";

        include "connectToNews.php";
        mysqli_set_charset($conToNews,"utf8");
        $query=mysqli_query ($conToNews, "UPDATE news SET fecha='$Fecha' headline='$Headline' story='$Story' WHERE id='$Id'");
    ?>
</head>

<body>
    <?php 
        echo "<p>News updated</p><br>";
        echo "<p><a href='updateNews.php'>Go back to form</a></p>";
    ?>
</body>
</html>

As you can see, I´m saving the posted data "$_POST['whatever']" into 4 variables, just to have an easier time writting the future mySql query.

Then, I echo this variables to check what info is really been passed. And this is where it gets weird, because te rendered texts are the ones retrieved from to the first row in my "news" table, no matter which row am I editing in the form or what I´m writting in the form.

The other problem is that, regard of getting the "ok" message related to the updating process, the data never saves to "news" table. Although, I could be wrong, because I´m really injecting the original text from row 1 into row 1, no matter of which row I was really trying to edit.

Could you read my code and tell me if you guys see any problem.

Thanks!!!

In an UPDATE query the columns being updated must be seperated by commas, this explains why your data is not being updated.

The reason you didnt know for sure that the query was failing, and why, is that you are not testing that the query actually worked or not.

It is always a VERY good idea to test the results of all MYSQLI_ calls so I would add. This will then show you an error message that would help in bebugging

$query=mysqli_query ($conToNews, 
        "UPDATE news SET fecha='$Fecha', 
                         headline='$Headline',
                         story='$Story' 
         WHERE id='$Id'");


if ( $query === FALSE ) {
    echo mysqli_error($conToNews);
    exit;
}

You have some SQL Injection issues in this code, you should read How can I prevent SQL injection in PHP?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM