I have a news manager system and on the add a news item page I want to give the user the ability to upload an image with the story.
My php is:
<?
// form not yet submitted
// display initial form
if (!$submit)
{
?>
<table cellspacing="5" cellpadding="5">
<form action="<? echo $PHP_SELF; ?>" method="POST" name="theform" id="theform">
<tr>
<td width="100" align="right"><font class="text-blog"><b>Headline: </b></font></td>
<td align="left"><input size="40" maxlength="250" type="text" name="headline"></td>
</tr>
<tr>
<td width="100" align="right"><font class="text-blog"><b>Story: </b></font></td>
<td align="left"><textarea name="story" cols="40" rows="10"></textarea></td>
</tr>
<tr>
<td width="100" align="right"><font class="text-blog"><b>Image: </b></font></td>
<td align="left"><input size="40" maxlength="250" type="text" name="image"> <input name="upload" value="Upload Image" type="button"></td>
</tr>
<tr>
<td colspan=2 align="center"><input type="Submit" name="submit" value="Add"></td>
</tr>
</form>
</table>
<?
}
else
{
// includes
include("dbx.php");
// set up error list array
$errorList = array();
$count = 0;
// validate text input fields
if (!$headline) { $errorList[$count] = "Invalid entry: Headline"; $count++; }
// check for errors
// if none found...
if (sizeof($errorList) == 0)
{
// open database connection
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");
// select database
mysql_select_db($db) or die ("Unable to select database!");
// generate and execute query
$query = "INSERT INTO news(headline, story, image) VALUES('$headline', '$story', '$image')";
$result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());
// print result
echo "<font class='text-blog'>Your link was successfully added and saved to the database. </font><a href='admin/main.php'>Go back to the control panel menu</a>.</font>";
// close database connection
mysql_close($connection);
}
else
{
// errors found
// print as list
echo "<font size=-1>The following errors were encountered: <br>";
echo "<ul>";
for ($x=0; $x<sizeof($errorList); $x++)
{
echo "<li>$errorList[$x]";
}
echo "</ul></font>";
}
}
?>