I am a novice php developer and i have hit a snag on my new skill learning exercise i have web page that is sending data to a php script and the php file is supposed to upload the new data to the database but instead when i hit the submit button on my form i am linked to my php page and all the code is displayed.
my html form:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Untitled 1</title>
</head>
<body>
<form method="post" action="do_addrecord.php">
<table cellspacing="3" cellpadding="3">
<tr>
<td>
<p>name: <br/> <input type="text" name="name" size="10" maxlength="50"/></p>
</td>
</tr>
<tr>
<td>
<p>image:<br/> <input type="file" name="image" size="10" /></p>
</td>
</tr>
<tr>
<td>
<p>Price: <br/> <input type="text" name="price" size="14" maxlength="14"/></p>
</td>
</tr>
<tr>
<td>
<p>inventory: <br/> <input type="text" name="inventory" size="11" maxlength="1"/></p>
</td>
</tr>
<tr>
<td>
<select name="category" size="1"> Select Your category
<option selected ="selected"/>--Choose One--
<option/>magical
<option/>mundane
<option/>candles
<option/>oil
<option/>jewelery
<option/>copper
<option/>clothing
<option/>athames
<option/>bakery
<option/>dragon
<option/>lion
<option/>one
</select>
</td>
</tr>
<tr>
<td>
<p>subcat: <br/> <input type="text" name="subcategory" size="10" maxlength="50"/></p>
</td>
</tr>
<tr>
<td>
<p> <input type="submit" name="submit" value="add record" maxlength="50"/></p>
</td>
</tr>
</table>
</form>
</body>
</html>
myphp file:
<?
if((!$_POST['name']) || (!$_POST['image']) ||
($_POST['price']) || ($_POST['inventory']) || ($_POST['category'])
|| ($_POST['subcategory']))
{
header(" Location: /addinventory.html");
exit;
}
$db_name = "db";
$table_name= "inventory"
$connection = @mysql_connect("localhost", "name",
"******") or die(mysql_error());
$db = @mysql_select_db($db_name, $connection) or
die(mysql_error());
$sql = "INSERT INTO $table_name
(name,image,price,inventory,category,subcategory) VALUES
('$_POST[name]', '$_POST[image]', '$_POST[price]',
'$_POST[inventory]', '$_POST[category]', '$_POST[subcategory]')";
$result = @mysql_query($sql,$connection) or
die(mysql_error());
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type"
/>
<title>Untitled 1</title>
</head>
<body>
<h1>Adding a record to<? echo "$table_name"; ?></h1>
<table cellspacing="3" cellpadding="3">
<tr>
<td>
<p>name: <? echo "$_POST[name]"; ?></p></td>
</tr>
<tr>
<td>
<p>image: <? echo "$_POST[image]"; ?></p>
</td>
</tr>
<tr>
<td>
<p>Price: <? echo "$_POST[price]"; ?></p></td>
</tr>
<tr>
<td>
<p>inventory: <? echo "$_POST[inventory]"; ?></p>
</td>
</tr>
<tr>
<td>
<p>category: <? echo "$_POST[category]"; ?></p>
</td>
</tr>
<tr>
<td>
<p>subcat: <? echo "$_POST[subcategory]"; ?></p>
</td>
</tr>
</table>
</body>
</html>
my html form:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Untitled 1</title>
</head>
<body>
<form method="post" action="do_addrecord.php">
<table cellspacing="3" cellpadding="3">
<tr>
<td>
<p>name: <br/> <input type="text" name="name" size="10" maxlength="50"/></p>
</td>
</tr>
<tr>
<td>
<p>image:<br/> <input type="file" name="image" size="10" /></p>
</td>
</tr>
<tr>
<td>
<p>Price: <br/> <input type="text" name="price" size="14" maxlength="14"/></p>
</td>
</tr>
<tr>
<td>
<p>inventory: <br/> <input type="text" name="inventory" size="11" maxlength="1"/></p>
</td>
</tr>
<tr>
<td>
<select name="category" size="1"> Select Your category
<option selected ="selected"/>--Choose One--
<option/>magical
<option/>mundane
<option/>candles
<option/>oil
<option/>jewelery
<option/>copper
<option/>clothing
<option/>athames
<option/>bakery
<option/>dragon
<option/>lion
<option/>one
</select>
</td>
</tr>
<tr>
<td>
<p>subcat: <br/> <input type="text" name="subcategory" size="10" maxlength="50"/></p>
</td>
</tr>
<tr>
<td>
<p> <input type="submit" name="submit" value="add record" maxlength="50"/></p>
</td>
</tr>
</table>
</form>
</body>
</html>
myphp file:
<?
if((!$_POST['name']) || (!$_POST['image']) ||
($_POST['price']) || ($_POST['inventory']) || ($_POST['category'])
|| ($_POST['subcategory']))
{
header(" Location: /addinventory.html");
exit;
}
$db_name = "db";
$table_name= "inventory"
$connection = @mysql_connect("localhost", "name",
"******") or die(mysql_error());
$db = @mysql_select_db($db_name, $connection) or
die(mysql_error());
$sql = "INSERT INTO $table_name
(name,image,price,inventory,category,subcategory) VALUES
('$_POST[name]', '$_POST[image]', '$_POST[price]',
'$_POST[inventory]', '$_POST[category]', '$_POST[subcategory]')";
$result = @mysql_query($sql,$connection) or
die(mysql_error());
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type"
/>
<title>Untitled 1</title>
</head>
<body>
<h1>Adding a record to<? echo "$table_name"; ?></h1>
<table cellspacing="3" cellpadding="3">
<tr>
<td>
<p>name: <? echo "$_POST[name]"; ?></p></td>
</tr>
<tr>
<td>
<p>image: <? echo "$_POST[image]"; ?></p>
</td>
</tr>
<tr>
<td>
<p>Price: <? echo "$_POST[price]"; ?></p></td>
</tr>
<tr>
<td>
<p>inventory: <? echo "$_POST[inventory]"; ?></p>
</td>
</tr>
<tr>
<td>
<p>category: <? echo "$_POST[category]"; ?></p>
</td>
</tr>
<tr>
<td>
<p>subcat: <? echo "$_POST[subcategory]"; ?></p>
</td>
</tr>
</table>
</body>
</html>