0

I am learning basic php since September 2015, and I have to create a website with a fully functioning php update code.

What I have right now is a news page with 3 rows that have to be updated in an administration panel with a submit button for every field (date, title and content), so there are 3 forms for 3 news articles.

[https://i.stack.imgur.com/5cUwK.png picture showing the fields.

So everytime a new row gets created (even though there will always be 3), a new form appears in the administration panel.

Now, the problem that I have is that only the last field gets updated. For example, I type information in all 3 forms, but only the last form gets the updated information shown on the news page when pressing the submit button according to the corresponding form. The updated information is also not getting added in the database.

If I type info in the first content field (inhoud) and press update, it will only check if the last form is updated.

Thinking about it, this would mean that my code only checks if the last id of the database, right? For testing, I added a new row in the database with an auto-incrementing id of 4. And the result is that only the fourth id is checked for updating. It also says in my error checker:

"NO RECORDS UPDATED ... ... ... WHERE ID=9"

This is all the information I could think of to put in here.

This is my php code:

<?php
session_start();
if(!isset($_SESSION['gebruiker'])){
    header ("Location: admin_login.php");
}
else{

require_once 'db_config.php';

$query = "
    SELECT 
        * 
    FROM 
        gip_home_nieuws";
$result = mysql_query($query);
    if(!$res = mysql_query($sql))
    {
        trigger_error(mysql_error().'<br />In query: '.$sql);
    }
    elseif(mysql_num_rows($res) == 0)
    {
        echo 'Geen resultaten gevonden';
    }
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Pagina's updaten</title>
<link href="admin_page1_css.css" rel="stylesheet" type="text/css">
</style>
</head>
<body>
<div id="container">
<div id="cont_header">
    <div id="header">
    <div id="header_logo"><a href="home.php"><img src="images/logo.png" width="200" height="110"></a></div>
        <div id="header_slogan">We secure your future, the easy way</div>
        <div id="knop_cont">
            <div class="navigatie_knop" id="knop_nieuws"><a href="nieuws.php">Nieuws</a></div>
            <div class="navigatie_knop" id="knop_overons"><a href="overons.php">Over ons</a></div>
            <div class="navigatie_knop" id="knop_producten"><a href="producten.php">Producten</a></div>
            <div class="navigatie_knop" id="knop_winkel"><a href="winkel.php">Winkel</a></div>
            <div class="navigatie_knop" id="knop_contact"><a href="contact.php">Contact</a></div>
            <div class="navigatie_knop" id="knop_uitloggen"><a href="admin_logout.php">Uitloggen</a></div>
        </div> 
    </div>
</div>
<div id="divider1"></div>
<div id="cont_inhoud">
<div id="contact1">
<div class="contact_titel id="contact1_t>Administratie</div>
<div id="contact1_c">
<form method="post" action="<?php $_PHP_SELF ?>">
<table width="400" border="0" cellspacing="1" cellpadding="2">
<tr>

<?php 

while($row = mysql_fetch_array($result))
            {

$id = $row['id'];
$nieuws_date = $row['nieuws_date'];
$nieuws_titel = $row['nieuws_titel'];
$nieuws_content = $row['nieuws_content'];
?>


<td width="100"></td>
</tr>
<tr>
<td width="100">id</td>
<td><input class="input1" name="form_id" type="text" value="<?=$id?>"></td>
</tr>
<tr>
<td width="100">Datum</td>
<td><input class="input1" name="form_date" type="text" value="<?=$nieuws_date?>"></td>
</tr>
<tr>
<td width="100">Titel</td>
<td><input class="input2" name="form_titel" type="text" value="<?=$nieuws_titel?>"></td>
</tr>
<tr>
<td width="100">Inhoud</td>
<td><textarea class="input3" name="form_content" type="text"><?=$nieuws_content?></textarea></td>
</tr>
<tr>
<td width="100"> </td>
<td> </td>
</tr>
<tr>
<td width="100"> </td>
<td>
<p><input type="submit" name="updaten" value="gegevens updaten"/>
<?php } ?></p>
</td>
</tr>
</table>
</form>



<?php

if(isset($_POST[updaten]))
{
$id2 = $_POST['form_id'];
$nieuws_date2 = $_POST['form_date'];
$nieuws_titel2 = $_POST['form_titel'];
$nieuws_content2 = $_POST['form_content'];

$sql = "
        UPDATE
            gip_home_nieuws
        SET
            nieuws_date = '".$nieuws_date2."',
            nieuws_titel = '".$nieuws_titel2."',
            nieuws_content = '".$nieuws_content2."'
        WHERE
            id=$id2
    ";
    }
    if(!$res = mysql_query($sql)) {
        trigger_error(mysql_error().'<br />In query: '.$sql);
    }
    elseif(mysql_affected_rows() == 0) {
        echo 'Geen records gegwijzigd. <br />Query: '.$sql;
        echo"<a href=\"index.php\">Terug</a";
    }
    else
    {
        echo 'Update was succesvol!';
        echo"<a href=\"index.php\"<br><br>>Terug</a";
    }
?>
</div>
</div>
</div>
<div id="divider2"></div>
<div id="footer">
    <div id="cont_footer">
      <div id="footer_info">© Belgian Space Industries 2016</div>
    </div>
</div>
</div>
<?php } ?>
</body>
</html>

I would really appreciate some help!

EDIT: seeing some posts with comments saying I shouldn't use mysql_* functions, I was teached to use MYSQL_* functions.

So sorry if there is confusion about this, but I can't really do something about this!

  • [Little Bobby](http://bobby-tables.com/) says [your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php). Even [escaping the string](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) is not safe! – Jay Blanchard May 26 '16 at 12:27
  • 1
    Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard May 26 '16 at 12:27

1 Answers1

0

If you run this code and fill in each text input what happens when you submit the form?

<?php
var_dump($_POST)
?>
<form method="POST">
    <input type="text" name="foo">
    <input type="text" name="foo">
    <input type="text" name="foo">
    <input type="submit">
</form>

As all three of those foo fields above share the same name, when you POST, only one of the foo input's value will be available (it will clobber the other two).

You need to create three separate forms by creating each form inside your loop. Currently you have one form (your form html tags surround the loop).

You want something more akin to this:

<?php for($i=0; $i<3; $i++ ) { ?>
    <form method="POST">
        <table>
            <tr>
                <td>
                    Foo:
                </td>
                <td>
                    <input type="text" name="foo">
                </td>
            </tr>
            <tr>
                <td>
                </td>
                <td>
                    <input type="submit">
                </td>
            </tr>
        </table>
    </form>
<?php } ?>

Swap out the for for your while.

You should at least escape submitted values within your queries to reduce the chances of sql injection.

$sql = "
        UPDATE
            foo_table
        SET
            bar_field = '".mysql_real_escape_string($bar_value)."',
        WHERE
            baz_field = $baz_value
    ";

As others have pointed out the mysql functions are deprecated in PHP 5.5.0, and removed in PHP 7.0.0. Mysqli is a similar an alternative extension.

Progrock
  • 7,373
  • 1
  • 19
  • 25
  • Thanks for the comment. When filling each field with test1 - test2 - test3, it displays following code on top: array(1) { ["foo"]=> string(5) "test3" } Would you mind giving me a basic code of where to put the loop exactly? I am still pretty much beginner in php and covered loops very little. – Mauro Cardinaels May 26 '16 at 12:37
  • @MauroCardinaels added an example of form creation within a loop. – Progrock May 26 '16 at 13:00
  • Changing $i<3 to $i<1 completely fixed my issue! Thanks for the help, appreciate it! – Mauro Cardinaels May 26 '16 at 13:27
  • @MauroCardinaels, I think you missed the point. The for loop was just an example of creating three individual forms using a loop. I meant for you to swap the for loop with your while loop. (If you changed the for condition to $i<1, it will just run the above loop once.) – Progrock May 26 '16 at 13:30