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!