Just as I thought “It works!”, a new problem surfaced - the classic(?) “Warning: Cannot modify header information - headers already sent”.
Here is the Edit form (changes the content of the database):
if (isset($_POST[uppdatera]))
{
include 'connect.php';
$rubrik = mysqli_real_escape_string($link, $_POST['rubrik']);
$brodtext = mysqli_real_escape_string($link, $_POST['brodtext']);
$id = mysqli_real_escape_string($link, $_POST['id']);
$sql = "UPDATE artiklar SET
artikel='$brodtext',
namn='$rubrik'
WHERE id='$id'";
if (!mysqli_query($link, $sql))
{
$error = 'Error updating submitted joke.';
include 'error.html.php';
exit();
}
header('Location: update.php');
exit();
}
On the update.php there is a message “The update was successful”.
When I click the update button the headers already sent error message appears.
This is the entire admin.php:
<div id="header">
if(isset($_SESSION['mypassword']))
{
echo "<p id='welcome'>Welcome " . $_SESSION['myusername'] . ", you are logged in! </p>";
echo "<h1> Webbadministration</h1>";
}
else
{
header("location:index.php");
session_destroy();
}
</div>
<div id="container">
<div id="lagg_till">
<h3> Add content</h3>
<form action="" id="form1" method="post">
<div>
<label for="namn">Artikelrubrik:</label><br />
<input name="namn" type="text" id="namn" style="width:400px;" /><br /><br />
<label for="artikel">Artikeltext:</label><br />
<textarea id="artikel" name="artikel" rows="20" cols="60"></textarea><br /><br />
<label for="datum">Datum:</label><br /><br />
<input name="datum" type="text" id="datum" /><br />
</div>
<div><input type="submit" value="Add"/></div>
</form>
</div>
<a href="http://localhost/" id="home">Home</a>
<?php
//---------------INSERT INTO DATABASE---------------------------
include 'connect.php';
if (isset($_POST['namn']))
{
$namn = mysqli_real_escape_string($link, $_POST['namn']);
$artikel = mysqli_real_escape_string($link, $_POST['artikel']);
$datum = mysqli_real_escape_string($link, $_POST['datum']);
$sql = 'INSERT INTO artiklar SET
namn="' . '<h3>' . $namn . '</h3>' . '",
datum="' . $datum . '",
artikel="' . '<p>' . $artikel . '</p>' . '"';
if (!mysqli_query($link, $sql))
{
$error = 'Error adding submitted joke: ' . mysqli_error($link);
include 'error.html.php';
}
header('Location: add.php');
exit();
}
?>
<div id="edit">
<h3>Artikellista</h3>
<?php
include 'connect.php';
//---Show Content--------------
$result = mysqli_query($link, 'SELECT id, namn FROM artiklar ORDER BY datum DESC');
if (!$result)
{
$error = 'Error fetching artikelrubrik from database!';
include 'error.html.php';
exit();
}
while ($row = mysqli_fetch_array($result))
{
$authors[] = array('id' => $row['id'], 'namn' => $row['namn']);
}
include 'authors.html.php';
//---- Delete Content---------------
if (isset($_POST['action']) and $_POST['action'] == 'Delete')
{
$id = mysqli_real_escape_string($link, $_POST['id']);
// Delete quotes belonging to author
$sql = "DELETE FROM artiklar.artiklar WHERE artiklar.ID = '$id'";
if (!mysqli_query($link, $sql))
{
$error = 'Error deleting jokes for author.';
include 'error.html.php';
exit();
}
header('Location: admin.php');
}
//---- Edit Content---------------
if (isset($_POST['action']) and $_POST['action'] == 'Edit')
{
include 'connect.php';
$id = mysqli_real_escape_string($link, $_POST['id']);
$sql = "SELECT id, namn, artikel FROM artiklar WHERE id='$id'";
$result = mysqli_query($link, $sql);
while ($row = mysqli_fetch_array($result))
{
include 'testing.php';
}
}
//-------------EDIT---------------------------
if (isset($_POST['updatera']))
{
include 'connect.php';
$rubrik = mysqli_real_escape_string($link, $_POST['rubrik']);
$brodtext = mysqli_real_escape_string($link, $_POST['brodtext']);
$id = mysqli_real_escape_string($link, $_POST['id']);
$sql = "UPDATE artiklar SET
artikel='$brodtext',
namn='$rubrik'
WHERE id='$id'";
if (!mysqli_query($link, $sql))
{
$error = 'Error updating submitted joke.';
include 'error.html.php';
exit();
}
header('Location: update.php');
exit();
}
</div>
<div id="footer">The PHP Webpage, 2003-<?php echo date('Y');?></div>
</div>
</body>
</html>