I am relatively new to PHP and I attempting to teach myself how to use PHP, MYSQL, and Apache as I work on designing a webpage. At the moment I am working on a page to insert user-added information into my mysql database. My computer is set up as a local server that I am running my code on. I had no problems generating the form and no problems inserting the information into the database. After the user hits submit and the information is inserted into the table I would like the page to refresh so the user can add additional information. To refresh the page I use the Header : location function. When I hit submit I get the following error: "Warning: Cannot modify header information - headers already sent by (output started at C:\webdev\newlink.php:3) in C:\webdev\newlink.php on line 20". The data is entered into my table.
A couple years ago I was doing some work with PHP for my job and one of the guys I worked with helped me through a lot of it. I had programmed a page that performed the same task. I was comparing my code for the project with the code in my current project and it looks similar so I am not sure where the error is. I tried looking this error up online and I saw there are a lot of people that have this error but nothing I read was able to help me. I spent a large part of this afternoon trying to modify my code to get it to work. I kind of understand why the error exists, It is just not obvious to me what I need to fix.
Here is my PHP setup.
PHP Version: 5.2.6
Display Errors: On
Error Level: Not E_ALL
Register Globals: Off
Here is my code for this form:
Code: Select all
<html>
<body>
<?php
include("config.php");
if(! empty($_REQUEST['submit']))
{$subject='';
for ($i=0; $i<count($_POST['subject']); $i++)
{
$subject .= $_POST['subject'][$i];
}
$dbconnect = do_connect();
$linkaddress = '"' . trim($_REQUEST['link']) . '"';
$linkname = '"' . trim($_REQUEST['name']) . '"';
$linkdescription = '"' . trim($_REQUEST['description']) . '"';
$date = date("Ymd");
$sql = "INSERT INTO hyperlink (link, name, description, subject, date_Added) values (" . $linkaddress . "," .$linkname . "," . $linkdescription . ", '" . $subject . "' ," . $date . ")";
mysqli_query($dbconnect, $sql) or die("Error: Failed to run insert query. <br>");
unset ($linkaddress,$linkname,$linkdescription,$date,$subject);
header("Location: http://localhost/newlink.php");
exit;
}
?>
<form action="newlink.php" method="post"><table>
<tr><td>Address: </td><td> <input type="text" name="link" size="65"/></td></tr>
<tr><td>Link Name: </td><td> <input type="text" name="name"/></td></tr>
<tr><td>Description: </td><td> <TEXTAREA NAME="description" Rows="4" Columns = "65"></TEXTAREA></td></tr>
<tr><td>Subject: </td><td>
Physics <input type = "checkbox" name = "subject[]" value ="PH"/>
Earth Science <input type = "checkbox" name = "subject[]" value = "ES"/>
Biology <input type = "checkbox" name = "subject[]" value = "BIO"/>
Chemistry <input type = "checkbox" name = "subject[]" value = "CH" />
Math <input type = "checkbox" name = "subject[]" value = "MA" />
Misc. <input type = "checkbox" name = "subject[]" value = "MISC"/></td></tr></table>
<input type="submit" name="submit" value="Submit"><input type="reset" name="reset" value="Clear Fields">
</form>
</body>
</html>Neil