Ok, I am walking my way through an instructional php book and basically going over the basics again. After I finished doing the script the way the book said to do it, I kept getting a header error saying that the headers have already been submitted. The book had my echo statements spread out all over the place instead of just doing a self processing file. You know? The kind that looks back at itself for print commands once a submit button his clicked. SO I modified the file to be self contained. Seems to work with just one problem that I cannot figure out how to fix. It keeps adding the same submission to my database every time the refresh button is clicked. So until you add a new joke to it, the old one will be re-added. And the mayhem continues with the new joke. (It's a lousy joke database for testing purposes) Here's the code:
<?php
if (get_magic_quotes_gpc())
{
function stripslashes_deep($value)
{
$value = is_array($value) ?
array_map('stripslashes_deep', $value) :
stripslashes($value);
return $value;
}
$_POST = array_map('stripslashes_deep', $_POST);
$_GET = array_map('stripslashes_deep', $_GET);
$_COOKIE = array_map('stripslashes_deep', $_COOKIE);
$_REQUEST = array_map('stripslashes_deep', $_REQUEST);
}
if (isset($_GET['addjoke']))
{
echo '<form action="?" method="post">
<div>
<label for="joketext">Type your joke here:</label>
<textarea id="joketext" name="joketext" rows="3" cols="40">
➥</textarea>
</div>
<div><input type="submit" value="Add"/></div>
</form>';
exit();
}
else
{
echo '<p><a href="?addjoke" >Add your own joke<a></p>';
}
echo '<p>Here are all the jokes in the database:</p>';
$link = mysqli_connect('********************', '****', '*********************');
if (!$link)
{
$output = 'Unable to connect to the database server' .mysqli_error($link). ' ';
include 'output.html.php';
exit();
}
if (!mysqli_set_charset($link, 'UTF8'))
{
$output = 'unable to set database connection encoding ' .mysqli_error(). ' ';
include 'output.html.php';
exit();
}
if (mysqli_select_db($link, 'dante2'))
{
echo 'Database connection established.';
}
else
{
echo 'Unable to locate the dante2 database. ' .mysqli_error($link). ' ';
}
if (isset($_POST['joketext']))
{
$joketext = mysqli_real_escape_string($link, $_POST['joketext']);
$sql = 'INSERT INTO joke SET
joketext = " ' .$joketext. ' ",
jokedate = CURDATE() ';
if (!mysqli_query($link, $sql))
{
echo 'Error adding submitted joke: ' .mysqli_error($link);
}
foreach ($jokes as $joke)
{
echo "<blockquote><p>$joke</p></blockquote>";
}
}
$result = mysqli_query($link, 'SELECT joketext FROM joke');
if (!$result)
{
echo 'Error fetching jokes: ' .mysqli_error($link);
exit();
}
while ($row = mysqli_fetch_array($result))
{
$jokes[] = $row['joketext'];
}
foreach ($jokes as $joke)
{
echo "<blockquote><p>$joke</p></blockquote>";
}
?>
I know I am missing something, I'm just not sure what. I believe the submission is being held in memory somewhere, I'm just aware of how to clear it. Thanks for any pointers. =)Heck, I'll settle for hints. I'm not lazy, just lost atm.